Friday, March 9, 2012

error with subquery.....

I am getting an error from a query that that has a subquery.

Msg 512, Level 16, State 1, Line 2

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

(0 row(s) affected)

This is a the query.

select *

from dhcp

where nameofcomputer = (SELECT p.nameofcomputer

FROM v_Pams_DHCP p

Left Outer Join adat2005_main a

ON p.nameofcomputer = a.nameofcomputer

where a.serialnumber is null)

Thanks in advance.

Gene

Use in operator,

Code Snippet

select

*

from

dhcp

where

nameofcomputer

in (

SELECT

p.nameofcomputer

FROM

v_Pams_DHCP p

Left Outer Join adat2005_main a

ON p.nameofcomputer = a.nameofcomputer

where

a.serialnumber is null

)

Exists Might be faster than IN,

Code Snippet

select

*

from

dhcp

where

Exists

(

SELECT

p.nameofcomputer

FROM

v_Pams_DHCP p

Left Outer Join adat2005_main a

ON p.nameofcomputer = a.nameofcomputer

where

a.serialnumber is null and p.nameofcomputer = dhcp.nameofcomputer

)

No comments:

Post a Comment