create table t
(col varchar(20))insert t
select '2' union all
select '3' union all
select '4' union all
select '5' union all
select '6'
go
create trigger t_update
on t
for update
as
 set xact_abort on
 begin tran
  select * from inserted
 commit tran
goupdate t set col=case col when '2' then '4'
                          when '3' then '5' 
                          when '4' then '6'
                          else col end
drop trigger t_update
drop table t
col                  
-------------------- 
4
5
6
5
6(所影响的行数为 5 行)
(所影响的行数为 5 行)

解决方案 »

  1.   

    这样就返回更新前的记录
    create table t
    (col varchar(20))insert t
    select '2' union all
    select '3' union all
    select '4' union all
    select '5' union all
    select '6'
    go
    create trigger t_update
    on t
    for update
    as
     set xact_abort on
     begin tran
      select * from deleted
     commit tran
    goupdate t set col=case col when '2' then '4'
                              when '3' then '5' 
                              when '4' then '6'
                              else col end
    drop trigger t_update
    drop table tcol                  
    -------------------- 
    2
    3
    4
    5
    6(所影响的行数为 5 行)
      

  2.   

    1. 可以用触发器, 在触发器中查询inserted逻辑表
    2. 如果你的表有标识列, 可以用@@identity返回刚插入记录的标识值, 通过这个标识值可以查到插入的记录
      

  3.   

    3. 如果你用sql 2005 , 可以在插入语句中, 指定output选项, 将刚入记录保存到一个表变量中.
      

  4.   

    --sql 2005中的output使用示例.USE tempdb
    GOCREATE TABLE dbo.ta
    (
    ID int
    )
    INSERT dbo.ta SELECT TOP (CAST(RAND()*100 as int)) id FROM sysobjects
    CREATE TABLE dbo.ta_deletelog
    (
    ID int,
    userid int
    )
    GODECLARE @deleted TABLE(ID int)
    DELETE dbo.ta
    OUTPUT deleted.* INTO @deleted
    INSERT dbo.ta_deletelog SELECT ID, @@SPID FROM @deleted
    SELECT * FROM dbo.ta_deletelog
    GODROP TABLE dbo.ta_deletelog, dbo.ta
      

  5.   

    刚刚更新的记录会保存在临时表 inserted,deleted里面的!做个触发器把操作的记录保存在别的表里就行了!insert 操作会保存在 inserted 里
    delete 操作会保存在 deleted 里
    update 操作其实就是一个先delete操作,后insert操作的过程!