且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

查询:查找不属于值列表的行

更新时间:2023-11-14 18:43:16

我认为问题在于您试图从语句中找到值.您需要做的是将您的 in 语句变成表格,然后您可以确定哪些值不同.

The problem I believe is that your trying to find values from you in statement. What you need to do is turn your in statement into a table and then you can determine which values are different.

create table #temp
(
value int
)

insert into #temp values 1
insert into #temp values 2
insert into #temp values 3
insert into #temp values 4

select
 id
from
 #temp
where
 not exists (select 1 from Tab where Col = id)

更好的替代方法是创建一个表值函数,将逗号分隔的字符串转换为表.我手头没有任何代码,但在 Google 上应该很容易找到.在这种情况下,您只需使用以下语法.

A better alternative would be to create a table-valued function to turn your comma-delimited string into a table. I don't have any code handy, but it should be easy to find on Google. In that case you would only need to use the syntax below.

select
 id
from
 dbo.SplitStringToTable('2,3,6,7')
where
 not exists (select 1 from Tab where Col = id)

希望能帮到你