Skip to content

Instantly share code, notes, and snippets.

@dibikhin
Created November 12, 2014 13:07
Show Gist options
  • Save dibikhin/1fe2425cd03c1869943b to your computer and use it in GitHub Desktop.
Save dibikhin/1fe2425cd03c1869943b to your computer and use it in GitHub Desktop.
T-SQL insert test returning Id of a new row
CREATE TABLE Customers
(
Id INT IDENTITY(1,1) NOT NULL,
Name NVARCHAR(255) NOT NULL
);
GO
SET IDENTITY_INSERT Customers OFF;
CREATE PROCEDURE CustomerAdd
(
@CustomerName NVARCHAR(255),
@Id INT OUTPUT
)
AS
BEGIN
INSERT INTO Customers
(Name)
VALUES
(@CustomerName);
SET @Id = SCOPE_IDENTITY();
END;
GO
DECLARE @NewId INT
EXEC CustomerAdd 'Jimmy', @NewId OUTPUT
SELECT @NewId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment