Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WHERE IN (SELECT) with multiple columns

3 views
Skip to first unread message

Peter

unread,
Nov 12, 2009, 10:35:55 PM11/12/09
to
A continuing annoyance is that I can do stuff like this in SQL Server:

SELECT * FROM X WHERE A,B IN (SELECT A,B FROM Y)

This is getting more complex if any of A or B can be NULL. Is there an
alternative in SQLServer 2005+ to implement such predicates?

Pete


sloan

unread,
Nov 12, 2009, 10:49:41 PM11/12/09
to

Select * from dbo.Employee e where exists ( select null from
dbo.SomeOtherTable sot where sot.StateID = e.StateID and sot.EmployeeID =
e.EmployeeID )

You can start there and then experiment.

Throw in a " or e.StateID IS NULL "... or something like that.

You should post some DDL , some INSERTS and then desired results
.................


"Peter" <peteATkapiti.co.nz> wrote in message
news:eJSgqJBZ...@TK2MSFTNGP06.phx.gbl...

--CELKO--

unread,
Nov 13, 2009, 12:17:33 PM11/13/09
to
You have discovered Standard SQL syntax, but missed the row
constructor notation:

SELECT * FROM X WHERE (a,b) IN (SELECT a, b FROM Y);

SQL Server is behind other products. You wind up using

SELECT *
FROM X
WHERE EXISTS
(SELECT *
FROM Y
WHERE X.a = Y.a
AND X.b = Y.b);

Peter

unread,
Nov 13, 2009, 4:13:23 PM11/13/09
to
Yes you're right, I (accidentially) did missed the row constructor syntax in
my original post.

Still, this "Standard SQL Syntax" works fine in Oracle and MySQL but NOT SQL
Server 2005. The SELECT in the following SQL will give you a syntax error.

CREATE TABLE A (x int, y int )
CREATE TABLE B (x int, y int )
INSERT INTO A (x,y) VALUES (1,1)
INSERT INTO A (x,y) VALUES (1,3)
INSERT INTO A (x,y) VALUES (2,1)
INSERT INTO A (x,y) VALUES (2,3)
INSERT INTO B (x,y) VALUES (1,1)
INSERT INTO B (x,y) VALUES (1,2)
INSERT INTO B (x,y) VALUES (1,3)
INSERT INTO B (x,y) VALUES (2,1)
INSERT INTO B (x,y) VALUES (2,2)
INSERT INTO B (x,y) VALUES (2,3)
SELECT * FROM B WHERE (x,y) IN (SELECT x,y FROM A)
DROP TABLE B
DROP TABLE A


"--CELKO--" <jcel...@earthlink.net> wrote in message
news:a96e328b-ed78-4650...@o10g2000yqa.googlegroups.com...

Plamen Ratchev

unread,
Nov 13, 2009, 4:57:53 PM11/13/09
to
Yes, vector expressions in predicates are still not supported in SQL Server. The current method using the EXISTS
predicate can be very verbose especially when NULLs are involved. Hope we see something in the next versions.

--
Plamen Ratchev
http://www.SQLStudio.com

--CELKO--

unread,
Nov 13, 2009, 9:38:18 PM11/13/09
to
>> Hope we see something in the next versions. <<

Me, too! And I hope to help with it.

Message has been deleted
0 new messages