Table insertion

  • Hi All,

    I Have 3 Tables where each table needs to get inserted with 100 Records.1st Table primary key is referenced by 2nd and the 3rd Tables as foreign key.

    So I can insert one row in the 1st table and get the primary key of the inserted row and insert the second and the third table. I am repeating this process for 100 times .

    Is there any better way to do it .

  • I think thats the only way, because you are dealing with individual rows. I assume you are using cursors in your repetition.


    Everything you can imagine is real.

  • Hi

    Thats the only way to do it.

    But if you are using cursor or looping to insert, consider using the number table or the tally table.

  • No cursors needed ;-). Where do your 100 rows and the related data come from?

    If you have any surrogate key within your data (e.g. a client side id, a row number, ...) you can use an ORDERed INSERT into your table1. After this you can calculate all generated IDs by SCOPE_IDENTITY() and @@ROWCOUNT.

    Those IDs can be used for your other tables.

    Greets

    Flo

Viewing 4 posts - 1 through 3 (of 3 total)

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