CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2003
    Location
    Ottawa
    Posts
    124

    Insert and Select in 1 statement - help

    Hi,

    I have 2 tables that have a relationship based on a primary id key which is auto generated.

    In my application I insert a new row in the first table and the number is auto generated, but when I want to insert the related data in the second table I will need the value of the generated number from the first table.

    Is there a way that the insert statement against the first table returns a columns of the new row that represent the ID generated so I wouldn't have to do a select on all the columns to retreive this number.?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Insert and Select in 1 statement - help

    Which DB are you using?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766

    Re: Insert and Select in 1 statement - help

    You may be able to use the @@Identity function?
    JP

    Please remember to rate all postings.

  4. #4
    Join Date
    Jan 2002
    Posts
    195

    Re: Insert and Select in 1 statement - help

    If your using access be sure that the

    Enforce Referential Integrity
    Cascade Update Related Rields

    checkboxes are selected in your releationship.

    Also if you want both to be deleted when one or the other is deleted then make sure the

    Cascade Delete Related Records

    checkbox is selected.

    Hope this helps

  5. #5
    Join Date
    Apr 2007
    Posts
    10

    Re: Insert and Select in 1 statement - help

    u can use
    declare @id int
    insert .............

    select @id=@@identity
    u will get the auto generated value in table 1

  6. #6
    Join Date
    May 1999
    Location
    New Delhi, India
    Posts
    359

    Re: Insert and Select in 1 statement - help

    guys!!
    @@identity is not going to solve the purpose as it is not session specific... to get teh correct valye go with scope_Identity().

    Hope this helps!!
    Whenever I hear "It can't be done", I know, I am close to success!!

  7. #7
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Insert and Select in 1 statement - help

    Quote Originally Posted by pranay
    guys!!
    @@identity is not going to solve the purpose as it is not session specific... to get teh correct valye go with scope_Identity().
    @@identity gets the value on the session level.
    Check MSDN:
    Quote Originally Posted by msdn
    @@IDENTITY and SCOPE_IDENTITY return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; @@IDENTITY is not limited to a specific scope.

    IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Insert and Select in 1 statement - help

    Gah.. all you guys went off on giving SQL Server advice without it ever being confirmed that the OP was actually using SQL Server.. Let's hope he was!
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  9. #9
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Insert and Select in 1 statement - help

    Quote Originally Posted by cjard
    Gah.. all you guys went off on giving SQL Server advice without it ever being confirmed that the OP was actually using SQL Server.. Let's hope he was!
    He did not confirm the DBMS he uses, anyway, the thread turned to discuss the replies more than the question
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured