USE AdventureWorks2008R2;GODECLARE @MyTableVar table( NewScrapReasonID smallint,Name varchar(50),ModifiedDate datetime);INSERT Production.ScrapReasonOUTPUT INSERTED.ScrapReasonID, INSERTED.Name, INSERTED.ModifiedDateINTO @MyTableVarVALUES (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, ModifiedDateFROM Production.ScrapReason;GO
create table TableWithIdentity( IdentityColumnName int identity(1, 1) not null primary key,... )
-- type of this table's column must match the type of the-- identity column of the table you'll be inserting intodeclare @IdentityOutput table ( ID int )
insert TableWithIdentity( ... )output inserted.IdentityColumnName into @IdentityOutputvalues( ... )
select @IdentityValue = (select ID from @IdentityOutput)
CREATE TABLE #foo(fooid INT IDENTITY NOT NULL,fooname VARCHAR(20))
SELECT @@Identity AS [@@Identity],Scope_identity() AS [SCOPE_IDENTITY()],Ident_current('#Foo') AS [IDENT_CURRENT]
SET IDENTITY_INSERT #foo ON
INSERT INTO #foo(fooid,fooname)VALUES (1,'one'),(2,'Two')
SET IDENTITY_INSERT #foo OFF
SELECT @@Identity AS [@@Identity],Scope_identity() AS [SCOPE_IDENTITY()],Ident_current('#Foo') AS [IDENT_CURRENT]
INSERT INTO #foo(fooname)VALUES ('Three')
SELECT @@Identity AS [@@Identity],Scope_identity() AS [SCOPE_IDENTITY()],Ident_current('#Foo') AS [IDENT_CURRENT]
-- YOU CAN INSERTSET IDENTITY_INSERT #foo ON
INSERT INTO #foo(fooid,fooname)VALUES (10,'Ten'),(11,'Eleven')
SET IDENTITY_INSERT #foo OFF
SELECT @@Identity AS [@@Identity],Scope_identity() AS [SCOPE_IDENTITY()],Ident_current('#Foo') AS [IDENT_CURRENT]
SELECT *FROM #foo