tricky SP?

  • Probably very simple, but I am still struggling with stored procedures:

    I have 2 tables, and need to insert data into both of them, so far simple enough, but I need to get the id from the one table, and input that into the second table...

    pseudo:

    insert into table1:

    table1.cell1

    table1.cell2

    table1.cell3

    insert into table2:

    table1_id

    table2.cell1

    table2.cell2

    table2.cell3

    makes sense?

    EDIT: I should add that while table1 will only get one entry, I may need to add as many as 5 rowsets to table2, all with the same id from table1

    -

    Signature? I don't need no stinking signature!

    Edited by - dna on 05/27/2002 05:20:04 AM


    -
    Signature? I don't need no stinking signature!

  • declare @pk int

    insert into table1:

    table1.cell1

    ,table1.cell2

    ,table1.cell3

    select @pk = @@identity

    insert into table2:

    @pk

    ,table2.cell1

    ,table2.cell2

    ,table2.cell3

    Regards,

    Andy Jones

    andyj93@hotmail.com

    .

  • If you are using SQL 2K I suggest use SCOPE_IDENTITY() instead. However, if this is based on every insert transaction I would consider using a trigger so you can catch the details from the inserted table and get all information even if someone bypasses you SP.

    "Don't roll your eyes at me. I will tape them in place." (Teacher on Boston Public)

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

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