Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何解决sql(case when),(in)_Sql Server_Tsql - Fatal编程技术网

Sql server 如何解决sql(case when),(in)

Sql server 如何解决sql(case when),(in),sql-server,tsql,Sql Server,Tsql,这是错误的 “,”附近的语法不正确 当我选择使用case语句时,请帮助我谢谢 if语句: declare @module nvarchar(max)='tenant' if (@module='tenancy') select 'All' UNION select distinct affect_table from sys_log where affect_table in ('contract','rent_free','dn_override') select 'All' UNION

这是错误的

“,”附近的语法不正确

当我选择使用case语句时,请帮助我谢谢

if语句:

declare @module nvarchar(max)='tenant'
if (@module='tenancy')
select 'All'
UNION 
select distinct affect_table from sys_log where affect_table in ('contract','rent_free','dn_override')
select 'All'
UNION 
select distinct affect_table from sys_log where affect_table in (case when @module='tenant' THEN ('contract','rent_free','dn_override'))
语句时的大小写:

declare @module nvarchar(max)='tenant'
if (@module='tenancy')
select 'All'
UNION 
select distinct affect_table from sys_log where affect_table in ('contract','rent_free','dn_override')
select 'All'
UNION 
select distinct affect_table from sys_log where affect_table in (case when @module='tenant' THEN ('contract','rent_free','dn_override'))

看起来您不需要
案例
,只需要简单的逻辑:

select distinct affect_table from sys_log
where
   (affect_table in ('contract','rent_free','dn_override')
    and
    @module = 'tenant')
至于为什么您的尝试不起作用,那是因为
CASE
是一个表达式,它必须返回一个标量值。不是一组值。也没有一份清单。或者你决定的任何其他东西('contract'、'rent\u free'、'dn\u override'),就其本身而言,就是一个例子