Results 1 to 3 of 3
  1. #1
    Join Date
    May 2002
    Location
    Australia
    Posts
    26

    Unanswered: INSERT WITH relationships

    Hi,

    I've got two tables that i wish to insert data into simultaneously. The two tables have a relationship between the id on table1 and the field table1ID on table 2. the id field on table1 is an identity field and i wish to insert that into table2 [for several rows]

    I can insert into table1 but not table2.

    Does this make sense?

    Kind Regards,
    Rim

  2. #2
    Join Date
    Jan 2003
    Location
    Nottinghamshire, UK
    Posts
    364
    Can't do it with one Statement

    Table2 Can't be an Identity field as well

    You will have to insert the Header(Table1) First then Details(Table2) After

    When Inserting into identity field records I believe theres a system variable @@Identity that can identify the newly created id without having to do a re SELECT

    U Can

    Maybe pass the Insert Values into a Stored Proceedure that will complete the 2 steps for you


    BEGIN
    --Insert Header Row
    END
    @MyId = (SELECT @@Identity)
    BEGIN
    --Insert Detail Rows with @MyId
    END

    Type thingy

    Hope this helps

    GW
    "Everything should be made as simple as possible, but not simpler." - Albert Einstein
    "Everything should be made as complex as possible, so I look Cleverer." - Application Developer

  3. #3
    Join Date
    May 2002
    Location
    Australia
    Posts
    26
    Originally posted by GWilliy
    Can't do it with one Statement

    Table2 Can't be an Identity field as well

    You will have to insert the Header(Table1) First then Details(Table2) After

    When Inserting into identity field records I believe theres a system variable @@Identity that can identify the newly created id without having to do a re SELECT

    U Can

    Maybe pass the Insert Values into a Stored Proceedure that will complete the 2 steps for you


    BEGIN
    --Insert Header Row
    END
    @MyId = (SELECT @@Identity)
    BEGIN
    --Insert Detail Rows with @MyId
    END

    Type thingy

    Hope this helps

    GW

    yeah that makes sense. i figured out the @@identity thing. It's good to get someone's opinion too rather that just the help files.

    thank you very much.

Posting Permissions

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