Solved

insert into select

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…

762 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