Execute the following Microsoft SQL Server T-SQL script in Management Studio Query Editor to demonstrate capturing a single assigned IDENTITY or a range of IDENTITY values using the OUTPUT clause.
USE tempdb;
CREATE TABLE Employee
(
idnum int IDENTITY(1,1),
fname varchar (20),
minit char(1),
lname varchar(30)
);
INSERT Employee (fname, minit, lname)
VALUES ('Elvis', 'A', 'Presley');
INSERT Employee (fname, minit, lname)
VALUES ('Frank', 'A', 'Sinatra');
GO
DECLARE @IDrange TABLE (ID int, Seq int identity(1,1),
Dstamp datetime default (CURRENT_TIMESTAMP));
DECLARE @i int = 0;
WHILE (@i < 5)
BEGIN
INSERT Employee(fname, minit, lname)
OUTPUT inserted.idnum INTO @IDrange(ID)
SELECT fname, minit, lname
FROM Employee;
SET @i += 1;
END -- loop
SELECT * FROM @IDrange ORDER BY Seq;
GO
DROP TABLE tempdb.dbo.Employee;
No comments:
Post a Comment