SQL:如果 var = ''不做任何其他事情向 WHERE 子句添加条件

发布于 2024-12-05 18:11:07 字数 1279 浏览 1 评论 0原文

我想在变量具有值的条件下执行 WHERE 子句的一部分。我尝试过以多种不同的方式编写此内容,但总是遇到错误,并且想知道是否有人可以提供帮助。

下面的代码是我当前有效的 SQL 语句,但如果我的 @itemId 为空,则不会返回任何内容。我只想减少 @itemId 作为值的结果。

DECLARE @itemId nvarchar(max)
SET @itemId = 'someId'

SELECT
SalesOrdersItems.Id,
CASE
    WHEN SalesOrderItems.ItemType = 'P' THEN Products.ProductId
    WHEN SalesOrderItems.ItemType = 'S' THEN Sundries.SundryId
    WHEN SalesOrderItems.ItemType = 'F' THEN FreightMethods.FreightMethodId
    ELSE SalesOrderItems.FreeTextItem
END AS ItemId
FROM SalesOrderItems
    LEFT OUTER JOIN Products ON SalesOrderItems.Product = Products.Product
    LEFT OUTER JOIN Sundries ON SalesOrderItems.Sundry = Sundries.Sundry
    LEFT OUTER JOIN FreightMethods ON SalesOrderItems.FreightMethod = FreightMethods.FreightMethod
WHERE 
SalesOrdersItems.area LIKE 'SE%'
AND (Products.ProductId = @itemId OR Sundries.SundryId = @itemId OR FreightMethods.FreightMethodId = @itemId OR SalesOrderItems.FreeTextItem = @itemId)

我尝试像这样编写最后一行(参见下面的代码),但它不起作用。

AND CASE 
        WHEN @itemId = '' THEN 1
        ELSE (Products.ProductId = @itemId OR Sundries.SundryId = @itemId OR FreightMethods.FreightMethodId = @itemId OR SalesOrderItems.FreeTextItem = @itemId)
    END

有人知道我哪里出错了吗?

I would like to perform one part of a WHERE clause on the condition that a variable has a value. I have tried writing this in many different ways but always hit an error and was wondering if someone could help.

The code below is my current SQL statement that works but will return nothing if my @itemId is empty. I would only like to reduce the results if the @itemId as a value.

DECLARE @itemId nvarchar(max)
SET @itemId = 'someId'

SELECT
SalesOrdersItems.Id,
CASE
    WHEN SalesOrderItems.ItemType = 'P' THEN Products.ProductId
    WHEN SalesOrderItems.ItemType = 'S' THEN Sundries.SundryId
    WHEN SalesOrderItems.ItemType = 'F' THEN FreightMethods.FreightMethodId
    ELSE SalesOrderItems.FreeTextItem
END AS ItemId
FROM SalesOrderItems
    LEFT OUTER JOIN Products ON SalesOrderItems.Product = Products.Product
    LEFT OUTER JOIN Sundries ON SalesOrderItems.Sundry = Sundries.Sundry
    LEFT OUTER JOIN FreightMethods ON SalesOrderItems.FreightMethod = FreightMethods.FreightMethod
WHERE 
SalesOrdersItems.area LIKE 'SE%'
AND (Products.ProductId = @itemId OR Sundries.SundryId = @itemId OR FreightMethods.FreightMethodId = @itemId OR SalesOrderItems.FreeTextItem = @itemId)

I have tried writing the last line like this(see code below) but it doesn't work.

AND CASE 
        WHEN @itemId = '' THEN 1
        ELSE (Products.ProductId = @itemId OR Sundries.SundryId = @itemId OR FreightMethods.FreightMethodId = @itemId OR SalesOrderItems.FreeTextItem = @itemId)
    END

Does anyone have any ideas where I am going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

卖梦商人 2024-12-12 18:11:07

如果我有误解,请告诉我,我假设如果您有一个空白的@itemID,您需要所有结果,否则您将限制结果。

WHERE SalesOrdersItems.area LIKE 'SE%' 
AND ((@itemId != '' 
    AND (Products.ProductId = @itemId 
         OR Sundries.SundryId = @itemId 
         OR FreightMethods.FreightMethodId = @itemId 
         OR SalesOrderItems.FreeTextItem = @itemId)) 
    OR @itemId = '')

我相信我的括号是正确的,但它们可能不均匀。希望它足够清楚地理解我的意图。

If I'm misunderstanding please let me know, I'm assuming that if you have a blank @itemID you want all results, otherwise you'll limit the results.

WHERE SalesOrdersItems.area LIKE 'SE%' 
AND ((@itemId != '' 
    AND (Products.ProductId = @itemId 
         OR Sundries.SundryId = @itemId 
         OR FreightMethods.FreightMethodId = @itemId 
         OR SalesOrderItems.FreeTextItem = @itemId)) 
    OR @itemId = '')

I believe I have the parens correct, but they might be uneven. Hopefully it is it clear enough to understand my intent.

小清晰的声音 2024-12-12 18:11:07
...
WHERE
1 = CASE WHEN ... THEN 1 ELSE 0 END
...
...
WHERE
1 = CASE WHEN ... THEN 1 ELSE 0 END
...
掀纱窥君容 2024-12-12 18:11:07

您可能可以使用 NULLIF()

AND CASE 
        WHEN NULLIF(@itemId, '') IS NULL THEN 1
        ELSE (Products.ProductId = @itemId OR Sundries.SundryId = @itemId OR FreightMethods.FreightMethodId = @itemId OR SalesOrderItems.FreeTextItem = @itemId)
    END

You could probably use NULLIF()

AND CASE 
        WHEN NULLIF(@itemId, '') IS NULL THEN 1
        ELSE (Products.ProductId = @itemId OR Sundries.SundryId = @itemId OR FreightMethods.FreightMethodId = @itemId OR SalesOrderItems.FreeTextItem = @itemId)
    END
初相遇 2024-12-12 18:11:07

根据我的理解,你应该做如下的事情。

     (Products.ProductId = @itemId OR ISNULL(@itemId, '') = '') OR 
     (Sundries.SundryId = @itemId OR ISNULL(@itemId, '') = '') OR 
     (FreightMethods.FreightMethodId = @itemId OR ISNULL(@itemId, '') = '') OR 
     (FreightMethods.FreeTextItem = @itemId OR ISNULL(@itemId, '') = '') 

希望这有帮助!

As per my understanding, you should do something like below.

     (Products.ProductId = @itemId OR ISNULL(@itemId, '') = '') OR 
     (Sundries.SundryId = @itemId OR ISNULL(@itemId, '') = '') OR 
     (FreightMethods.FreightMethodId = @itemId OR ISNULL(@itemId, '') = '') OR 
     (FreightMethods.FreeTextItem = @itemId OR ISNULL(@itemId, '') = '') 

Hope this helps!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文