此文章发布于64
个月前,部分信息可能已经过时
,请自行斟酌确认。
返回插入的数据: output inserted.列
insert into T_Test(Province,City) output inserted.Province, inserted.City values('广东','深圳')
返回删除的数据: output deleted.列
delete from T_Test output deleted.* where id = 2 (返回删除数据所有列)
返回更新前后的数据(前两个的结合):
output deleted.列(更新前)
output inserted.列(更新后)
update T_Test set Province = '湖南', City = '郴州' output '(更新前)' + deleted.Province + deleted.City as [更新前], '(更新后)' + inserted.Province + inserted.City as [更新后] where id = 2
还可以将返回的结果保存在表变量中,这在删除数据时保存历史记录很实用
DECLARE @tempTable TABLE(
id int,
Province varchar(50),
City varchar(50)
)
DELETE FROM TestTB OUTPUT deleted.* INTO @tempTable WHERE id > 4
SELECT * FROM @tempTable