Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

insert data to table from another table

hello,
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.
0
z1357_kh
Asked:
z1357_kh
  • 2
1 Solution
 
SimonLarsenCommented:
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.
0
 
LowfatspreadCommented:
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


hth
0
 
z1357_khAuthor Commented:
please describe more,
table1 has relation with table3 and no relation with table2
fields in table1 and table2 is equal.
please indicate all commands
thanks
0
 
SimonLarsenCommented:
yah so you need in table3, say, the value Elephant before you can put it into table1 from table2.

So:

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.

Then:

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now