Results 1 to 4 of 4

Thread: @@Identity Output

  1. #1
    Join Date
    Jan 2003
    Location
    UK
    Posts
    55

    @@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

  2. #2
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    --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

  3. #3
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    --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

  4. #4
    Join Date
    Sep 2002
    Posts
    5,938
    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
  •