SQL SERVER 不支持多字段的IN 和 NOT IN


SQL SERVER 不支持多字段的IN 和 NOT IN    但是ORACLE是支持的。


表a  有字段:a, b, c還可能有其他字段。

表b  有字段:a,b,c 還可能有其他字段。

create table a (
a varchar(100),
b varchar(100),
c varchar(100)
)
create table b (
a varchar(100),
b varchar(100),
c varchar(100)
)


insert a values ('1','a','甲');
insert a values ('2','b','乙');
insert a values ('3','c','丙');


insert b values ('1','a','甲');
insert b values ('2','b','乙');

insert b values ('3','c','丙');


執行該語句:select * from a where (a,b) IN (select a,b from b);

結果:

消息 102,級別 15,狀態 1,第 2 行
',' 附近有語法錯誤。



語句改成 exists :

select * from a
where exists (select 1 from b
where a.a=b.a and a.b = b.b
 );


執行結果:



所以:在sqlserver 中要使用多字段的in 或者是 not in  應該改為 exists 或者 not  exists 語句。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM