简体   繁体   English

如何在 SQL 服务器中创建返回多个值的条件 WHERE 子句?

[英]How can I create a conditional WHERE clause in SQL Server that returns multiple values?

I have the following SQL code for a SSRS report.我有以下 SQL 代码用于 SSRS 报告。 I simplified the code because the original query is much longer.我简化了代码,因为原始查询要长得多。 There is a parameter @ARTICLE which a user can input.有一个用户可以输入的参数@ARTICLE。 What I want to do is create a conditional WHERE statement.我想要做的是创建一个条件 WHERE 语句。 If a user enters an article number (@ARTICLE) the query should filter ID's from Table1 that match with ID's for which the entered article number (@ARTICLE) have a match with a 'detailcode' from another table.如果用户输入文章编号 (@ARTICLE),则查询应过滤 Table1 中与输入的文章编号 (@ARTICLE) 与另一个表中的“详细代码”匹配的 ID 匹配的 ID。 If there is no article number given, do not filter (or skip the whole WHERE statement)如果没有给出文章编号,则不要过滤(或跳过整个 WHERE 语句)

With the code below I get the following error:使用下面的代码,我收到以下错误:

Subquery returned more than 1 value.子查询返回超过 1 个值。 This is not permitted when the subquery follows =, ,=, <, <=, >.当子查询跟随 =, ,=, <, <=, > 时,这是不允许的。 >= or when the subquery is used as an expression.' >= 或当子查询用作表达式时。

Logically it works perfectly fine without the CASE statement, so when only the subquery is used to check for matching ID's.逻辑上它在没有 CASE 语句的情况下工作得很好,所以当只使用子查询来检查匹配的 ID 时。 However, I only want to return matching IDs if the @ARTICLE parameter has a value.但是,如果 @ARTICLE 参数有值,我只想返回匹配的 ID。 If it is NULL or an empty string I want to return all IDs (or just skip the entire WHERE statement).如果是 NULL 或空字符串,我想返回所有 ID(或跳过整个 WHERE 语句)。 How can I include a condition in the WHERE clause that allows multiple rows to return given the example below?如何在 WHERE 子句中包含允许返回多行的条件,给出以下示例?

I feel like my approach is way to complicated, any help is much appreciated!我觉得我的方法很复杂,非常感谢任何帮助!

DECLARE @ARTICLE AS VARCHAR(50) = '1234567'

SELECT * FROM Table1
    
WHERE 
Table1.ID IN (

CASE
    WHEN ISNULL(@ARTICLE,'')<>'' THEN
    (
    SELECT  ID
    FROM    Table2
    WHERE   detailcode IN (@ARTICLE)
    )
    ELSE Table1.ID
    END
    )

You're right, you're overcomplicating it a bit - if you look at the LIKE operator you can do something like:你是对的,你有点过于复杂了——如果你看一下LIKE运算符,你可以这样做:

DECLARE @filter NVARCHAR(50) = '123456';

DECLARE @f NVARCHAR(100) = '%' + @filter + '%';

SELECT * 
FROM [Table1] AS [t1]
     INNER JOIN [Table2] AS [t2]
                ON [t2].[joinField] = [t1].[joinField]
                   AND [t2].[detailCode] LIKE @f;

Where @filter is a parameter to the stored procedure.其中@filter是存储过程的参数。

Or to account for detailCode being null:或者说明detailCode为 null:

DECLARE @filter NVARCHAR(50) = '123456';

DECLARE @f NVARCHAR(100) = '%' + @filter + '%';

IF @filter != NULL    
     SELECT * 
     FROM [Table1] AS [t1]
          INNER JOIN [Table2] AS [t2]
                     ON [t2].[joinField] = [t1].[joinField]
                        AND [t2].[detailCode] LIKE @f;
 ELSE
     SELECT * 
     FROM [Table1] AS [t1]
          INNER JOIN [Table2] AS [t2]
                     ON [t2].[joinField] = [t1].[joinField];

I would check wether @ARTICLE is NULL or if it is NOT NULL and your subquery is fulfilled, like so:我会检查@ARTICLENULL还是NOT NULL并且您的子查询已完成,如下所示:

WHERE
    ISNULL(@ARTICLE, '') = ''
    OR
    (
        ISNULL(@ARTICLE, '') <> ''
        AND ID IN
            (
                SELECT ID FROM Table2
                    WHERE detailcode = @ARTICLE
            )
    )

Maybe you can do it entire different, with an exists for example.也许你可以做完全不同的事情,例如存在。
So you return all rows when @ARTICLE is null or '' OR exists at least one row in table2 with this article因此,当@ARTICLE is null or ''exists at least one row in table2 with this article一行时,您将返回所有行
The OR will have the effect that no filtering is done when the variable is null or ''当变量为 null 或 '' 时, OR将产生不进行过滤的效果

DECLARE @ARTICLE AS VARCHAR(50) = '1234567'

select t1.*
from   table1 t1
where  ( isnull(@ARTICLE, '') = ''
         or
         exists ( select 1 from table2 t2 where t2.detailcode = @ARTICLE )
       ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM