0

任何关于将员工 11 的总工资四舍五入的想法是 10.44,但出于某种原因将其四舍五入为 10,即使他们都设置为小数 (8,2)

   ALTER procedure [dbo].[hms_GetEmployeeSallaryByEmpNo]
    (

    @Id int
   )
as
/* lets make our tempory tables */
create table #contracts
( 
empno INT,
contract_id INT
)

 DECLARE @contract_id int
 DECLARE @totalsallary decimal(8,2)
 begin 
  INSERT into #contracts SELECT c.emp_no,c.contract_id
  FROM  contract c
  where c.emp_no =  @Id AND c.leave_date='1900-01-01 00:00:00.000'
 end 

 SET @totalsallary =(select SUM(salary)
from #contracts c,salary s
where s.contract_id = c.contract_id)
return @totalsallary
4

1 回答 1

1

存储过程只能返回数值:

来自MSDN

存储过程可以将整数值返回给调用过程或应用程序。

你想要的是一个输出参数:

ALTER procedure [dbo].[hms_GetEmployeeSalaryByEmpNo]
(
   @Id int,
   @totalsalary decimal(8,2) OUTPUT
)
...
SET @totalsalary =
(
    SELECT SUM(salary)
    FROM #contracts c,salary s
    WHERE s.contract_id = c.contract_id
)

--return @totalsalary  -- not necessary anymore

或标量值函数:

CREATE FUNCTION [dbo].[hms_GetEmployeeSalaryByEmpNo]
(
    @Id int
)
RETURNS DECIMAL(8,2)
AS
....
SET @totalsalary =
(
    SELECT SUM(salary)
    FROM #contracts c,salary s
    WHERE s.contract_id = c.contract_id
)

RETURN @totalsalary

*另请注意,我在示例中更正了“薪水”的拼写。

于 2013-08-29T13:42:23.557 回答