Showing posts with label howdy. Show all posts
Showing posts with label howdy. Show all posts

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
>

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.