如果在SQL过程中更新表的ELSE语句不起作用

时间:2019-01-15 20:58:19

标签: sql sql-update procedure sqlanywhere

我有一个名为 Employees 的表,如果输入了 1 ,则需要将Employee的SalaryId更新1,如果输入-1,则将其减少-1。 。 我认为我的方法有完全错误的逻辑,但我找不到 尽管我已经搜索了一段时间,但我想做什么。有人可以帮助我了解我在做什么错吗?

ALTER PROCEDURE "dba"."updatePosition"(IN rating int, @PersonalID int )
AS BEGIN
   IF rating = 1 
     UPDATE dba.Employees
      IF (dba.Employees.SalaryId  > 1 && dba.Employees.SalaryId < 7)

      SET dba.Employees.SalaryId = dba.Employees.SalaryId  + 1
      WHERE dba.Employees.PersonalID = @PersonalID

   ELSEIF rating = -1
   UPDATE dba.Employees
   SET dba.Employees.SalaryId  = dba.Employees.SalaryId  - 1
        IF dba.Employees.SalaryId  < 1
           dba.Employees.SalaryId  = 1
        IF dba.Employees.SalaryId  > 7
           dba.Employees.SalaryId = 7
        WHERE dba.Employees.PersonalID = @PersonalID
    END

2 个答案:

答案 0 :(得分:0)

这对我来说没有意义:

 UPDATE dba.Employees
  IF (dba.Employees.SalaryId  > 1 && dba.Employees.SalaryId < 7)

  SET dba.Employees.SalaryId = dba.Employees.SalaryId  + 1
  WHERE dba.Employees.PersonalID = @PersonalID

也许你想要

UPDATE dba.Employees
  SET dba.Employees.SalaryId = dba.Employees.SalaryId  + 1
  WHERE dba.Employees.PersonalID = @PersonalID AND
        dba.Employees.SalaryId  > 1 AND
        dba.Employees.SalaryId < 7

答案 1 :(得分:0)

您正在疯狂地混合过程代码(IF)和SQL代码(UPDATEWHERE)...

我可以想象你正在追求类似的东西:

ALTER PROCEDURE "dba"."updatePosition"(IN @Rating int,
                                       IN @PersonalID int)
AS
BEGIN
  IF abs(@Rating) = 1 THEN
    UPDATE dba.Employees
           SET SalaryId + @Rating
           WHERE PersonalID = @PersonalID
                 AND SalaryId + @Rating >= 1
                 AND SalaryId + @Rating <= 7;
  END IF;
END;

根据@Rating的符号,{p}会增加或减少,SalaryId会由@PersonalID标识为1的员工SalaryId导致1下降到7以下或上升到WHERE以上–属于IF子句的检查不以UPDATE的形式散布到-1语句。为了检查只能执行1UPDATE的步骤,将IF包裹在@Rating中,只有在绝对值{{1}时才输入}等于1。如果您想一次允许执行多个IF步骤,也可以删除1