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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert - Subquery returns more then 1 row

Status
Not open for further replies.

wysiwygGER01

Programmer
Feb 18, 2009
188
AU
Hi,

I'm trying to get this SQL statement to work:
Code:
INSERT INTO lnkroletasks(taskid, roleid)VALUES('108', (SELECT id FROM tblroles WHERE role = 'Customer Service Manager' OR role='Engineering Manager'))

I think the error shows up because the first value to insert('108') is a static value. The second value to insert is the result of a select query.

I want to use '108' for all rows which are returned by the sub-select.
Is this something that can be done?
 
Are you getting an error in which case what is it?
is taskid an integer in which casde get rid of the quotes
what happens if you run
Code:
SELECT id FROM tblroles WHERE role = 'Customer Service Manager' OR role='Engineering Manager'
do you get more than 1 row returned ?
 
Thanks for your quick reply.

The error code is 1242 - Subquery returns more then 1 row.
Which is correct.

This code returns more then 1 row.
Code:
SELECT id FROM tblroles WHERE role = 'Customer Service Manager' OR role='Engineering Manager'

My questions is whether there is a way of telling MySQL to use the taskid 108 for all rows returned from the subquery?

Tried with ANY or ALL for the subquery as well but this didn't work either.
 
Code:
INSERT 
  INTO lnkroletasks
     ( taskid
     , roleid )
SELECT 108
     , id 
  FROM tblroles 
 WHERE role = 'Customer Service Manager' 
    OR role = 'Engineering Manager'
:)

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top