How find the last inserted identity value or values in the SQL server

CREATE TABLE [dbo].[CHECKIDENT](
[ID] [int] IDENTITY(1,1) NOT NULL,
[date] [date] NULL,
[descp] [varchar](10) NULL
) ON [PRIMARY]

GO

INSERT INTO DBO.CHECKIDENT(DESCP)

VALUES (‘CHECK’)

SELECT @@IDENTITY AS ‘LASTINSERTED IDENTITY VALUE’

INSERT INTO DBO.CHECKIDENT(DESCP)

VALUES (‘CHECK’),
(‘CHECK1’)

SELECT @@IDENTITY AS ‘LASTINSERTED IDENTITY VALUE’

–OR

— find all identity values insereted in the previous satement

declare @t table (ID int)

INSERT INTO DBO.CHECKIDENT(DESCP)

OUTPUT inserted.ID into @t(ID)

VALUES (‘CHECK3’),
(‘CHECK4’)

select * from @t

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s