SQL Server Condition Statement in WHERE Clause Based off Parameter Value

jobtbby3  于 2023-06-21  发布在  其他
关注(0)|答案(3)|浏览(203)

I am trying to find the best way to use a conditional statement in my WHERE clause based off a parameter.

The statement should be 'if @AvailQtyOperator = 1 then {bring me back data where} id.AvailableQuantity > @AvailQtyValue'

If @AvailQtyOperator = 1 means greaterThan So I am wanting to only select data where AvailableQuantity is greater than the incoming parameter @AvailQtyValue

I am uncertain if CASE is the best choice for this. Any direction would be appreciated.

Here is what I have tried so far but can't find the right syntax:

SELECT  id.PK_Import, 
        id.importDate,
        id.AvailableQuantity
FROM Import id
WHERE
    ((id.ImportDate >= @ImportDateFrom OR @ImportDateFrom IS NULL) AND
    (id.ImportDate <= @ImportDateTo OR @ImportDateTo IS NULL)) AND

    /* Below is the problem */
    (CASE WHEN @AvailQtyOperator = '1' then
        id.AvailableQuantity > @AvailQtyValue END)
    (CASE WHEN @AvailQtyOperator = '2' then 
      id.AvailableQuantity < @AvailQtyValue END)
    (CASE WHEN @AvailQtyOperator = '3' then 
     id.AvailableQuantity = @AvailQtyValue END) 
    (CASE WHEN @AvailQtyOperator = '4' then
     id.AvailableQuantity <> @AvailQtyValue END)
x6yk4ghg

x6yk4ghg1#

You can just use normal boolean logic. Note that AND has a higher precedence than OR .

SELECT
  id.PK_Import, 
  id.importDate,
  id.AvailableQuantity
FROM Import id
WHERE
  ( id.ImportDate >= @ImportDateFrom OR @ImportDateFrom IS NULL) AND
  ( id.ImportDate <= @ImportDateTo   OR @ImportDateTo   IS NULL) AND
  (
    @AvailQtyOperator = 1 AND id.AvailableQuantity > @AvailQtyValue OR
    @AvailQtyOperator = 2 AND id.AvailableQuantity < @AvailQtyValue OR
    @AvailQtyOperator = 3 AND id.AvailableQuantity = @AvailQtyValue OR
    @AvailQtyOperator = 4 AND id.AvailableQuantity <> @AvailQtyValue
  );

Having said that, this is going to be highly inefficient. You are better off building a dynamic query that will do the correct conditions. You should still pass the parameters in properly using sp_executesql . This type of query is called a Kitchen Sink Query.

DECLARE @sql nvarchar(max) = '
SELECT
  id.PK_Import, 
  id.importDate,
  id.AvailableQuantity
FROM Import id
WHERE 1=1 ';

IF @ImportDateFrom IS NOT NULL
    SET @sql += '
  AND id.ImportDate >= @ImportDateFrom';

IF @ImportDateTo IS NOT NULL
    SET @sql += '
  AND id.ImportDate <= @ImportDateTo';

IF @AvailQtyOperator BETWEEN 1 AND 4
    SET @sql += '
  AND id.AvailableQuantity ' +
      CASE @AvailQtyOperator
      WHEN 1 THEN '>'
      WHEN 2 THEN '<'
      WHEN 3 THEN '='
      WHEN 4 THEN '<>'
      END + ' @AvailQtyValue
';

PRINT @sql;   -- your friend

EXEC sp_executesql @sql,
  N'@ImportDateFrom datetime2,
    @ImportDateTo datetime2,
    @AvailQtyValue int',
  @ImportDateFrom = @ImportDateFrom,
  @ImportDateTo = @ImportDateTo,
  @AvailQtyValue = @AvailQtyValue;
jljoyd4f

jljoyd4f2#

(
    @AvailQtyOperator = '1' and id.AvailableQuantity >  @AvailQtyValue or
    @AvailQtyOperator = '2' and id.AvailableQuantity <  @AvailQtyValue or
    @AvailQtyOperator = '3' and id.AvailableQuantity =  @AvailQtyValue or
    @AvailQtyOperator = '4' and id.AvailableQuantity <> @AvailQtyValue
)
lhcgjxsq

lhcgjxsq3#

SELECT  id.PK_Import, 
        id.importDate,
        id.AvailableQuantity
FROM Import id
WHERE
    ( id.ImportDate >= @ImportDateFrom OR @ImportDateFrom IS NULL)  AND
    ( id.ImportDate <= @ImportDateTo   OR @ImportDateFrom IS NULL) AND
    CASE 
      WHEN @AvailQtyOperator = 1 THEN id.AvailableQuantity > @AvailQtyValue
      WHEN @AvailQtyOperator = 2 THEN id.AvailableQuantity < @AvailQtyValue
      WHEN @AvailQtyOperator = 3 THEN id.AvailableQuantity = @AvailQtyValue
      WHEN @AvailQtyOperator = 4 THEN id.AvailableQuantity <> @AvailQtyValue
    END;

The CASE statement in the above code will return a value that is not NULL if the value of the @ AvailQtyOperator parameter is equal to one of the values in the list. If the value of the @ AvailQtyOperator parameter is not equal to any of the values in the list, the CASE statement will return a value that is NULL .

The WHERE clause in the above code is used to filter the rows where the CASE statement returns a value that is not NULL.

相关问题