Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2002
    Posts
    13

    Question Unanswered: SQL to Select Inserted Records

    There's probably a simple answer to this that I've just never had occassion to do it before. I need to write a statement (or statements) that insert records and then returns those records. I need to return the system generated ID for these records, which I obviously can't know before they're inserted. Anyone know an easy way to do this?

  2. #2
    Join Date
    Mar 2001
    Location
    Lexington, KY
    Posts
    606
    Well, if you are using an IDENTITY column it is as easy as:
    SELECT @@identity

    e.g.
    begin transaction
    insert into bob values( ... )
    select @@identity
    commit transaction
    Thanks,

    Matt

  3. #3
    Join Date
    Nov 2002
    Posts
    13
    Originally posted by MattR
    Well, if you are using an IDENTITY column it is as easy as:
    SELECT @@identity

    e.g.
    begin transaction
    insert into bob values( ... )
    select @@identity
    commit transaction
    Not sure if I can go that way just because I'm sending the SQL from a client application, but I might be able to move this piece of it to a trigger instead, always a better idea. Thanks for the input!

Posting Permissions

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