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
seeking clarification.

cheers,John Smith (genericemailaccount@.genericdomain.genericTLD) writes:
> 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
> seeking clarification.

That should be OK. A bit unusual maybe, but certainly valid.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||>> I was told it wasn't valid, but I'm pretty sure it worked for me. <<

It is valid, Standad SQL and can be a useful trick to avoid OR-ed
predicates. The IN() list just has to be expressions that will cast
to the proper data type.

No comments:

Post a Comment