我创建以下存储过程来计算百分比,它工作正常,但我总是无法得到确切的结果
例如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;
答案 0 :(得分:3)
<强> Procedures in SQL Server can only return INTeger types 即可。你需要一个实值函数来做到这一点,我相信(或者,更好的是,正如其他人指出的那样,你的过程的十进制OUTPUT参数)。
答案 1 :(得分:1)
另一种可能是使用参数
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;
答案 2 :(得分: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))
;