需要帮助构建存储过程

时间:2013-10-17 10:44:04

标签: sql stored-procedures

我正在尝试创建SP以下,但是当我尝试执行这五个参数时,它要求输入最后3个输出参数。有人可以指出我正在搞乱的事情以及我如何解决它:

create PROCEDURE division_price_info_heatmap (@comp_id INT,
@compe_id INT, 
@Division_id INT out, 
@Division_name VARCHAR(20) out, 
@final_shipping_price DECIMAL(11,2) out)
AS
BEGIN
select T1.div, T1.div_n, (T1.price-T2.price)  as final_shipping_price from
(select  tcpi.division as div, td.division_name as div_n,
sum(tcpi.final_price_shipping) as price
from tb_competitor_product_info_new as tcpi 
inner join tb_division td on tcpi.division=td.division_id  
where competitor_id=@comp_id
group by tcpi.division, td.division_name
)   as T1,

(select  tcpi.division as div, td.division_name as div_n,
sum(tcpi.final_price_shipping) as price
from tb_competitor_product_info_new as tcpi 
inner join tb_division td on tcpi.division=td.division_id  
where competitor_id=@compe_id
group by tcpi.division, td.division_name
)   as T2
where T1.div=T2.div

END

执行命令: - division_price_info_heatmap 1,2

错误:

错误:过程或函数'division_price_info_heatmap'需要参数'@Division_id',这是未提供的。 SQLState:S0004 ErrorCode:201

我正在使用mssql和squirrel UI。

还有一件事是我需要分配参数。

2 个答案:

答案 0 :(得分:0)

如果我没记错,MS SQL不会使用OUT关键字而是使用OUTPUT来存储过程,请尝试更改该部分,它应该可以正常工作。 See this for reference

当您调用该过程时,您必须提供将接收输出的变量,尽管在另一个答案中指出。

在旁注中看起来你实际上并没有返回任何东西,因为你从未向输出变量分配任何内容,但也许你在遗漏的代码中出现了这些内容。

此外,不需要围绕变量的括号,因此语句的开头应为:

CREATE PROCEDURE division_price_info_heatmap 
  @comp_id INT,
  @compe_id INT, 
  @Division_id INT OUTPUT, 
  @Division_name VARCHAR(20) OUTPUT, 
  @final_shipping_price DECIMAL(11,2) OUTPUT
AS

答案 1 :(得分:0)

您传递了两个参数,该过程需要更多参数。执行命令应为:

DECLARE @Division_id INT, @Division_name VARCHAR(20), @final_shipping_price DECIMAL(11,2)
EXEC division_price_info_heatmap 1,2,@Division_id OUT,@Division_name OUT, @final_shipping_price OUT

或者您可以修改程序:

create PROCEDURE division_price_info_heatmap (@comp_id INT,
@compe_id INT, 
@Division_id INT = NULL out, 
@Division_name VARCHAR(20) = NULL out, 
@final_shipping_price DECIMAL(11,2) = NULL out)
AS
...