I want to create a procedure that inserts into a table and returns it’s primary key.
The primary key of table is uniqueidentyfire and it’s guid is true
This is the stored procedure:
ALTER PROCEDURE [dbo].[pr_Tbl_Erae_Insert]
@sfk_Dars varchar(20),
@sfk_Ostad varchar(20),
@byfk_Gerayesh tinyint,
@ifk_term int,
@guidErae_ID uniqueidentifier,
@byGroup_Number tinyint,
AS
-- INSERT a new row in the table.
INSERT [dbo].[Tbl_Erae]
(
[fk_Dars],
[fk_Ostad],
[fk_Gerayesh],
[fk_term],
[Erae_ID],
[Group_Number]
)
VALUES
(
@sfk_Dars,
@sfk_Ostad,
@byfk_Gerayesh,
@ifk_term,
ISNULL(@guidErae_ID, (newid())),
@byGroup_Number
)
I need to return Erae_Id when it inserted into table how can I do that?
Can I use from output variables? How?
There is an OUTPUT clause available, depending on your version; however, in this case it is probably easier (since you only have one row) to handle this separately:
then use
@guidErae_ID“as is” in theINSERT(noISNULL), and then either end with:and use
var result = (Guid)command.ExecuteScalar(), or mark@guidErae_IDasOUTPUTin the parameter definition, and query it after callingcommand.ExecuteNonQuery().