Groups | Blog | Home
all groups > sql server programming > april 2006 >

sql server programming : how to write a stored procedure that adds a record to a table and returns the value of the ID Column


keithb
4/15/2006 9:24:53 PM
Please help. I need to write a stored procedure that adds a record to a
table and returns the value of the ID Column.

Thanks,

Keith

Tibor Karaszi
4/16/2006 12:00:00 AM
Does the ID column have the identity property? If so:

USE tempdb
CREATE TABLE a(c1 int identity, c2 int)
GO
CREATE PROC p @val int, @myId int OUTPUT
AS
SET NOCOUNT ON
INSERT INTO a(c2) VALUES(@val)
SET @myId = SCOPE_IDENTITY()
GO
DECLARE @theId int
EXEC p @val = 23, @myId = @theId OUTPUT
PRINT @theId


--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/


[quoted text, click to view]
Robert Bravery
4/16/2006 12:00:00 AM
Assuming that your id column is an identity column, look at SCOPE_IDENTITY()

Robert

[quoted text, click to view]

AddThis Social Bookmark Button