返回插入的数据: 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