Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Old Forums
 CLOSED - General SQL Server
 Subqueries are not allowed in this context

Author  Topic 

michael1
Starting Member

28 Posts

Posted - 2004-06-21 : 09:10:18
I have 3 tables
I need to enter primary keys of the 2 tables into the third table as follows

Insert into table3 (fld1, fld2) Values ((Select table1PK from table1 where fld3 ='text1'),
(Select table2PK from table2 where fld4 ='text2'))

I get the following error
"Subqueries are not allowed in this context. Only scalar expressions are allowed."
Is there a solution to this problem?

Alternatively, I can store the output of the two select statement in variables and then use the variables to insert records into the third table.
But the problem is I am writing a sql script. The third table is basically a relation table between the first two tables.
So, I don't want to define varibles for each sql statement. Do we have a better solution?
Thanks in Advance
Michael

nr
SQLTeam MVY

12543 Posts

Posted - 2004-06-21 : 09:16:21
Insert into table3 (fld1, fld2) select (Select table1PK from table1 where fld3 ='text1'),
(Select table2PK from table2 where fld4 ='text2')

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

michael1
Starting Member

28 Posts

Posted - 2004-06-22 : 08:00:36
Thanks nr for the reply
Go to Top of Page
   

- Advertisement -