1

我创建了以下存储过程来计算百分比,它工作正常,但我总是无法得到确切的结果

例如98.000,而不是98.947


CREATE PROCEDURE sp_finger_calc_percent
AS
DECLARE  @no_action decimal(18,4);
DECLARE  @yes_action decimal(18,4);

 SELECT   @no_action = COUNT(*)
FROM         Action INNER JOIN
                      Employee ON Action.b_num = Employee.b_num INNER JOIN
                      Department ON Employee.dep_id = Department.dep_id
WHERE     (Department.dep_id = 50) AND (Action.action = 0);


SELECT   @yes_action= COUNT(*)
FROM       Action INNER JOIN
                      Employee ON Action.b_num = Employee.b_num INNER JOIN
                      Department ON Employee.dep_id = Department.dep_id
WHERE     (Department.dep_id = 50) AND (Action.action = 1);

If(@no_action = 0.00)
 return 100.00
Else return cast( (100.00  *(@yes_action  /@no_action ) )as decimal(18,4)) 
;

DECLARE  @return_status decimal(18,4);
EXEC @return_status = sp_finger_calc_percent
select @return_status;
4

3 回答 3

3

SQL Server 中的过程只能返回 INTeger 类型。我相信,您需要一个实值函数来执行此操作(或者,更好的是,正如其他人指出的那样,您的过程需要一个十进制 OUTPUT 参数)。

于 2012-11-20T17:04:28.777 回答
1

另一种可能性是使用 out 参数

Alter PROCEDURE sp_finger_calc_percent(@return_status decimal(18,4) out)
AS
DECLARE  @no_action decimal(18,4);
DECLARE  @yes_action decimal(18,4);

SELECT   @no_action = COUNT(*)
FROM         Action INNER JOIN
                      Employee ON Action.b_num = Employee.b_num INNER JOIN
                      Department ON Employee.dep_id = Department.dep_id
WHERE     (Department.dep_id = 50) AND (Action.action = 0);


SELECT   @yes_action= COUNT(*)
FROM       Action INNER JOIN
                      Employee ON Action.b_num = Employee.b_num INNER JOIN
                      Department ON Employee.dep_id = Department.dep_id
WHERE     (Department.dep_id = 50) AND (Action.action = 1);

If(@no_action = 0.00)
 select @return_status= 100.00
Else select @return_status = cast( (100.00  *(@yes_action  /@no_action ) )as decimal(18,4)) 
;

称呼

DECLARE  @return_status decimal(18,4);
EXEC  sp_finger_calc_percent @return_status out
select @return_status;
于 2012-11-20T17:12:18.380 回答
1

更改为输出参数(如 Alex K 所述):

CREATE PROCEDURE sp_finger_calc_percent
    @RETURNVALUE DECIMAL(18,4) OUTPUT
AS
...
If(@no_action = 0.00)
 SET @RETURNVALUE = 100.00
Else SET @RETURNVALUE = cast( (100.00  *(@yes_action  /@no_action ) )as decimal(18,4)) 
;
于 2012-11-20T17:12:44.650 回答