简体   繁体   English

将结果从存储过程返回到另一个存储过程

[英]Return results from stored proc to another stored proc

I have a stored proc - [dbo].[DynamicPivotTableInSql1] that I need to call from another stored proc and continue further query creation.我有一个存储过程 - [dbo].[DynamicPivotTableInSql1] 我需要从另一个存储过程调用并继续进一步的查询创建。 I see that the stored proc [dbo].[DynamicPivotTableInSql1] is executing successfully but the results are not getting returned.我看到存储过程 [dbo].[DynamicPivotTableInSql1] 正在成功执行,但没有返回结果。 Could you please let me know if I am doing something wrong here.如果我在这里做错了什么,请告诉我。

I have very recently posted a related question, but on further analysis found that the results not getting returned to the calling stored proc is the issue.我最近发布了一个相关问题,但进一步分析发现结果没有返回到调用存储过程是问题所在。

I need to use dynamic sql in [dbo].[DynamicPivotTableInSql1], because the query is quite complicated and I have to extend the where clause based on certain input parameters.我需要在[dbo].[DynamicPivotTableInSql1]中使用动态sql,因为查询相当复杂,我必须根据某些输入参数扩展where子句。 I have only posted a part of it.我只贴了一部分。

Thanks for your help and time!感谢您的帮助和时间!

CREATE PROCEDURE [dbo].[DynamicPivotTableInSql1]

AS
BEGIN
DECLARE @colsPivot AS NVARCHAR(MAX),
@query  AS NVARCHAR(MAX),
@str_comp1 AS NVARCHAR(MAX),
@str_comp2 AS NVARCHAR(MAX) 


SET @str_comp1 = ' comp1 IN (''Capacity'') and '
SET @str_comp2 = ' comp2 IN (''Refinery'') '

SET @colsPivot = 'SELECT STUFF((SELECT  '','' 
                      + quotename(link_id)
                    from [dbo].[test_excel_poc_head] t
WHERE ' + @str_comp1 +  @str_comp2 + '
FOR XML PATH(''''), TYPE ).value(''.'', ''NVARCHAR(MAX)'') 
,1,1,'''')'

EXEC(@colsPivot)
END

The calling procedure is below:调用流程如下:

BEGIN
DECLARE @colsPivot1 AS NVARCHAR(MAX),
@return_result  AS NVARCHAR(MAX) 

EXEC    @return_result = [dbo].[DynamicPivotTableInSql1]
print('Return Value: ' + @return_result)
END

Below is the result of executing stored proc [dbo].[DynamicPivotTableInSql1]:下面是执行存储过程 [dbo].[DynamicPivotTableInSql1] 的结果: 在此处输入图片说明

But below is the returned result:但下面是返回的结果: 在此处输入图片说明

Your problem is that you're attempting to use a stored procedure as a function.您的问题是您试图将存储过程用作函数。 A stored procedure returns a numeric value, 0 for success, or another value that represents an error.存储过程返回一个数值,0 表示成功,或另一个表示错误的值。

Functions do not allow for calling things like EXEC , however, you can define a stored procedure's variable as OUT or OUTPUT (depending on the need) and in your case, it appears you only need OUT .函数不允许调用诸如EXEC东西,但是,您可以将存储过程的变量定义为OUTOUTPUT (取决于需要),在您的情况下,您似乎只需要OUT

Here is an example of how you can do this:以下是如何执行此操作的示例:

/* Create base procedure with an OUT parameter */
CREATE OR ALTER PROCEDURE dbo.Procedure1 (
    @results xml OUT -- OUT parameter to return the results.
)
AS
BEGIN

    /* Build dynamic SQL statement */
    DECLARE @statement nvarchar(1000) = 'SET @dynamicResults = ( SELECT * FROM Misc FOR XML PATH( '''' ), TYPE );';

    /* Execute the dynamic SQL statement and capture results into the @results OUT parameter */
    EXEC sp_executesql @statement, N'@dynamicResults xml OUT', @dynamicResults = @results OUT;

END
GO

/* Create a procedure that captures the results from the base procedure */
CREATE OR ALTER PROCEDURE dbo.Procedure2
AS
BEGIN

    /* Declare variable to capture the results from Procedure1 */
    DECLARE @results xml;

    /* Call Procedure1 and capture its results */
    EXEC dbo.Procedure1 @results OUT;

    /* Return the results from Procedure1 */
    SELECT @results AS ResultsFromProcedure1;

END
GO

/* Call Procedure2 */
EXEC dbo.Procedure2;

Calling Procedure2 in this example simply returns the results captured from Procedure1 .在此示例中调用Procedure2仅返回从Procedure1捕获的结果。 Once you have the results from the first procedure, you can continue working with them.获得第一个程序的结果后,您可以继续使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM