Results 1 to 13 of 13

Thread: [2005] how to insert into 2 tables at once?

  1. #1

    Thread Starter
    Lively Member nudge's Avatar
    Join Date
    Apr 2007
    Location
    There !
    Posts
    111

    Lightbulb [2005] how to insert into 2 tables at once?


    Hi,



    I have two relational tables in my project, and I have to insert data into both of them at one,, or since the first table was filled,, the second should be too ..
    for example I have a book table, and book info table ..
    in the book I would have the ISBN, name .. etc ..
    book info has FK (ISBN) and other info about the book such as:description, category .....

    information about the book are in a text file, I want to extract the information
    and have them into two tables with a FK (ISBN) between them ..


    can I insert into these two tables at once??
    if yes, how ?
    if no , what should I do instead ?


    thanx in advance

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2005] how to insert into 2 tables at once?

    It would probably be easier to read through your file line by line, and add records to the appropriate tables.

  3. #3

    Thread Starter
    Lively Member nudge's Avatar
    Join Date
    Apr 2007
    Location
    There !
    Posts
    111

    Re: [2005] how to insert into 2 tables at once?

    reading and other things are ok .. they are working fine ..

    but the problem is when inserting in two relational tables .. I got an error

  4. #4

    Thread Starter
    Lively Member nudge's Avatar
    Join Date
    Apr 2007
    Location
    There !
    Posts
    111

    Re: [2005] how to insert into 2 tables at once?

    emmmmmmmmmmm,, anyone ?!?!

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [2005] how to insert into 2 tables at once?

    Please do not bump your threads.

    You cannot insert two tables at once... you should insert into each table in turn.

    If you want to ensure that the data is written to both tables (or nothing is written at all), but the Insert statements inside a transaction.

  6. #6

    Thread Starter
    Lively Member nudge's Avatar
    Join Date
    Apr 2007
    Location
    There !
    Posts
    111

    Re: [2005] how to insert into 2 tables at once?

    Quote Originally Posted by si_the_geek
    Please do not bump your threads.

    You cannot insert two tables at once... you should insert into each table in turn.

    If you want to ensure that the data is written to both tables (or nothing is written at all), but the Insert statements inside a transaction.
    ok, sorry ..

    what do you mean by "Insert statements inside a transaction" ..??

    thank you

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [2005] how to insert into 2 tables at once?

    A Transaction is basically a safety net.. normally when you run multiple SQL statements, an error in one of them means you are likely to have done part of the work, but not all of it - which is awkward to either 'undo', or finish off.

    When you use a transaction, it puts all of your SQL statements into a group - and ensures that if one fails, they all 'undo'.


    How you would implement a transaction depends on how you are working with the database, and possibly which database system you are using.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] how to insert into 2 tables at once?

    Pass your variables to a stored procedure. Have your stored procedure insert into both tables within a BEGIN TRANSACTION...COMMIT TRANSACTION block.

    http://msdn2.microsoft.com/en-us/library/ms190295.aspx

  9. #9

    Thread Starter
    Lively Member nudge's Avatar
    Join Date
    Apr 2007
    Location
    There !
    Posts
    111

    Re: [2005] how to insert into 2 tables at once?

    thank you si_the_geek for your help,

  10. #10

    Thread Starter
    Lively Member nudge's Avatar
    Join Date
    Apr 2007
    Location
    There !
    Posts
    111

    Re: [2005] how to insert into 2 tables at once?

    Quote Originally Posted by mendhak
    Pass your variables to a stored procedure. Have your stored procedure insert into both tables within a BEGIN TRANSACTION...COMMIT TRANSACTION block.

    http://msdn2.microsoft.com/en-us/library/ms190295.aspx

    thank you very much .. it really helps!
    thanks for your understanding that am new to these things

  11. #11

    Thread Starter
    Lively Member nudge's Avatar
    Join Date
    Apr 2007
    Location
    There !
    Posts
    111

    Re: [2005] how to insert into 2 tables at once?



    Hi mendhak,

    is it right this way ??

    Code:
    USE Customer;
    GO
    BEGIN TRANSACTION;
    GO
    Insert into customerInfo (id, name, age, gender) values (123, "John", 27, "F");
    GO
    Insert into customerEnroll (customerId, enrolmentDate) values (id, "22-02-2007");
    GO
    COMMIT TRANSACTION;
    GO

  12. #12
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] how to insert into 2 tables at once?

    After your inserts...

    Code:
    		/* Error Handling Routine */
    		SELECT @ErrorNo = @@ERROR
    		IF @ErrorNo <> 0 
                      BEGIN
    			ROLLBACK TRANSACTION
    			GOTO PROC_RETURN
    		END
                    COMMIT TRANSACTION

    At the end,

    Code:
    PROC_RETURN:
    
    RETURN @ErrorFlag

  13. #13
    Addicted Member
    Join Date
    Jul 2005
    Posts
    194

    Re: [2005] how to insert into 2 tables at once?

    not sure I understand all but sum tips;

    1. you can make store procedure in the SQL SERVER, that get the data
    and insert them to the tables.

    2. second option is make a trigger on insert,
    and when you insert to one table it run the trigger and insert the dada to the second table

    3.I think your tow table have to connect by value
    if this value is the auto number you can get it after the insert statement
    with writing "select scope_identity() "

    4. if you want to insert the 2 table and if it fail on running to cancel
    then make TRANSACTION

    hope i help, and sorry on my english

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