use*_*723 3 null stored-procedures sybase except
有很多解释为什么 NOT IN NULL 不起作用,但我没有看到任何解决方案。(最近的问题在这里:为什么 NOT IN 包含 NULL 的集合总是返回 FALSE/NULL?)
我有 6 个存储过程的可选参数,用于过滤查询的某个值。以前的开发人员做了这样的事情:
IF @var1 IS NULL AND .....
select ...
else
select ...
where value in (@var1, ...)
Run Code Online (Sandbox Code Playgroud)
我不是特别喜欢这个。(查询非常庞大),所以我决定选择这样的东西:(
无论如何,我们在临时表中得到了结果)
IF @var IS NOT NULL OR ...
delete from #temp where value not in (@var1,...)
Run Code Online (Sandbox Code Playgroud)
但我意识到这行不通。
我唯一能想到的就是创建另一个临时表,该表将只保存@var1 等中的非空值(使用 if 语句或删除 where 为 NULL),然后对该表进行连接删除。
有没有更好的办法?
不了解 Sybase,但在 SQL Server 中,您可以这样做。
select *
from yourtable
where value not in (select N
from (values (@var1),
(@var2),
(@var3),
(@var4),
(@var5)) T(N)
where N is not null)
Run Code Online (Sandbox Code Playgroud)
如果您不能用于values创建派生表,则可以union all改用。
where value not in (select N
from (select @var1 union all
select @var2 union all
select @var3 union all
select @var4 union all
select @var5) T(N)
where N is not null)
Run Code Online (Sandbox Code Playgroud)