Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2004
    Posts
    46

    Unanswered: ID for New record

    Hi,

    How can I insert a new record in sql table and retrun the ID of that reocrd using a stored procedure?

    Thanks,

  2. #2
    Join Date
    May 2004
    Posts
    125
    From Books Online:

    INSERT INTO jobs (job_desc,min_lvl,max_lvl)
    VALUES ('Accountant',12,125)
    SELECT @@IDENTITY AS 'Identity'


    In stored proc:

    create procedure sp_Test

    @outputvalue int output
    as

    INSERT INTO jobs (job_desc,min_lvl,max_lvl)
    VALUES ('Accountant',12,125)
    SELECT @outputvalue = @@IDENTITY

  3. #3
    Join Date
    Oct 2004
    Posts
    46

    ID for New record

    Thanks DMWCincy.

  4. #4
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    Watch out fot @@IDENTITY, better use SCOPE_IDENTITY() besuase @@IDENTITY will return the last increment for the table, not necessarily the one that was generated by you transaction.
    "The data in a record depends on the Key to the record, the Whole Key, and
    nothing but the Key, so help me Codd."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •