有两个表目标表tab1和源表tab2,这两个表不是完全相同的表,有部分字段一样,现在需要以个触发器,向目标表插入源表的字段数据,现在问题是,目标表有个ITM的字段,是记录数据行的项号,不能重复和为空,需要插入源表的数据时候,要根据已经存在的数据行的号码字段为条件,插入源表的相同号码字段的数据行,特别注意的是ITM的字段,要续接。
例如:mp_no  itm  prd_no  name  date(目标表取的字段)  mp_no  itm  prd_no  name date (源表要取的字段)
       001      1       a001      000     0000      001    1     c001  000   0000 
       001      2       a002      000     0000      001    2     c002  000   0000
       002      1       b001      000     0000      002    1     d001  000   000
       002      2       b002      000     0000      002    2     d002  000   0000
                                                    002    3     d004  000   0000
最后我想达到的效果是触发源表 
          mp_no    itm     prd_no    name    date    (目标表取的字段) 
           001      1       a001      000     0000                                      
           001      2       a002      000     0000                                     
           001      3       c001      000     0000 
           001      4       c002      000     0000 
           002      1       b001      000     0000                                       
           002      2       b002      000     0000     
           002      3       d002      000     0000   
           002      4       d004      000     0000
源表的data是条件 凡是为0000的 都要插入到目标表       

解决方案 »

  1.   

    使用触发器不太合适,可以考虑用存储过程,,,
    create table tab1(mp_no varchar(8), itm int, prd_no varchar(8), name varchar(16), date varchar(8))
    create table tab2(mp_no varchar(8), itm int, prd_no varchar(8), name varchar(16), date varchar(8))insert tab1 select '001', 1, 'a001', '000', '0000'
    union all select '001', 2, 'a002', '000', '0000' 
    union all select '002', 1, 'b001', '000', '0000' 
    union all select '002', 2, 'b002', '000', '0000' insert tab2 select '001', 1, 'c001', '000', '0000'
    union all select '001', 2, 'c002', '000', '0000' 
    union all select '002', 1, 'd001', '000', '000' 
    union all select '002', 2, 'd002', '000', '0000' 
    union all select '002', 3, 'd004', '000', '0000' insert tab1(mp_no, prd_no, [name], date, itm) select mp_no, prd_no, [name], date
    ,itm=(select max(itm) from tab1 where mp_no=tab2.mp_no)
    +(select count(1) from tab2 T where mp_no=tab2.mp_no and date='0000' and prd_no<=tab2.prd_no)
    from tab2 where date='0000'/*
    mp_no    itm         prd_no   name             date
    -------- ----------- -------- ---------------- --------
    001      1           a001     000              0000
    001      2           a002     000              0000
    001      3           c001     000              0000
    001      4           c002     000              0000
    002      1           b001     000              0000
    002      2           b002     000              0000
    002      3           d002     000              0000
    002      4           d004     000              0000(8 row(s) affected)
    */select * from tab1 order by mp_no,itmdrop table tab1,tab2
      

  2.   

    用存储过程:
    create table tab1(mp_no varchar(8), itm int, prd_no varchar(8), name varchar(16), date varchar(8))
    create table tab2(mp_no varchar(8), itm int, prd_no varchar(8), name varchar(16), date varchar(8))insert tab1 select '001', 1, 'a001', '000', '0000'
    union all select '001', 2, 'a002', '000', '0000' 
    union all select '002', 1, 'b001', '000', '0000' 
    union all select '002', 2, 'b002', '000', '0000' insert tab2 select '001', 1, 'c001', '000', '0000'
    union all select '001', 2, 'c002', '000', '0000' 
    union all select '002', 1, 'd001', '000', '000' 
    union all select '002', 2, 'd002', '000', '0000' 
    union all select '002', 3, 'd004', '000', '0000' go
    create procedure sp_Test
    as
    begin
    insert tab1(mp_no, prd_no, [name], date, itm) select mp_no, prd_no, [name], date
    ,itm=(select max(itm) from tab1 where mp_no=tab2.mp_no)
    +(select count(1) from tab2 T where mp_no=tab2.mp_no and date='0000' and prd_no<=tab2.prd_no)
    from tab2 where date='0000'
    end
    goexec dbo.sp_Testselect * from tab1 order by mp_no,itm
    /*
    mp_no    itm         prd_no   name             date
    -------- ----------- -------- ---------------- --------
    001      1           a001     000              0000
    001      2           a002     000              0000
    001      3           c001     000              0000
    001      4           c002     000              0000
    002      1           b001     000              0000
    002      2           b002     000              0000
    002      3           d002     000              0000
    002      4           d004     000              0000(8 row(s) affected)
    */drop procedure dbo.sp_Test
    drop table tab1,tab2