Question : Stored procedure. Insert then return primary key.

I need a quick example of writing a stored procedure that inserts a record. then returns the primary key. Please no links.

The primary key is an integer that autofills in increments.

Answer : Stored procedure. Insert then return primary key.

Write your stored procedure and then at the end put

SELECT @@IDENTITY


For example:

CREATE PROCEDURE TestProcedure
      -- Add the parameters for the stored procedure here
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for procedure here
      INSERT INTO TestTable (column1, column2) VALUES ('test1', 'test2')

SELECT @@IDENTITY
END


If you are returning this to an application you are writing, you would change the SELECT @@IDENTIY to
SET @VariableName = SELECT @@IDENTITY
Random Solutions  
 
programming4us programming4us