在 T-SQL 中结合存储过程和查询 [英] Combine stored procedure and query in T-SQL
本文介绍了在 T-SQL 中结合存储过程和查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将存储过程的执行与在常规 SQL 查询中使用其结果或参数结合起来?
How do I combine executing of a stored procedure and using its result or parameters in a regular SQL query?
例如,我想执行以下操作:
For example I would like to do something like the following:
-- passing result of SELECT to SP
SELECT a, b FROM t
EXEC my_sp a, b
-- passing result of SP to INSERT
INSERT INTO t
EXEC my_sp a, b
等
推荐答案
不,你需要使用临时表
create table #results (col1 int, col2 varchar(5) ...)
INSERT INTO #results
EXEC YourProcedure @parma...
然后你就可以加入了
SELECT
*
FROM YourTable y
JOIN #results r ON ...
....
如果你不知道过程中的列和数据类型,你可以使用这个很好的答案:将存储过程的结果插入临时表
if you don't know the columns and data types from the procedure you can use this excellent answer: Insert results of a stored procedure into a temporary table
简而言之,它使用 OPENROWSET
将存储过程执行到动态创建的 #temp 表中,而无需命名和知道所有列的类型.
In brief it uses OPENROWSET
to execute the stored procedure into a #temp table that is created on the fly, without the need to name and know the type all the columns.
这篇关于在 T-SQL 中结合存储过程和查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文