带有条件结果的存储过程
我想编写一个存储过程,其工作原理如下:
SELECT * from T where T.A = @a and T.B = @b
如果返回行,则返回这些行,如果没有,则返回
SELECT * from T where T.A = @a and T.B IS NULL
编辑:
感觉应该有一种方法来创建一个过程,以便它第一个查询运行一次,仅在必要时运行第二个查询。
结束编辑。
我能做到的最好的方法是执行以下操作,它(理论上)运行第一个查询两次,除非它被缓存:
IF EXISTS (SELECT * from T where T.A = @a and T.B = @b) THEN
SELECT * from T where T.A = @a and T.B = @b
ELSE
SELECT * from T where T.A = @a and T.B IS NULL
对于它的价值,这是在 Microsoft SQL Server 2008 中
I want to write a stored procedure that works something like this:
SELECT * from T where T.A = @a and T.B = @b
if that returns rows, return those rows, if not, return
SELECT * from T where T.A = @a and T.B IS NULL
Edit:
It feels that there should be a way to create a procedure such that it runs the first query once and runs the second query only if necessary.
End Edit.
The best I could manage was the follow, which (in theory) runs the first query twice, unless maybe its cached:
IF EXISTS (SELECT * from T where T.A = @a and T.B = @b) THEN
SELECT * from T where T.A = @a and T.B = @b
ELSE
SELECT * from T where T.A = @a and T.B IS NULL
For what its worth, this is in Microsoft SQL Server 2008
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这应该避免为了存在性检查而进行额外的表访问。我不确定是否有更简洁的方法。
This should avoid the additional table access for the existence check. I'm not sure if there's a neater way.
我认为您可以使用表变量来完成此操作,这应该避免两个结果集问题。像这样的东西:
I think you can do this with a table variable, which should avoid the two resultsets issue. Something like:
为什么不能在单个查询中执行此操作:
另一个单个查询解决方案:
Why can't you do this in a single query:
Another single query solution:
您也可以在一个查询中完成此操作:
You can also do it in one query:
我不知道它是否对性能有帮助,但你可以尝试表值函数:
但我怀疑它有帮助。
一般来说,我会坚持你原来的方法。这是最简单、最干净的。缓存和良好的索引应该照顾到性能。
如果这里确实存在性能问题,我会退一步看看这个数据库设计。为什么那里有空值?你为什么要尝试两个过滤器?可以用不同的方式建模吗?如果不是,也许有点非规范化?
I don't know it if helps at all performance-wise, but you could try table-valued function:
But I doubt it helps.
Generally I would stick with your original approach. It is the simplest and cleanest. And cache and good index should take care of performance.
If there were real performance problems here, I would step back and look at this database design. Why are you having nulls there? Why are you trying two filters? Can it be modeled differently? If not, maybe a little denormalization?
试试这个,如果第一个选择返回 rows ,那么如果第一个失败则返回,然后下一个选择返回或最后一个选择:
Try this, if the first select return rows , then it returns if the first fails then the next select returns or finally the last select:
编辑 编辑问题后编辑了答案。
EDIT The answer was edited after the question was edited.