出于测试目的,我想知道是否可以合并以下两个选项:
begin transaction
select x
from example_table
update example_table
set x = 'new value'
select x
from example_table
rollback transaction
基本上,我希望看到结果输出如下:
列名,更新列名
答案 0 :(得分:5)
如果我理解正确,您可以使用OUTPUT子句(SQL Server 2005 +)
use tempdb
go
create table #tbl (i int)
insert into #tbl values (10),(20),(30),(40)
update #tbl
set i=i+1
output deleted.i i_old,inserted.i i_updated
drop table #tbl
从在线书籍
OUTPUT 子句从每个子句返回信息或表达式 受INSERT,UPDATE,DELETE或MERGE语句影响的行。
...
DELETED 是一个列前缀,用于指定由中删除的值 更新或删除操作。以DELETED为前缀的列反映了 UPDATE,DELETE或MERGE语句之前的值已完成。
...
INSERTED 是一个列前缀,用于指定由...添加的值 插入或更新操作。以INSERTED为前缀的列反映了 UPDATE,INSERT或MERGE语句完成后的值,但是 在触发器执行之前。
<强> EDITED 强>
begin transaction
update example_table
set x = 'new value'
output deleted.x old_value, inserted.x new_value
rollback transaction