Solved

insert into select

Posted on 2004-10-19
4
16,827 Views
Last Modified: 2012-08-13
can i have statement like this:

insert into table2
....
..
select (... ,.., ,  from table1, table3 where table1.id = table3.id)

Thanks a lot for your help
0
Comment
Question by:Vasi04
4 Comments
 
LVL 12

Assisted Solution

by:catchmeifuwant
catchmeifuwant earned 50 total points
ID: 12355053
Yes you can have it...

1)If both the table structures are same - column positions and data type then do :

insert into table2
select a.* from
table1 a, table3 b
where a.id = b.id

This will insert all rows from table1 (matching with id of table 2) into table3

2)If you want selected combination of data from both tables then do:

insert into table2(col1,col2,col3)
select a.col1, b.col2, a.col3 from
table1 a, table3 b
where a.id = b.id
0
 
LVL 16

Assisted Solution

by:GreymanMSC
GreymanMSC earned 50 total points
ID: 12355090
No.  Well, almost.  Use joins for greater efficiency.

  insert into table2(col1,col2,col3)
  select a.col1, b.col2, a.col3
  from table1 as a inner join table3 as b on a.id = b.id
0
 
LVL 19

Accepted Solution

by:
NickUpson earned 100 total points
ID: 12355348
insert into table2 (field1, field2) values
  (select fielda, fieldb from table1, table3 where table1.id = table3.id)

is ok, the exact syntax can depend upon the database you are using, just make sure the
fields match in number, type, etc
0
 

Assisted Solution

by:a_tya
a_tya earned 50 total points
ID: 12355598
You can use this if tables have the same structure:

insert into table2
select a.*
 from table1 a
INNER JOIN table3 b on  a.id = b.id


or use :
select a.*
INTO table2
 from table1 a
INNER JOIN table3 b on  a.id = b.id

IF table2 doesn't exist and you want to copy both data and table design from Table1.

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

808 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