如何用几个输出参数编写程序?

时间:2014-04-01 11:26:04

标签: stored-procedures sql-server-2008-r2

我已经创建了这个程序,但这会引发错误。

CREATE PROCEDURE GetSurrenderPolicyDetails
  @policy_Id int,
  @name nvarchar(50) output,
  @policy_Amount Decimal(10,2) output,
  @premiumPaidTillDate Decimal(10,2) output
AS
BEGIN
  SET NOCOUNT ON;

  SELECT 
      @name = Policy_Details.Name,
      @policy_Amount = Insurance_Policy_Details.Policy_Amount,
      @premiumPaidTillDate = SUM(Payment_Premium_Details.Premium_Amount)
  FROM
      Policy_Details  
  INNER JOIN 
      Payment_Premium_Details ON (Policy_Details.Policy_Id = Payment_Premium_Details.Policy_Id)  
  INNER JOIN 
      Insurance_Policy_Details ON (Policy_Details.Ins_Id = Insurance_Policy_Details.Ins_Id)
  WHERE
      Policy_Details.Policy_Id = @policy_Id;      
END
GO

错误说

  

专栏' Policy_Details.Name'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

2 个答案:

答案 0 :(得分:0)

你的问题不是输出参数,而是你的连接条款;相反,尝试类似:

INNER JOIN  Insurance_Policy_Details 
    ON      Policy_Details.Ins_Id = Insurance_Policy_Amount.Ins_Id

INNER JOIN  Payment_Premium_Details 
    ON      Policy_Details.Policy_Id = Payment_Premium_Details.Policy_Id

答案 1 :(得分:0)

嗯,错误非常明确:您的SUM()汇总了SELECT,因此,您需要将GROUP BY用于SELECT中的其他列声明:

SELECT 
      @name = Policy_Details.Name,
      @policy_Amount = Insurance_Policy_Details.Policy_Amount,
      @premiumPaidTillDate = SUM(Payment_Premium_Details.Premium_Amount)
FROM
      Policy_Details  
INNER JOIN 
      Payment_Premium_Details ON (Policy_Details.Policy_Id = Payment_Premium_Details.Policy_Id)  
INNER JOIN 
      Insurance_Policy_Details ON (Policy_Details.Ins_Id = Insurance_Policy_Details.Ins_Id)
WHERE
      Policy_Details.Policy_Id = @policy_Id;      
GROUP BY
      Policy_Details.Name,
      Insurance_Policy_Details.Policy_Amount,