all groups > sql server programming > june 2007 >
You're in the

sql server programming

group:

WHERE / IN Syntax


Re: WHERE / IN Syntax Kalen Delaney
6/29/2007 4:32:52 PM
sql server programming:
Hi Rene

The query with IN allows for the subquery to return more than one row.
The query with = will fail if the subquery returns more than one row.

--
HTH
Kalen Delaney, SQL Server MVP
www.InsideSQLServer.com
http://sqlblog.com


[quoted text, click to view]

WHERE / IN Syntax Rene
6/29/2007 6:18:19 PM
Below are two update queries. One of the queries uses the *IN* keyword to
figure out what to update and the other one does not:

--------------------------------------------------

UPDATE SomeTable SET SomeColumn = 'abc'
WHERE AnotherColumn = (SELECT XColumn FROM XTable)


UPDATE SomeTable SET SomeColumn = 'abc'
WHERE AnotherColumn IN (SELECT XColumn FROM XTable)

--------------------------------------------------


According to me, the syntax of both queries are correct and both should do
identical work, could someone set me straight about this? If both queries
syntax are correct, is there a benefit for using the IN keyword?

Thank you.

Re: WHERE / IN Syntax Vt
6/30/2007 12:00:00 AM
Hi

see this link

http://www.sql-server-performance.com/transact_sql.asp

Vt
Knowledge is power;Share it
http://oneplace4sql.blogspot.com

[quoted text, click to view]

Re: WHERE / IN Syntax Dan Guzman
6/30/2007 12:00:00 AM
[quoted text, click to view]

To add to Kalen's response, an alternative to IN is EXISTS as shown in the
example below. I always use NOT EXISTS instead of NOT IN for nullable
columns to avoid the trap that no rows are when the NOT IN returns one or
more NULL values.

UPDATE dbo.SomeTable
SET SomeColumn = 'abc'
WHERE EXISTS
(
SELECT *
FROM dbo.XTable
WHERE SomeTable.AnotherColumn = XTable.XColumn
)

For your equality subquery, you could instead use a JOIN with the
proprietary UPDATE...FROM syntax:

UPDATE dbo.SomeTable
SET SomeColumn = 'abc'
FROM dbo.SomeTable
JOIN XTable ON
SomeTable.AnotherColumn = XTable.XColumn

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
AddThis Social Bookmark Button