Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

SQL Server:其中条件基于参数

SQL Server:其中条件基于参数,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,我有一个存储过程,如下所示。请注意,这只是一个示例,实际查询很长,有很多列 select *, (select field_1 from table_1 where field1='xx' and disty_name = @disty_name) as field_1, (select field_2 from table_2 where field1='xx' and disty_name = @disty_name) as field_2, from t

我有一个存储过程,如下所示。请注意,这只是一个示例,实际查询很长,有很多列

select 
    *,
    (select field_1 from table_1 where field1='xx' and disty_name = @disty_name) as field_1,
    (select field_2 from table_2 where field1='xx' and disty_name = @disty_name) as field_2,
from 
    table_xx 
where 
    disty_name = @disty_name
@disty\u name
参数将传递一些值并正常工作

我的问题是,如果
@disty\u name
参数包含值“All”,那么忽略
disty\u name=@disty\u name
条件的最佳和最短方法是什么

在某些情况下,我只想删除
disty\u name=@disty\u name
条件,因为用户希望查询所有记录,而不必过滤
disty\u name

一种方法是

(select field_1 from table_1 
 where field1='xx' and (@disty_name = 'All' OR disty_name = @disty_name)) as field_1, 
 etc..
请注意,这可能不会生成最佳计划。如果您有许多这样的表单,那么使用动态SQL在检查“All”的基础上运行一个表单或另一个表单可能是更好的方法。

使用
CASE

select *,
(select field_1 from table_1 where field1='xx' and disty_name = case when @disty_name='All' then disty_name else @disty_name end) as field_1,
(select field_2 from table_2 where field1='xx' and disty_name = case when @disty_name='All' then disty_name else @disty_name end) as field_2,
from table_xx where disty_name = case when @disty_name='All' then disty_name else @disty_name end

另外,我不经常使用它,所以我自己无法为您编写它,但我怀疑有一种更有效的方法可以使用
联合
s和
枢轴
实现这一点,我想我找到了答案

步骤1-使SP中的参数为可选参数

@距离名称ncarchar(40)=空

然后在查询中

select *,
(select field_1 from table_1 where field1='xx' and (@disty is null or dist_name=@disty))  as field_1,
(select field_2 from table_2 where field1='xx' and (@disty is null or dist_name=@disty)) as field_2,
from table_xx where (@disty is null or dist_name=@disty)

如果您传递@disty,它将从查询中过滤disty值。如果参数中有Null,它将解释为“Null为Null”,这是真的。如果我们有一个参数callrd'xyz',它将把它解释为xyz为null,这将返回false。这很酷。。是吗

我想要的是,如果@disty\u name='All',它应该返回所有行。基本上,它不应该过滤并返回all.Thank,但是如果disty_name='all'Thank。。。听起来不错。我应该。我会让你知道我测试后,希望它会工作。不正确的语法附近的“=”。我认为案例返回的是整个1=1或disty_name='value',在where条件下不起作用。更改了逻辑。现在应该没事了。打电话给@bluefeet,因为我知道他喜欢PIVOT;)
select *,
(select field_1 from table_1 where field1='xx' and (@disty is null or dist_name=@disty))  as field_1,
(select field_2 from table_2 where field1='xx' and (@disty is null or dist_name=@disty)) as field_2,
from table_xx where (@disty is null or dist_name=@disty)