两个表table1,table2,当table1的数据记录有增时,table2记录同步增加,但是对table1中的记录所作的任何其它操作(删除、修改)不对table2产生任何影响,如果写触发器?
在下初用sql server,不会写触发器,麻烦各位了!

解决方案 »

  1.   

    create trigger t_insert on table1
    for insert
    as
    begin
      insert into table2(column1,column2,...)
      select column1,column2,...
      from inserted
    end
    go
      

  2.   

    create trigger t_insert on table1
    for insert
    as
    begin
      insert table2 select * from inserted   ----这种是两个表的结构相同的情况
    end
    go
    create trigger t_insert on table1
    for insert
    as
    begin
        insert table2(col1,col2...) 
        select (col1,col2...) from inserted  ---这种是两个表的结构不同的情况,要把列名带上
    end
    go
      

  3.   

    加问:要用到三张表ta(tid,cid,cun,point)、tb(tid,cid,cun,point)、tc(tid,cid,cun,point),由于需求需要,它们要不断地动态更新生成,现已用存储过程实现,但是,有个问题,就是任意一张表里的数据都要和另两个进行数据整合,例如:ta里有记录ja,tb没有,则tb需要加一条记录,其tid,cid,cun都和ta表记录ja相同,但是point为0,能用触发器来实现这样的同步更新整合功能吗?触发器能嵌在存储过程里使用吗? 
      想法很多,就是不知道路能否走得通,诸位多指教!
      特别感谢leo_lesley(leo) ,还回了我另一个帖!