将查询结果传递给存储过程 [英] Pass result of a query into stored procedure

查看:54
本文介绍了将查询结果传递给存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受三个参数的存储过程.

I have a stored procedure that accepts three parameters.

是否可以使用查询作为输入来调用此存储过程?

Is it possible to call this stored procedure with a query as input?

例如,我有 tableA.

SELECT * FROM TABLEA 

| A | B | C |
|---+---+---|
| 1 | 2 | 3 |
| 4 | 5 | 6 |

现在,有没有办法可以调用

Now, is there a way that I can call

EXEC sp_name (SELECT * FROM TABLEA) 

这样存储过程会为每一行执行吗?

so that the stored procedure will execute for each row?

这背后的原因是我有一个需要为多行调用的存储过程.我可以编写一个脚本来执行此操作,但想知道是否可以使用 TSQL 来执行此操作.

My reasoning behind this is I have a stored procedure that needs to be called for multiple rows. I can write a script to do this, but want to know if its possible to do it with TSQL.

推荐答案

实现此目的的最佳方法是将您的第二个存储过程创建为用户定义的函数.然后就可以这样调用了:

The best way to accomplish this is to create your 2nd stored procedure as a user-defined function instead. Then you can call it in this way:

SELECT * 
FROM TableA
CROSS APPLY dbo.yourUserFunction(column1, column2, etc)

或者,可能有一种方法可以将两个存储过程合并为一个集合,但我必须查看您调用的第二个存储过程中的内容.

Or, there may be a way to combine both stored procedures into one set, but I'd have to see what was in the 2nd one you are calling.

这篇关于将查询结果传递给存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆