Solved

insert into select

Posted on 2004-10-19
4
16,804 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql to convert to date IF entry is in date format 4 74
Determine next b-weekly date 12 79
Update in Sql 7 30
Update from TABLE-A to TABLE-B 5 34
Introduction: I have seen many questions on EE and elsewhere, asking about how to find either gaps in lists of numbers (id field, usually) ranges of values or dates overlapping date ranges combined date ranges I thought it would be a good …
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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…

943 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

6 Experts available now in Live!

Get 1:1 Help Now