Solved

insert into select

Posted on 2004-10-19
4
16,833 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Email question 19 93
How do you think this website does searches? 5 35
Need definitions 2 19
MS Access Duplicate Data Assistance 9 31
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
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…

751 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