条件语句在WHERE子句中,基于参数值。

huangapple go评论63阅读模式
英文:

Condition Statement in WHERE Clause Based off Parameter Value

问题

以下是代码部分的翻译:

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

        /* 以下是问题部分 */
		(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)

希望这能帮助您在WHERE子句中使用条件语句。如果您有其他问题,请随时提出。

英文:

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 &gt;= @ImportDateFrom OR @ImportDateFrom IS NULL) AND
	(id.ImportDate &lt;= @ImportDateTo OR @ImportDateTo IS NULL)) AND

    /* Below is the problem */
	(CASE WHEN @AvailQtyOperator = &#39;1&#39; then
		id.AvailableQuantity &gt; @AvailQtyValue END)
	(CASE WHEN @AvailQtyOperator = &#39;2&#39; then 
	  id.AvailableQuantity &lt; @AvailQtyValue END)
	(CASE WHEN @AvailQtyOperator = &#39;3&#39; then 
	 id.AvailableQuantity = @AvailQtyValue END) 
	(CASE WHEN @AvailQtyOperator = &#39;4&#39; then
	 id.AvailableQuantity &lt;&gt; @AvailQtyValue END)

答案1

得分: 1

你可以使用普通的布尔逻辑。请注意,ANDOR具有更高的优先级。

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
  );

话虽如此,这将非常低效。最好构建一个动态查询,执行正确的条件。仍然应该正确传递参数,使用 sp_executesql。这种类型的查询被称为 "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;
英文:

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 &gt;= @ImportDateFrom OR @ImportDateFrom IS NULL) AND
  ( id.ImportDate &lt;= @ImportDateTo   OR @ImportDateTo   IS NULL) AND
  (
    @AvailQtyOperator = 1 AND id.AvailableQuantity &gt; @AvailQtyValue OR
    @AvailQtyOperator = 2 AND id.AvailableQuantity &lt; @AvailQtyValue OR
    @AvailQtyOperator = 3 AND id.AvailableQuantity = @AvailQtyValue OR
    @AvailQtyOperator = 4 AND id.AvailableQuantity &lt;&gt; @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) = &#39;
SELECT
  id.PK_Import, 
  id.importDate,
  id.AvailableQuantity
FROM Import id
WHERE 1=1 &#39;;

IF @ImportDateFrom IS NOT NULL
    SET @sql += &#39;
  AND id.ImportDate &gt;= @ImportDateFrom&#39;;

IF @ImportDateTo IS NOT NULL
    SET @sql += &#39;
  AND id.ImportDate &lt;= @ImportDateTo&#39;;

IF @AvailQtyOperator BETWEEN 1 AND 4
    SET @sql += &#39;
  AND id.AvailableQuantity &#39; +
      CASE @AvailQtyOperator
      WHEN 1 THEN &#39;&gt;&#39;
      WHEN 2 THEN &#39;&lt;&#39;
      WHEN 3 THEN &#39;=&#39;
      WHEN 4 THEN &#39;&lt;&gt;&#39;
      END + &#39; @AvailQtyValue
&#39;;

PRINT @sql;   -- your friend

EXEC sp_executesql @sql,
  N&#39;@ImportDateFrom datetime2,
    @ImportDateTo datetime2,
    @AvailQtyValue int&#39;,
  @ImportDateFrom = @ImportDateFrom,
  @ImportDateTo = @ImportDateTo,
  @AvailQtyValue = @AvailQtyValue;

答案2

得分: 0

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;

以上代码中的 CASE 语句将返回一个值,如果 @AvailQtyOperator 参数的值等于列表中的某个值,则该值将为非空。如果 @AvailQtyOperator 参数的值不等于列表中的任何值,则 CASE 语句将返回一个空值。

以上代码中的 WHERE 子句用于筛选 CASE 语句返回非空值的行。

英文:
    SELECT  id.PK_Import, 
        id.importDate,
        id.AvailableQuantity
FROM Import id
WHERE
    ( id.ImportDate &gt;= @ImportDateFrom OR @ImportDateFrom IS NULL)  AND
    ( id.ImportDate &lt;= @ImportDateTo   OR @ImportDateFrom IS NULL) AND
    CASE 
      WHEN @AvailQtyOperator = 1 THEN id.AvailableQuantity &gt; @AvailQtyValue
      WHEN @AvailQtyOperator = 2 THEN id.AvailableQuantity &lt; @AvailQtyValue
      WHEN @AvailQtyOperator = 3 THEN id.AvailableQuantity = @AvailQtyValue
      WHEN @AvailQtyOperator = 4 THEN id.AvailableQuantity &lt;&gt; @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.

答案3

得分: 0

(
@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
)

英文:
(
    @AvailQtyOperator = &#39;1&#39; and id.AvailableQuantity &gt;  @AvailQtyValue or
    @AvailQtyOperator = &#39;2&#39; and id.AvailableQuantity &lt;  @AvailQtyValue or
    @AvailQtyOperator = &#39;3&#39; and id.AvailableQuantity =  @AvailQtyValue or
    @AvailQtyOperator = &#39;4&#39; and id.AvailableQuantity &lt;&gt; @AvailQtyValue
)

huangapple
  • 本文由 发表于 2023年6月13日 01:57:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定