英文:
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 >= @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)
答案1
得分: 1
你可以使用普通的布尔逻辑。请注意,AND
比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
);
话虽如此,这将非常低效。最好构建一个动态查询,执行正确的条件。仍然应该正确传递参数,使用 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 >= @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;
答案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 >= @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.
答案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 = '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
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论