insert into relational tables

  • hello my friends

    i have a database with 5 tables :

    1-Company 2-Activity 3-Product 4-Project 5-Telephone

    each table have a one to many relation with Company Table ! i mean a Company can has many Activities,Products,Projects,Telephones

    i want to insert in tables at a same time insert into Company table ! i know that i should first insert into company Table and get CompanyID that need to insert in other tables.

    i hear that i can do this with define a VIEW of joining these tables and use trigger! I dont know how and what should to do !?

    I'll appreciate you to help me :O)

  • I don't know about using a View and a Trigger but you might look into using a Stored Procedure. In theory it could take all the data and perform the insert/update in the correct order and assigning any generated ID values to subordinate data. I would tend to manage the insert/updates a little more by created several Stored Procedures rather than trying to do it in one "big" procedure.

    --James.

  • Another very nice trick is that you can use the OUTPUT clause of an INSERT operation to get all of the ID's you just created, so it becomes easy to then use that to create your foreign keys for the other tables. You could in fact chain a series of inserts together by capturing the output of one insert into the next insert, etc....

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

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

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