Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INSERT statement includig sub-query 1

Status
Not open for further replies.

matpj

Technical User
Mar 28, 2001
687
GB
hi, I have gthe following query:

select proj_id
from msp_text_fields
group by proj_id;

this gives me a list of all of the proj_ids in the table.

I want to update a second table with all of these values.
The second table has the same column (proj_id) but has other columns aswell.
I have tried:
insert into msp_synchtable (proj_id) VALUES
(select proj_id
from msp_text_fields
group by proj_id)

but this doesn't work.
can anyone tell me where I am going wrong?
I get an error in line two saying missing expression.
 
If you are using a Sub Query, don't use values...

eg.

INSERT INTO Table1 (SELECT Values FROM Table2)

If the number of fields in Table1 differs from the SELECT you will need to add defaults.

eg. (if Table1 had four fields but you're only selecting the first from Table2 and you wanted the others to be 0 for example).

INSERT INTO Table1 (SELECT Field1, 0, 0, 0 FROM Table2)

Hope this makes sense....?

There are two ways to write error-free programs; only the third one works.
 
Sorry, you don't need the brackets for an INSERT around the SELECT

There are two ways to write error-free programs; only the third one works.
 
thanks for your help!
this works fine!
 
Glad it helped.
[2thumbsup]


There are two ways to write error-free programs; only the third one works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top