Friday, March 23, 2012

Literal value with "IN" clause

Howdy,
Is it okay to use a literal value with the IN clause. E.g.
SELECT somefield, anotherfield
...
WHERE ...etc.
AND 1234 IN (SELECT userid FROM tblUsers)
I was told it wasn't valid, but I'm pretty sure it worked for me. Just
sing clarification.
cheersYes, that is valid, but it seem to be a pretty meaningless operation to me.
The IN predicate will
not be dependent on the outer query, so it you get at least one match, then
the predicate is true
for all rows in the outer table:
USE pubs
SELECT * FROM authors WHERE 'White' IN (SELECT au_lname FROM authors)
SELECT * FROM authors WHERE 'GWhite' IN (SELECT au_lname FROM authors)
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"John Smith" <genericemailaccount@.genericdomain.genericTLD> wrote in message
news:443cd3b9$0$7532$afc38c87@.news.optusnet.com.au...
> Howdy,
> Is it okay to use a literal value with the IN clause. E.g.
> SELECT somefield, anotherfield
> ...
> WHERE ...etc.
> AND 1234 IN (SELECT userid FROM tblUsers)
> I was told it wasn't valid, but I'm pretty sure it worked for me. Just
> sing clarification.
> cheers
>|||Yes, it's ok.
Or you could use:
WHERE EXISTS(SELECT * FROM tblUsers WHERE userid = 1234)
BG, SQL Server MVP
www.SolidQualityLearning.com
www.insidetsql.com
Anything written in this message represents my view, my own view, and
nothing but my view (WITH SCHEMABINDING), so help me my T-SQL code.
"John Smith" <genericemailaccount@.genericdomain.genericTLD> wrote in message
news:443cd3b9$0$7532$afc38c87@.news.optusnet.com.au...
> Howdy,
> Is it okay to use a literal value with the IN clause. E.g.
> SELECT somefield, anotherfield
> ...
> WHERE ...etc.
> AND 1234 IN (SELECT userid FROM tblUsers)
> I was told it wasn't valid, but I'm pretty sure it worked for me. Just
> sing clarification.
> cheers
>

No comments:

Post a Comment