Wednesday, February 9, 2011

Using @@IDENTITY to return an identity value

Returning an identity value...

-- given a table like this..
CREATE TABLE Division
(DivisionId INT IDENTITY(1,1) PRIMARY KEY,
DivisionName varchar(100))

-- retrieve ID like this...
INSERT INTO Division (DivisionName)
VALUES (@DivisionName)
SELECT @DivisionID = @@IDENTITY

No comments: