Let's look for example Oracle SQL, which works perfectly:
Sample data:
SQL> create table test (a number, b number);
SQL> insert into test values(1, 1);
SQL> insert into test values(1, 2);
SQL> insert into test values(1, 3);
SQL> insert into test values(1, 4);
SQL> insert into test values(1, 5);
SQL> insert into test values(2, 1);
SQL> insert into test values(2, 2);
SQL> insert into test values(2, 3);
SQL> insert into test values(2, 4);
SQL> insert into test values(2, 5);
SQL> insert into test values(4, 1);
SQL> select * from test;
A B
---------- ----------
1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 3
2 4
2 5
4 1
Query:
SQL> select * from test where (a, b) in (select 1, 4 from dual);
A B
---------- ----------
1 4
Here's the sql-fiddle: http://www.sqlfiddle.com/#!4/8375e/3/0
Simple question: is there any equivalent in MS SQL of above "where (a, b)" clause? I've been looking around in google, MS Docs and nothing so far...
While SQL Server has a Table Value Constructor
that can be used for some use-cases, SQL Server doesn't support SQL standard row value expressions and predicates derived from row value expressions for general use (yet). You will have to resort to semi-joining your subquery using an equivalent EXISTS
clause:
This:
select * from test where (a, b) in (select 1, 4 from dual);
Is equivalent to this (see SQLFiddle demo):
select * from test where exists (
select * from (
select 1, 4 -- Replace with "real" subselect
) t(a, b)
where test.a = t.a and test.b = t.b
)
Or, a bit more generically, by using a common table expression (See SQLFiddle demo):
with t(a, b) as (
select 1, 4 -- Replace with "real" subselect
)
select * from test where exists (
select * from t
where test.a = t.a and test.b = t.b
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With