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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Can not see what is wrong with statement

Author  Topic 

csphard
Posting Yak Master

113 Posts

Posted - 2009-01-06 : 19:33:54
what is wrong with this statement
select empid from test
where empid <> (select empid from ptest)

I get the error message
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

I can not see what is wrong.

Howard

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-06 : 19:41:49
You can use NOT IN or NOT EXISTS instead. You can't use = or <> since your subquery returns more than one row.

SELECT empid
FROM test
WHERE empid NOT IN (SELECT empid FROM ptest)

SELECT empid
FROM test t
WHERE NOT EXISTS (SELECT * FROM ptest p WHERE t.empid = p.empid)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

DeveloperIQ
Yak Posting Veteran

71 Posts

Posted - 2009-01-06 : 19:44:15
Use a "NOT IN" instead of <>
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2009-01-06 : 19:44:38
this is because on the left of the inequality, you have one value, namely, empid, but the query on the right side of the inequality returns multiple values. T-SQL is unable to compare a single value against the multiple values.


What you probably want is something like this:

select empid from test t where not exists (select * from ptest p where p.empid = t.empid)
Go to Top of Page

csphard
Posting Yak Master

113 Posts

Posted - 2009-01-06 : 20:25:48
Thank you for your help.

Howard
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-01-06 : 22:47:56
SubQuery Returns Only one row, If u have more than one row in the subquery result then error occurs in the statement.
Tkizer suggested the correct one for ur requirment.
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2009-01-06 : 23:20:54
Use NOT EXISTS Rather than NOT IN For better Performance

Jai Krishna
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-07 : 02:19:48
You're welcome. Let us know if you need any other help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -