如何在mysql存储过程中进行arithemetic操作?

时间:2015-07-23 11:26:49

标签: mysql sql stored-procedures

我试图在MySQL存储过程中进行一些算术运算。

declare socSecAmt as double;
SET status_flag=0;
SET socSecAmt = (socSec/100)*fixedsal;

但上述失败。 socSecfixedsal作为从调用存储过程的语句中获取值的变量。

完整的SP代码。

DELIMITER $$

DROP PROCEDURE IF EXISTS `pro_empsalaryinfo_dao` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `pro_empsalaryinfo_dao`(

IN salid        varchar(15),
IN empcode      varchar(12),
IN empname      varchar(75),
IN empgender    varchar(8),
IN empage       varchar(5),
IN salarytype   varchar(10),
IN saldate      date,
IN salmonth     varchar(5),
IN salyear      varchar(4),
IN paidamt      double(10,2),
IN paiddetails  text,
IN advancepaid  double(10,2),
IN balamt       double(10,2),
IN fixedsal     double(10,2),
IN socSec       double(5, 1),
IN functionality  varchar(10),
OUT status_flag    INT
)
BEGIN
declare socSecAmt as double;

SET status_flag=0;
SET socSecAmt = (socSec/100)*fixedsal;



IF(functionality='save') THEN

insert into emp_salary_info (sal_id,emp_code,emp_name,gender,age,salary_type,sal_date,paid_amt,paid_details,
sal_month,sal_year,advance_paid,bal_amt,fixed_sal,soc_sec,soc_sec_amt)values
(salid,empcode,empname,empgender,empage,salarytype,saldate,paidamt,paiddetails,
salmonth,salyear,advancepaid,balamt,fixedsal,socSec,socSecAmt);

SET status_flag=1;

elseif(functionality='update') then

UPDATE emp_salary_info set sal_id=salid, emp_code=empcode, emp_name=empname, gender=empgender, age=empage, salary_type=salarytype, sal_date=saldate, paid_amt=paidamt, paid_details=paiddetails, 
sal_month=salmonth, sal_year=salyear, advance_paid=advancepaid, bal_amt=balamt, fixed_sal=fixedsal,soc_sec = socSec,soc_sec_amt=socSecAmt, esal_flag_id=0 where sal_id = salid and esal_flag_id='0';

SET status_flag=2;

else
SET status_flag=0;

end if;

END $$

DELIMITER ;

运行上述代码后显示的错误消息是

Script line: 4  You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as double;

SET status_flag=0;
SET socSecAmt = (socSec/100)*fixedsal;

SET ' at line 22

2 个答案:

答案 0 :(得分:0)

您正在分配错误的变量。 socSecAmt@socSecAmt是不同的事情。

也许你想要:

declare @socSecAmt as double;
SET status_flag=0;
SET @socSecAmt = (socSec/100)*fixedsal;

答案 1 :(得分:0)

最后,我通过将变量socSecAmt声明为Decimal

来解决它
DECLARE socSecAmt DECIMAL(10,2) DEFAULT 0;

SET status_flag=0;
SET socSecAmt = (socSec/100)*fixedsal;