USE tempdb; GO CREATE TABLE dbo.table1 ( id INT, employee VARCHAR(32) ) go INSERT INTO dbo.table1 VALUES (1, 'Fred') ,(2, 'Tom') ,(3, 'Sally') ,(4, 'Alice'); GO DECLARE @MyTableVar TABLE ( id INT, employee VARCHAR(32) ); PRINT 'table1, before delete' SELECT * FROM dbo.table1; DELETE FROM dbo.table1 OUTPUT DELETED.* INTO @MyTableVar WHERE id = 4 OR id = 2; PRINT 'table1, after delete' SELECT * FROM dbo.table1; PRINT '@MyTableVar, after delete' SELECT * FROM @MyTableVar; DROP TABLE dbo.table1; --Results --table1, before delete --id employee ------------- ------------------------------ --1 Fred --2 Tom --3 Sally --4 Alice -- --table1, after delete --id employee ------------- ------------------------------ --1 Fred --3 Sally --@MyTableVar, after delete --id employee ------------- ------------------------------ --2 Tom --4 AliceOutput với Insert
USE AdventureWorks2012; GO DECLARE @MyTableVar table( NewScrapReasonID smallint, Name varchar(50), ModifiedDate datetime); INSERT Production.ScrapReason OUTPUT INSERTED.ScrapReasonID, INSERTED.Name, INSERTED.ModifiedDate INTO @MyTableVar VALUES (N'Operator error', GETDATE()); --Display the result set of the table variable. SELECT NewScrapReasonID, Name, ModifiedDate FROM @MyTableVar; --Display the result set of the table. SELECT ScrapReasonID, Name, ModifiedDate FROM Production.ScrapReason; GO
Tham khảo link msdn sau để biết thêm các kiến thức về Output.
- http://msdn.microsoft.com/en-us/library/ms177564.aspx
Và một bài viết trong blog của Pinal Dave.
- http://blog.sqlauthority.com/2007/10/01/sql-server-2005-output-clause-example-and-explanation-with-insert-update-delete/
0 comments:
Post a Comment