Join tables and INSERT

  • I have 3 tables which have IDENTITY columns as PK and I have a 4th table where PK is the combination of Identity columns from 1st 3 tables.

    How can I join these 4 tables such that when I insert data into the 4th table the PK column shud update with the combination of Identity columns from those 3 tables.

    Pls help how can I do Insertion?

  • What is the relationship between the first three tables?  Insertion is performed through use of the INSERT statement, believe it or not

    Here's an example:

    INSERT INTO table3(ID)

    select table1.ID + table2.id from table1 join table2 on table1.id = table2.id

    This inserts composite records from tables 1 and 2 into table3, but as you will see, depends on a clear relationship between tables 1 and 2.

    It might help if you provide some sample data.

    Regards


  • The relationship between 1st 3 tables is in this way

    say table1=offices, table2=Depts, table3=sections.

    Here offices has many Depts, Depts has many sections.

  • Insert into dbo.Table4 (OfficeID, DeptID, SectionID)

    values (2234,654,23426)

    (actually use parameters passed to a proc to do the insert).

  • So does

    select o.OfficeID, d.DeptID, s.SectionID

    from offices o join depts d on o.OfficeID = d.OfficeID join Sections s on d.DeptID = s.DeptID

    Give you the results you want to insert to table4?


  • He didn't tell if he wanted to load the table from scratch or on a needed basis for further input... we'll have to wait and see .

Viewing 6 posts - 1 through 6 (of 6 total)

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