Get the Parent Tables' ID values into Child Table

  • Hi Everybody

    I am working on SQL Server 2005.

    I have 3 tables 'PT1', 'PT2' and 'PT3'. These 3 tables have a single Child table 'CT'.

    I need to get the ID values from primary tables into child table.

    PT1 Table:

    ID Name

    1John

    2Smith

    3Rick

    4Mike

    PT2 Table:

    1Scott

    2Riche

    3Ullas

    4Robert

    PT3 Table:

    1Williams

    2Jeff

    3Jesus

    4Lisa

    Then the expected Data for Child Table 'CT':

    ID PTID1 PTID2 PTID3

    1111

    2222

    3333

    4444

    Note: A complete script to create along with data is attached with this topic. Please help me in writing such code.

    Thanks in Advance

    Sy

  • CT should have an identity, right?

    otherwise we'd have to make up a number and insert it.

    If I understand your example you'd join P1, P2, and P3 in the from clause of your insert statement

    it should look something like this:

    insert CT ([PTID1], [PTID2],[PTID3])

    select pt1.id, pt2.id, pt3.id

    from PT1 inner join PT2 on pt1.id=pt2.id

    inner join pt3 on pt3.id = pt1.id

    This is a fairly bizarre example. I hope you're not really sticking first, middle, and last names in 3 different tables and then keying them into a lookup. That seems painfully normalized.

    ~BOT

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

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