我如何创建存储过程更新只有一组行...........?

时间:2014-10-04 14:35:42

标签: mysql stored-procedures

DELIMITER $$
CREATE PROCEDURE  update_salary()   
BEGIN
UPDATE employee SET salary = salary*1.4 where e_desc =  'Senior Manager';

UPDATE employee SET salary = salary*1.2 where e_desc = 'Manager';

UPDATE employee SET salary = salary*1.1 where e_desc = 'Assistant manager';

End $$
DELIMITER ;

我为My_sql创建存储过程以更新表值。它正在工作,但是,我想更改它以仅更新一组行。

e.g。当我给经理'仅更新该列。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以使用存储过程中的IF语句执行此操作,例如..

DELIMITER $$
CREATE PROCEDURE  update_salary(IN employeeType VARCHAR(255))   
BEGIN
IF (employeeType = "Seinor Manager") THEN
    UPDATE employee SET salary = salary*1.4 where e_desc =  'Senior Manager';
ELSEIF (employeeType = "Manager") THEN
    UPDATE employee SET salary = salary*1.2 where e_desc = 'Manager';
ELSE
    UPDATE employee SET salary = salary*1.1 where e_desc = 'Assistant manager';
END IF;
End $$
DELIMITER ;

或者如果套件更好

,你也可以使用案例陈述
DELIMITER $$
CREATE PROCEDURE  update_salary(IN employeeType VARCHAR(255))   
BEGIN
CASE employeeType
  WHEN "Seinor Manager") 
    THEN UPDATE employee SET salary = salary*1.4 where e_desc =  'Senior Manager';
  WHEN "Manager" 
    THEN UPDATE employee SET salary = salary*1.2 where e_desc = 'Manager';
  ELSE 
    UPDATE employee SET salary = salary*1.1 where e_desc = 'Assistant manager';
END;
End $$
DELIMITER ;