DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
 
 
SITE SEARCH SQLUSA.com VIDEOS
 
 
SQL E/BOOKS   WORLD, USA & SQL NEWS   FORMAT
SCRIPTS SQL 2005 SQL 2008 ARTICLES
How to capture with OUTPUT the inserted auto identity value?

Execute the following Microsoft SQL Server Transact-SQL (T-SQL) script in Management Studio (SSMS) Query Editor to capture the auto identity insert value in the ScrapReason table:

 

/* Related to: @@identity, scope_identity() */

 

USE AdventureWorks2012;

 

DECLARE @ID TABLE (ID INT);

 

INSERT Production.ScrapReason (Name, ModifiedDate)

 

OUTPUT INSERTED.ScrapReasonID INTO @ID

 

VALUES

(N'Operator dropped it', GETDATE()),

(N'Forklift rolled over it', GETDATE()),

(N'Buckled under heavy staff member', GETDATE());

 

SELECT * FROM @ID;

/*

ID

124

125

126

*/

 

DELETE SR

FROM Production.ScrapReason SR

INNER JOIN @ID I

  ON I.ID = SR.ScrapReasonID;

-- (3 row(s) affected)

GO

 

 

Exam Prep 70-461
SQL 2016 DESIGN & PROGRAMMING
 
 
 
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE