-
@@Identity Output
I want to output the @@Identity value from a stored procedure which inserts some values into a table. The procedure is below, but I can't get it to work ????
Thanks in Advance
--Declare @Test int
--Exec spIdentityTest @Test output
CREATE PROCEDURE dbo.spIdentityTest
@intID int output
AS
Begin
INSERT INTO tblTest (myDescription1, myDescription2, myDescription3, myDescription4)
VALUES ('Text1', 'Text2', 'Text3', 'Text3')
RETURN --@@Identity
set @intID = @@Identity
print @intID
END
-
--Return is in the wrong place. use this.
Alter PROCEDURE dbo.spIdentityTest
@intID int output
AS
Begin
set @intID =0
INSERT INTO tblTest (myDescription1, myDescription2, myDescription3, myDescription4)
VALUES ('Text1', 'Text2', 'Text3', 'Text3')
set @intID = @@Identity
RETURN --@@Identity
--print @intID
END
-
--or use scope identity
Alter PROCEDURE dbo.spIdentityTest
@intID int output
AS
Begin
set @intID =0
INSERT INTO tblTest (myDescription1, myDescription2, myDescription3, myDescription4)
VALUES ('Text1', 'Text2', 'Text3', 'Text3')
set @intID = scope_Identity()
RETURN --@@Identity
--print @intID
END
GO
declare @x int
exec spIdentityTest @x OUTPUT
print @x
-
I think should use scope_Identity() in this case.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|