sql where 子句仅包含 if case when
我想知道是否可以为多个客户端运行一个脚本,当我为指定的客户端运行时,我可以添加额外的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 SQL Server 上,您可以使用 HOST_NAME(),如下所示:
On SQL Server, you can use HOST_NAME() as follows:
当不需要参数时,您会在存储过程中经常看到这种情况:
不幸的是,这通常会导致扫描,因为它可能会也可能不会短路。
我要发布的另一个示例与 Derek Kromm 的示例相同。
最后,您可以使用动态 SQL - 人们要么喜欢它,要么讨厌它。
You'll see this in Stored Procedures a lot when a parameter is not needed:
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.