1、有两个表T1,T2,如果T1表中的数据发生改变(INSERT,UPDATE,DELETE),则T2表中的相应数据也发生变化
   这个触发器要怎么写啊?2、在表T中,要在每天0点做一个更新操作,比如更新NUM字段为0,这个触发器又要怎么写呢?
不好意思,初写触发器,还望各位赐教!
谢谢了

解决方案 »

  1.   

    2、在表T中,要在每天0点做一个更新操作,比如更新NUM字段为0,这个触发器又要怎么写呢? 这要用到JOB
      

  2.   

    就是,定时的操作最好写个job,屡试不爽!
      

  3.   

    create trigger tri_g on 表
    for insert,update,delete
    as 
    begin
    ...语句
    end
      

  4.   

    给你个例子,很相似的
    use song 
    go
    if exists (select name from sysobjects
    where name= 'tr_songlistchanged' and type='TR')
    drop trigger tr_songlistchanged
    go
    create trigger tr_songlistchanged
    on songlist
    for delete,insert,update
    asdeclare @insertCount int
    declare @deleteCount int
    declare @typeChange varchar(6)
    declare @updateType varchar(4)select @insertCount=count(*) from inserted
    select @deleteCount=count(*) from deleted
    select @updateType=''
    select @typeChange=
    case
    when @insertCount>0 and @deleteCount>0
    then 'UPDATE'
    when @insertCount=0 and @deleteCount>0
    then 'DELETE'
    else 'INSERT'
    endif @typeChange='DELETE' select @updateType='old'
    insert into watchsong
    (TypeChange,SongID,Artist,Title,Songaddtime)
    select @typeChange+@updateType,SongID,Artist,Title,Songaddtime
    from deleted
      

  5.   

    其中当songlist表变化,watchsong表会相应地加入变化的内容
      

  6.   

    if exists(select name from sysobjects
     where name='sample_trig' and type='TR')
    drop trigger sample_trig
    go
    create trigger sample_trig
     on table_name
     for insert, update, delete
    as
    begin
     insert into table_name select * from inserted
     update table_name set .....
     delete from table_name where .....
    end