insert data to table from another table

Posted on 2004-11-20
Last Modified: 2012-11-14
how can i insert data to table1 from table2?
table1 have relation with another tables and have primmary and foreign key ,so i have problem,
please help me.
Question by:z1357_kh
    LVL 8

    Expert Comment

    For each of your foreign keys you'll need to do:

    insert into LookupTable
    Select distinct (ForignKey) from Table2

    then do your insert from table2.
    LVL 50

    Expert Comment

    as simon has indicated
    you first need to insert the parent rows to the related tables...

    and then you can do the insert into Table1 from table2

    is this a one-off or a normal requirement ?

    if its going to be part of your standard requirements and all the data is present
    you may like to investigate BEFORE or INSTEAD OF TRIGGERS  
    on the tables or a new view


    Author Comment

    please describe more,
    table1 has relation with table3 and no relation with table2
    fields in table1 and table2 is equal.
    please indicate all commands
    LVL 8

    Accepted Solution

    yah so you need in table3, say, the value Elephant before you can put it into table1 from table2.


    Insert into table3 (ColumnName)
    select distinct (t2.foreignKeyColumn)
    from table2 t2 left join
          table3 t3
    on t2.ForignKeyColumn = t3.ColumnName
    where t3.ColumnName is null

    do this for each relationship.


    Insert into table1 (Col1, Col2, Col3)
    select Col1, Col2, Col3 from table2

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    In this article—a derivative of my blog post (—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
    Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now