我有这个存储过程:
CREATE PROCEDURE [dbo].[sp_get_correct_responses]
@QuestionUId UNIQUEIDENTIFIER
AS
BEGIN
...
-- This is the last part of the SP. I need to use the output
-- value of @AnswerGridCorrect in the calling SP
SELECT @AnswerGridCorrect = Correct
FROM Concatenated
WHERE RowNumber = ColumnCount
END
如何从另一个存储过程调用存储过程,传递@QuestionUId参数并将返回的变量@AnswerGridCorrect放入调用过程中声明的变量中?
更新:这是建议的答案:
CREATE PROCEDURE [dbo].[sp_get_correct_responses]
@QuestionUId UNIQUEIDENTIFIER,
@output VARCHAR(20) output
AS
BEGIN
select @QuestionUId
DECLARE @AnswerGridCorrect VARCHAR(20)
DECLARE @QuestionId int;
SELECT @QuestionId = QuestionId
FROM dbo.Question
Where QuestionUId = @QuestionUId;
Select @questionId;
WITH Partitioned AS (
SELECT ROW_NUMBER() OVER (PARTITION BY QuestionId ORDER BY AnswerId ASC) AS RowNumber,
COUNT(1) OVER (PARTITION BY QuestionId) AS ColumnCount,
CONVERT(VARCHAR(MAX), Correct) AS Correct
FROM dbo.Answer
WHERE [QuestionId] = @QuestionId
),
Concatenated AS (
SELECT RowNumber, ColumnCount, Correct FROM Partitioned WHERE RowNumber = 1
UNION ALL
SELECT P.RowNumber,
P.ColumnCount,
C.Correct + P.Correct AS Correct
FROM Partitioned P
INNER JOIN Concatenated C
ON P.RowNumber = C.RowNumber + 1
)
SET @output = (SELECT Correct
FROM Concatenated
WHERE RowNumber = ColumnCount)
RETURN
END
答案 0 :(得分:0)
您可以在父SP中创建一个表变量,并在下面插入子SP的结果:
DECLARE @TempTable TABLE(AnswerGridCorrect INT)
INSERT INTO @TempTable
EXEC [dbo].[sp_get_correct_responses] @QuestionUId
答案 1 :(得分:0)
您可以在另一个存储过程中使用临时表,并使用此结果填充它:
INSERT INTO #table
Exec sp_get_correct_responses @QuestionUId
另一种方法是修改sp_get_correct_responses
以产生输出,因为您只需要一个值。
CREATE PROCEDURE [dbo].[sp_get_correct_responses]
@QuestionUId UNIQUEIDENTIFIER,
@output VARCHAR(20) output
AS
BEGIN
...
-- This is the last part of the SP. I need to use the output
-- value of @AnswerGridCorrect in the calling SP
SELECT @output = Correct
FROM Concatenated
WHERE RowNumber = ColumnCount
RETURN
END
在你的其他SP中:
DECLARE @output VARCHAR(20)
EXEC sp_get_correct_responses
@QuestionUId,
@output output
SELECT @output