将存储过程中的存储过程的结果分配给变量

时间:2015-01-30 11:42:38

标签: sql sql-server sql-server-2008

我的脚本返回两个值,只需要一个值。我想将执行Sel_NPCA_Min_PlanID_ByID的结果分配给coalesce参数的最后一部分。这可以吗?

ALTER PROCEDURE [dbo].[Sel_NPCA_PlanID]
(@ID int)
AS
BEGIN
DECLARE @PlanID int
EXEC @PlanID = Sel_NPCA_Min_PlanID_ByID @ID

SELECT
COALESCE(checkboxTicked.PLAN_ID, firstPlanDiscussedAgreed.PLAN_ID,     firstPlanAgreed.PLAN_ID, @PlanID) AS PLAN_ID
        FROM referral   
            LEFT JOIN checkboxTicked
                ON referral.ID = checkboxTicked.ID          
            LEFT JOIN firstPlanDiscussedAgreed
                ON referral.ID = firstPlanDiscussedAgreed.ID AND checkboxTicked.PLAN_ID IS NULL         
            LEFT JOIN firstPlanAgreed
                ON referral.ID = firstPlanAgreed.ID AND checkboxTicked.PLAN_ID IS NULL AND firstPlanDiscussedAgreed.PLAN_ID IS NULL     
WHERE referral.ID = @ID

END

1 个答案:

答案 0 :(得分:0)

如果要将过程的输出分配给变量,则应该使用(标量)函数而不是过程。其他选项是在过程参数列表中使用输出变量。