1

I am getting an error when running an UPDATE statement:

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS

when I attempt to execute this SQL query:

Update tblCommunityServiceMembers  
Set SeniorManagerFlag = 'Y'   
Where EmployeeList =   
         (select distinct emp.EmployeeID, csm.CommunityServiceMembers,  
          csm.SeniorManagerFlag from dbo.tblEmployee emp,   
          dbo.CommunityServiceMembers csm  
          where charindex(emp.EmployeeID, > csm.CommunityServiceMembers,1) > 0)

The nested select query does return more than one record. So I want to update the flag value to Y for all those records. Any help is appreciated.

1
  • 1. What data type is EmployeeList? 2. What does > mean in charindex(emp.EmployeeID, > csm.CommunityServiceMembers,1)? I first thought it was a typing error, but then I'm not pretending to know all about CHARINDEX usage. 3. And just to clarify it, what type is emp.EmployeeID if it's not int? Commented Feb 4, 2011 at 7:01

3 Answers 3

2

If you want to update the flag for all records for which the inner query is true, then you can try following query. But still I dont understand the use of second parameter in CHARINDEX function. may be you need to correct it first.

Update tblCommunityServiceMembers
Set SeniorManagerFlag = 'Y'
Where exists (select distinct emp.EmployeeID, csm.CommunityServiceMembers,
csm.SeniorManagerFlag from dbo.tblEmployee emp,
dbo.CommunityServiceMembers csm
where charindex(emp.EmployeeID, > csm.CommunityServiceMembers,1) > 0)
Sign up to request clarification or add additional context in comments.

Comments

1

The subquery is returning 3 fields. (employeeid, communityservicemembers, seniormanagementflag). You're returning it into an equality test, and SQL Server isn't telepathic enough to know which field you want to compare the EmployeeList against.

4 Comments

well, I was intending to "update the seniormanagerflag for all records returned". What would be the best way to do this?
return only the employeeIDs from the subquery. That's all your really need.for the outer query to work on.
Just curious, would the join work (in the nested select) if I only returned the employeeID ?
Sure, joins don't require you to "select" the fields you're joining on. You can select field A, join on B&C, and never ever see the contents of B&C returned.
0

An "equal to" matches the specified value with the selected value. Here as it appears, you want to just check that if the sub-query returns any value you need to update the table. So there's no point in checking for the equal values you just need an exists check for the same and also your select return 3 results where as there's a single column to check for,which will definitely throw an error.

Hope this might help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.