如何加入存储过程?
我有一个不带参数的存储过程,它返回两个字段。 该存储过程汇总了应用于租户的所有事务,并返回余额和租户 ID。
我想使用它通过查询返回的记录集,并且我需要将其结果连接到租户的 ID 上。
这是我当前的查询:
SELECT t.TenantName, t.CarPlateNumber, t.CarColor, t.Sex, t.SSNO, t.Phone, t.Memo,
u.UnitNumber,
p.PropertyName
FROM tblTenant t
LEFT JOIN tblRentalUnit u
ON t.UnitID = u.ID
LEFT JOIN tblProperty p
ON u.PropertyID = p.ID
ORDER BY p.PropertyName, t.CarPlateNumber
存储过程是这样的:
SELECT tenant.ID AS TenantID, SUM(ISNULL(trans.Amount,0)) AS TenantBalance FROM tblTenant tenant
LEFT JOIN tblTransaction trans
ON tenant.ID = trans.TenantID
GROUP BY tenant.ID
我也想将存储过程中的余额添加到其中。
我怎样才能做到这一点?
I have a stored procedure that takes no parameters, and it returns two fields. The stored procedure sums up all transactions that are applied to a tenant, and it returns the balance and the id of the tenant.
I want to use the record set it returns with a query, and I need to join it's results on the id of the tenant.
This is my current query:
SELECT t.TenantName, t.CarPlateNumber, t.CarColor, t.Sex, t.SSNO, t.Phone, t.Memo,
u.UnitNumber,
p.PropertyName
FROM tblTenant t
LEFT JOIN tblRentalUnit u
ON t.UnitID = u.ID
LEFT JOIN tblProperty p
ON u.PropertyID = p.ID
ORDER BY p.PropertyName, t.CarPlateNumber
The stored procedure is this:
SELECT tenant.ID AS TenantID, SUM(ISNULL(trans.Amount,0)) AS TenantBalance FROM tblTenant tenant
LEFT JOIN tblTransaction trans
ON tenant.ID = trans.TenantID
GROUP BY tenant.ID
I would like to add the balance from the stored procedure to it also.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
将 SP 的结果插入临时表,然后连接:
insert the result of the SP into a temp table, then join:
我实际上喜欢前面的答案(不要使用 SP),但如果您由于某种原因与 SP 本身绑定,您可以使用它来填充临时表,然后加入临时表。 请注意,您将在那里花费一些额外的开销,但这是我能想到的使用实际存储过程的唯一方法。
同样,您最好将 SP 中的查询内联到原始查询中。
I actually like the previous answer (don't use the SP), but if you're tied to the SP itself for some reason, you could use it to populate a temp table, and then join on the temp table. Note that you're going to cost yourself some additional overhead there, but it's the only way I can think of to use the actual stored proc.
Again, you may be better off in-lining the query from the SP into the original query.
简短的回答是“你不能”。 您需要做的是使用子查询,或者可以将现有存储过程转换为表函数。 将其创建为函数取决于您需要它的“可重用性”程度。
The short answer is "you can't". What you'll need to do is either use a subquery or you could convert your existing stored procedure in to a table function. Creating it as function would depend on how "reusable" you would need it to be.
您的存储过程可以轻松地用作视图。 然后您可以将其加入到您需要的任何其他内容中。
SQL:
您可以执行任何语句,例如:
Your stored procedure could easily be used as a view instead. Then you can join it on to anything else you need.
SQL:
The you can do any statement like:
已经有人回答了,最好的解决方法是将存储过程转换为 SQL 函数或视图。
简而言之,您不能直接在 SQL 中 JOIN 存储过程,除非您使用存储过程的输出到临时表中并 JOIN 临时表来创建另一个存储过程或函数。
我将通过将存储过程转换为 SQL 函数来回答这个问题,并向您展示如何在您选择的查询中使用它。
现在要在 SQL 中使用该函数...
如果您希望从上述 SQL 中将参数传递到函数中,那么我建议您使用
CROSS APPLY
或CROSS OUTER APPLY
代码>.请阅读此处。
干杯
It has already been answered, the best way work-around is to convert the Stored Procedure into an SQL Function or a View.
The short answer is that you cannot directly JOIN a Stored Procedure in SQL, unless you create another stored procedure or function using the stored procedure's output into a temporary table and JOINing the temporary table.
I will answer this by converting your Stored Procedure into an SQL function and show you how to use it inside a query of your choice.
Now to use that function, in your SQL...
If you wish to pass parameters into your function from within the above SQL, then I recommend you use
CROSS APPLY
orCROSS OUTER APPLY
.Read up on that here.
Cheers
我通过编写函数而不是过程并在 SQL 语句中使用 CROSS APPLY 解决了这个问题。 此解决方案适用于 SQL 2005 及更高版本。
I resolved this problem writing function instead of procedure and using CROSS APPLY in SQL statement. This solution works on SQL 2005 and later versions.
这对你来说是一个糟糕的主意。
使用别名,从您的服务器创建一个新的链接服务器到它自己的别名。
现在你可以这样做:
Here's a terrible idea for you.
Use an alias, create a new linked server from your server to its own alias.
Now you can do:
我希望您的存储过程没有执行游标循环!
如果没有,请从存储过程中获取查询并将该查询集成到您在此处发布的查询中:
如果您在存储过程中执行的操作不仅仅是查询,请创建一个临时表并将存储过程执行到该临时表中,然后然后加入您的查询中。
I hope your stored procedure is not doing a cursor loop!
If not, take the query from your stored procedure and integrate that query within the query you are posting here:
If you are doing something more than a query in your stored procedure, create a temp table and execute the stored procedure into this temp table and then join to that in your query.
为什么不直接在 SQL 中执行计算呢?
Why not just performing the calculation in your SQL?