我想当text表一条数据有更新操作时,把原数据插入test2表中
create trigger test3 
on test for update
as
declare @usern char(20),@sex3 char(20)
select @usern=inserted.names from inserted
select @sex3=inserted.sex from inserted
begin
if @usern is not null
insert into test2(names,sex)
values(@usern,@sex3)
end我的这个test2中插入的也是更新后的数据,请问@usern和@sex3怎么赋值才能实现我要的效果,谢谢!

解决方案 »

  1.   

    create trigger test3
    on test for update
    as
    --插入更新后的数据
    insert into test2(names,sex)
    select names,sex from inserted
      

  2.   

    create trigger test3
    on test for update
    as
    --插入原数据
    insert into test2(names,sex)
    select names,sex from deleted
      

  3.   

    create trigger test3 
    on test for update 
    as 
    --插入更新前的数据
    insert into test2(names,sex)
    select names,sex from deleted
    go
      

  4.   

    create trigger test3 
    on test for update 
    as 
      insert into test2(names,sex) select names,sex from deleted
    end 
      

  5.   

    create trigger test3 on test 
    for update 
    as 
    insert into test2(names,sex) 
    select names,sex from inserted where names is not null
      

  6.   

    create trigger test3 on test 
    for update 
    as 
    --要插入test的原数据,应该用deleted表。
    insert into test2(names,sex) 
    select names,sex from deleted
      

  7.   

    不会test 每次更新数据,都要往test2里插入旧数据吧?
      

  8.   

    我是想每次更新test表 都把原数据插入test2中 哦    正确的实现方法到底是哪个啊?
      

  9.   

    我现在这样写
    create trigger test3 
    on test for update
    as
    declare @usern char(20),@sex3 char(20)
    select @usern=inserted.names from  deleted
    select @sex3=inserted.sex from  deleted
    begin
    if @usern is not null
    insert into test2(names,sex)
    values(@usern,@sex3)
    end
    错误是这样:
    服务器: 消息 107,级别 16,状态 2,过程 test3,行 5
    列前缀 'inserted' 与查询中所用的表名或别名不匹配。
    服务器: 消息 107,级别 16,状态 1,过程 test3,行 6
    列前缀 'inserted' 与查询中所用的表名或别名不匹配。
      

  10.   

    --这个应该符合楼主的意思。
    create trigger test3 on test 
    for update 
    as 
    --要插入test的原数据,应该用deleted表。
    --还要判断更新前的names不为NULL
    insert into test2(names,sex) 
    select names,sex from deleted where names is not null
      

  11.   


    create trigger test3 on test 
    for update 
    as 
    insert into test2(names,sex) 
    select names,sex from inserted where names is not null