【问题标题】:filtering a parameter in a where clause using an if/case statement使用 if/case 语句过滤 where 子句中的参数
【发布时间】:2020-07-24 13:59:56
【问题描述】:

我有两个这样的查询

select *
from table 1
where id=145

查询 2

select *
from table 1
where code=452 and typeCode <> 3

如何根据参数值过滤 where 条件,例如,我有以下参数,可以是“历史”或“最新”等

Declare @Type nvarchar(10)
set @Type='History'

所以现在我想在我的 where 子句中说 if type = history 而不是第一个查询如果它等于最新而不是第二个查询

select *
from table 1
where if @Type='History'
then 
id=145
else if @type='latest'
then
code=452 and typeCode <> 3

或如何使用上面的案例

where case @type='History'
then
id=145
when @type='Latest'
then
code=452 and typeCode <> 3

我的语法不正确,我该如何做到这一点?

【问题讨论】:

    标签: sql-server if-statement switch-statement


    【解决方案1】:

    只需使用OR 运算符

    select *
    from table 1
    where (
           @Type='History'
           AND id=145 
          )
    OR 
          (
           @type='latest'
           AND code=452 
           and typeCode <> 3
          )
    

    【讨论】:

      【解决方案2】:

      您的示例中有硬编码值。如果您希望这些内容中的每一个都是可选的,您可以将参数默认为 null 并像这样执行 where 语句

      CREATE PROCEDURE procMyProcedure
         @ID int = NULL,
         @Code int = NULL,
         @NotTypeCode int = NULL
      AS
      BEGIN
         SELECT *
         FROM tblMyTable
         WHERE (@ID IS NULL OR id = @ID) AND
            (@Code IS NULL OR code = @Code) AND
            (@NotTypeCode IS NULL OR typeCode <> @NotTypeCode)
      END 
      

      然后,您可以在代码中根据您的“历史”或“最新”确定要传递哪些参数。

      【讨论】:

        【解决方案3】:

        你可以用这个

        where id = ( case when @type = 'History' then 145 else id end )
        and code = ( case when @type = 'Latest'  then 452 else code end )
        and typecode = ( case when @type = 'Latest' and typeCode = 3 then -1 
                         else typeCode 
                         end 
                       )
        

        【讨论】:

          【解决方案4】:

          你可以像这样在 where 子句中使用 case-when:

          DECLARE @type varchar (15)
          SET @type = 'History'
          --SET @type = 'Latest'
          
          
          SELECT TOP (1000) *     
                  FROM [AdventureWorksDW].[dbo].[FactResellerSales]
          WHERE ProductKey = (CASE WHEN @type = 'History' THEN 349
                                   WHEN @type = 'Latest' THEN 350
                                   END)
                  AND CurrencyKey = (CASE WHEN @type = 'History' THEN 100
                                          WHEN @type = 'Latest' THEN CurrencyKey
                                          END)
          

          使用您的代码:

          DECLARE @type varchar (15)
          SET @type = 'History'
          --SET @type = 'Latest'
          
          
          SELECT *     
                  FROM table1
          WHERE id = (CASE WHEN @type = 'History' THEN 145
                           WHEN @type = 'Latest' THEN id
                           END)
                  AND typeCode <> (CASE WHEN @type = 'Latest' THEN 3 
                                        ELSE '' END)
                  AND code = (CASE WHEN @type = 'History' THEN code
                                   WHEN @type = 'Latest' THEN 452
                                   END)
          
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-07-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-01-07
            • 1970-01-01
            相关资源
            最近更新 更多