sql where 子句仅包含 if case when

发布于 2024-12-18 19:03:05 字数 430 浏览 3 评论 0原文

我想知道是否可以为多个客户端运行一个脚本,当我为指定的客户端运行时,我可以添加额外的 where 子句,而不必多次重写基本脚本?

例如。基本脚本是

SELECT Status, count(distinct T1.AccountNo)
FROM Orders (nolock), Accounts (NOLOCK)
WHERE Orders.AccountNo = Accounts .AccountNo  
AND date between '1 Oct 2011' and '31 Oct 2011'
GROUP BY Status

但是当我为客户运行时 AI 希望包含额外的标准

AND Accounts.Role IN ('User','Admin')
AND Accounts.Active= 'Y'

I was wondering if it's possible to run a script for multiple Clients where I can add extra where clauses when I'm running for the specified Client, without having to rewrite the basic script multiple times?

Eg. basic script is

SELECT Status, count(distinct T1.AccountNo)
FROM Orders (nolock), Accounts (NOLOCK)
WHERE Orders.AccountNo = Accounts .AccountNo  
AND date between '1 Oct 2011' and '31 Oct 2011'
GROUP BY Status

But when I run for Client A I would like to include the extra criteria

AND Accounts.Role IN ('User','Admin')
AND Accounts.Active= 'Y'

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

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

发布评论

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

评论(3

晨光如昨 2024-12-25 19:03:05
SELECT Status, count(distinct T1.AccountNo)
FROM Orders (nolock), Accounts (NOLOCK)
WHERE Orders.AccountNo = Accounts .AccountNo  
AND date between '1 Oct 2011' and '31 Oct 2011'
AND CASE WHEN Client = 'A' THEN Accounts.Role ELSE 'User' END IN ('User', 'Admin')
AND CASE WHEN Client = 'A' THEN Accounts.Active ELSE 'Y' END = 'Y'
GROUP BY Status
SELECT Status, count(distinct T1.AccountNo)
FROM Orders (nolock), Accounts (NOLOCK)
WHERE Orders.AccountNo = Accounts .AccountNo  
AND date between '1 Oct 2011' and '31 Oct 2011'
AND CASE WHEN Client = 'A' THEN Accounts.Role ELSE 'User' END IN ('User', 'Admin')
AND CASE WHEN Client = 'A' THEN Accounts.Active ELSE 'Y' END = 'Y'
GROUP BY Status
纸伞微斜 2024-12-25 19:03:05

在 SQL Server 上,您可以使用 HOST_NAME(),如下所示:

SELECT Status, count(distinct T1.AccountNo)
FROM Orders (nolock), Accounts (NOLOCK)
WHERE Orders.AccountNo = Accounts .AccountNo  
AND date between '1 Oct 2011' and '31 Oct 2011'
AND ((HOST_NAME() <> 'ClientA') OR (Accounts.Role IN ('User') AND Accounts.Active= 'Y'))
GROUP BY Status

On SQL Server, you can use HOST_NAME() as follows:

SELECT Status, count(distinct T1.AccountNo)
FROM Orders (nolock), Accounts (NOLOCK)
WHERE Orders.AccountNo = Accounts .AccountNo  
AND date between '1 Oct 2011' and '31 Oct 2011'
AND ((HOST_NAME() <> 'ClientA') OR (Accounts.Role IN ('User') AND Accounts.Active= 'Y'))
GROUP BY Status
友欢 2024-12-25 19:03:05

当不需要参数时,您会在存储过程中经常看到这种情况:

SELECT *
FROM SomeTable
WHERE ((@myvariable IS NULL OR @myvariable = mycolumn))

不幸的是,这通常会导致扫描,因为它可能会也可能不会短路。

我要发布的另一个示例与 Derek Kromm 的示例相同。

最后,您可以使用动态 SQL - 人们要么喜欢它,要么讨厌它。

You'll see this in Stored Procedures a lot when a parameter is not needed:

SELECT *
FROM SomeTable
WHERE ((@myvariable IS NULL OR @myvariable = mycolumn))

Unfortunately this usually causes a scan since it may or may not short circuit.

The other example that I was going to post is the same as Derek Kromm's.

Lastly you can use Dynamic SQL - people either love it or hate it.

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