用INSERTED表可以做到的,读出现在的记录,然后和INSERTED表的数据比较一下就可以了,最后取出列名等。

解决方案 »

  1.   

    同时写文件的触发
    create table tab(id int,name sysname,sex sysname,score1 int,score2 int,sum_score int)
    go
    create trigger tr1 on tab for insert as
    begin
    declare @id varchar(10),@name sysname,@sex sysname,@score1 sysname,@score2 sysname,@sum_score sysname,@sql varchar(8000)
    select @id='',@name='',@sex='',@score1='',@score2='',@sum_score='',@sql=''
    select @id=cast(id as varchar),@name=name,@sex=sex,@score1=cast(score1 as varchar),@score2=cast(score2 as varchar),@sum_score=cast(sum_score as varchar)
    from inserted
    select @sql= 'echo '+ 'INSERT Table1 VALUES('+@id+',"'+@name+'","'+@sex+'",'+@score1+','+@score2+','+@sum_score+')' + '>>c:\test.txt'
    exec master..xp_cmdshell  @sql
    end
    go
    insert tab values (1,'001','man',10,20,30)
      

  2.   

    create table 表 (a int)
    go
    insert 表 values (1)
    go
    CREATE TRIGGER 名 ON 表
    FOR UPDATE
    AS
    select a 更新前被删除的数据 from deleted
    select a 更新后被插入的数据 from inserted
    go
    ------------测试:
    update 表 set a=3
      

  3.   

    create table 表 (a int,b int)
    go
    insert 表 values (1,2)
    go
    CREATE TRIGGER 名 ON 表
    FOR UPDATE
    AS
    if update(a)
    begin
      select a 更新前a被删除的数据 from deleted
      select a 更新后a被插入的数据 from inserted
    end
    if update(b)
    begin
      select a 更新前b被删除的数据 from deleted
      select a 更新后b被插入的数据 from inserted
    end
    go
    ------------测试:
    update 表 set a=3
    -----------------
    update 表 set b=4