Unable to output @@IDENTITY

  • I have created a stored procedure as follows:

    CREATE PROCEDURE [dbo].[InsertVersion]

    -- Add the parameters for the stored procedure here

    @Version_Name VARCHAR(50),

    @Starting_FC_Month VARCHAR(3),

    @Start_Date_Criteria VARCHAR(10),

    @End_Date_Criteria VARCHAR(10),

    @Created_By VARCHAR(100),

    @Shared INT,

    @Version_ID INT OUTPUT

    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 tblVersions

    (name, date_created, starting_fc_month, start_date_criteria, end_date_criteria, created_by, shared)

    VALUES

    (@Version_Name, GETDATE(), UPPER(@Starting_FC_Month), CAST(@Start_Date_Criteria AS DATETIME),

    CAST(@End_Date_Criteria AS DATETIME), @Created_By, @Shared)

    SET @Version_ID = @@IDENTITY

    RETURN @Version_ID

    END

    When I test the stored procedure using the following it returns NULL instead of the generated id?

    declare @id int

    exec InsertVersion 'EE','JUL','2001-01-01','2001-02-01','dw134121',1,@id

    select @id

  • Try this:

    exec InsertVersion 'EE','JUL','2001-01-01','2001-02-01','dw134121',1, @id OUTPUT

  • As simple as that! Thanks for your help!

  • David-155102 (2/1/2010)


    As simple as that! Thanks for your help!

    All I did was verify the EXECUTE statement syntax in Books Online (Shift {f1} in SSMS).

    Glad I could help.

  • I am assuming that there is an Identity Column in the tblVersion table 🙂

  • Didn't think of this earlier, but you really shouldn't be using @@IDENTITY either. You should be using SCOPE_IDENTITY().

  • Lynn Pettis (2/1/2010)


    Didn't think of this earlier, but you really shouldn't be using @@IDENTITY either. You should be using SCOPE_IDENTITY().

    Agreed to avoiding @@identity- although if you are in 2005 or later, I would use the OUTPUT clause instead. It seems to avoid ALL of the vulnerabilities with the three more "traditional" options. It's a bit more work to get there, but it seems to be bullet-proof.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Matt Miller (#4) (2/1/2010)


    Lynn Pettis (2/1/2010)


    Didn't think of this earlier, but you really shouldn't be using @@IDENTITY either. You should be using SCOPE_IDENTITY().

    Agreed to avoiding @@identity- although if you are in 2005 or later, I would use the OUTPUT clause instead. It seems to avoid ALL of the vulnerabilities with the three more "traditional" options. It's a bit more work to get there, but it seems to be bullet-proof.

    And using temp tables or table variables (depends on your needs) you can actually capture the identity value for multiple rows inserted at once.

  • Hi Matt,

    Can you give me an example please?

    Thanks

  • Assuming the identity column in your table is called version_id, your SP will look like

    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 tblVersions

    (name, date_created, starting_fc_month, start_date_criteria, end_date_criteria, created_by, shared)

    OUTPUT INSERTED.version_id

    VALUES

    (@Version_Name, GETDATE(), UPPER(@Starting_FC_Month), CAST(@Start_Date_Criteria AS DATETIME),

    CAST(@End_Date_Criteria AS DATETIME), @Created_By, @Shared)

    END

    You can also define a table variable and do something like

    OUTPUT col1, col2, col3 INTO @t

    Check BOL for more information.

    Regards,

    Willem
    http://wschampheleer.wordpress.com[/url]

  • Thanks Willem, I'll try that.

  • Matt Miller (#4) (2/1/2010)


    Lynn Pettis (2/1/2010)


    Didn't think of this earlier, but you really shouldn't be using @@IDENTITY either. You should be using SCOPE_IDENTITY().

    Agreed to avoiding @@identity- although if you are in 2005 or later, I would use the OUTPUT clause instead. It seems to avoid ALL of the vulnerabilities with the three more "traditional" options. It's a bit more work to get there, but it seems to be bullet-proof.

    Agreed. There was a bug with SCOPE_IDENTITY() and parallel plans. The OUTPUT clause does seem reliable - though more work, for sure.

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply