TSQL:两个选择的联合结果(过程?)
我有两个过程 - 两组巨大的选择以及几个子选择和联合。 我需要合并这些过程的结果,但我仍然需要它们单独存在。
类似这样的事情:
if @Param = 1 Then
PROCEDURE1
if @Param = 2 THEN
PROCEDURE2
if @Param = 3 Then
PROCEDURE1 union PROCEDURE2
我读到不可能在过程上有联合,并且我不能使用临时表。
有什么想法吗?
I have two procedures - two huge sets of selects with several sub-select and unions.
I need to union results from these procedures and I still need them to exists separately.
Something like that:
if @Param = 1 Then
PROCEDURE1
if @Param = 2 THEN
PROCEDURE2
if @Param = 3 Then
PROCEDURE1 union PROCEDURE2
I read that it's impossible to have union on procedures and I can't use temporary tables.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将过程转换为视图。
或者
您可以将过程执行到临时表中,然后将另一个过程执行到同一个临时表中:
You can convert the procedures to views.
OR
You can exec the procedures into a temp table, and then exec the other one into the same temp table:
您可以:
1)将数据插入临时表,然后从中选择:
2)只需从存储过程返回2个结果集,并让您的调用代码处理2个结果集
You could either:
1) Insert the data into a temp table then SELECT from that:
2) Just return 2 resultsets from the sproc and let your calling code handle 2 resultsets
有多种方法可以处理这种情况:
该问题缺乏具体细节,因此很难推荐一个而不是另一个,更不用说提供量身定制的答案了。该逻辑可以在单个查询中执行是合理的。
There are various ways to handle the situation:
The question lacks concrete details so it's hard to recommend one over another, much less provide a tailored answer. It's plausible that the logic could be performed within a single query.
如果不使用临时表,我只能想到其他两种方法。
如果可能,将 s'procs 转换为视图。
将调用哪个过程的逻辑移至您的应用程序(如果存在)。让它单独运行每个过程并合并结果。
Without the use of temp tables there are only two other ways that I can think of.
Convert the s'procs to views, if possible.
Move the logic on which proc to call to your application (if one exists). Have it run each proc separately and combine the results.