T-SQL相当于oracle sql where子句有多个列

use*_*281 5 sql t-sql sql-server oracle row-value-expression

让我们来看看Oracle SQL,它完美地运行:

样本数据:

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
Run Code Online (Sandbox Code Playgroud)

查询:

SQL> select * from test where (a, b) in (select 1, 4 from dual);

         A          B
---------- ----------
         1          4
Run Code Online (Sandbox Code Playgroud)

这是sql-fiddle:http://www.sqlfiddle.com/#!4/8375e/3 /0

简单的问题:在上面的"where(a,b)"子句的MS SQL中是否有任何等价物?我一直在寻找谷歌,MS Docs,到目前为止没什么...

Luk*_*der 5

虽然SQL Server具有Table Value Constructor可用于某些用例的SQL Server,但SQL Server不支持SQL标准行值表达式和从行值表达式派生的谓词(尚未使用).您将不得不使用等效EXISTS子句来使用半连接子查询:

这个:

select * from test where (a, b) in (select 1, 4 from dual);
Run Code Online (Sandbox Code Playgroud)

相当于此(参见SQLFiddle演示):

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
)
Run Code Online (Sandbox Code Playgroud)

或者,更一般地说,通过使用公用表表达式(请参阅SQLFiddle演示):

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
)
Run Code Online (Sandbox Code Playgroud)


Tec*_*hDo 0

下面的查询怎么样,它在sql server中支持;我猜想a=1 and b=4在 sql server 中给出的结果与 oracle 查询相同:

select 
    * 
from 
    test 
where 
    a=1 and 
    b=4;
Run Code Online (Sandbox Code Playgroud)