[英]IF ELSE statements to update table in sql procedure does not work
I have a table called Employees and need to update the SalaryId of the Employee by 1 if 1 is inputed and decrease it by -1 in case -1 is inputed. 我有一个表叫员工 ,需要通过1如果1便被输入到更新员工的SalaryId和-1的情况下,减少它-1便被输入。 I think my approach has totally wrong logic, but I cannot find what I wanna do although I hav been searching for a while.
我认为我的方法有完全错误的逻辑,但是尽管我已经搜索了一段时间,但我找不到想要做的事情。 Can someone assist me understand what I am doing wrong?
有人可以协助我了解我在做什么错吗?
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
This doesn't make sense to me: 这对我来说没有意义:
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
Perhaps you want: 也许您想要:
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
You're wildly mixing procedural code ( IF
) and SQL code ( UPDATE
, WHERE
)... 您正在疯狂地混合过程代码(
IF
)和SQL代码( UPDATE
, WHERE
)...
I can imagine you're after something like that: 我可以想象你正在追求这样的事情:
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;
It increases or decreases -- depending on the sign of @Rating
-- the SalaryId
of the employee identified by @PersonalID
by 1
, if that change doesn't cause the SalaryId
to drop below 1
or raise above 7
-- the checks for that belong in the WHERE
clause an not in IF
s spread anywhere in the UPDATE
statement. 它增加或减少-取决于
@Rating
的符号-由SalaryId
标识的员工的@PersonalID
1
,如果该更改不会导致SalaryId
低于1
或提高至7
以上-对此的检查属于WHERE
子句,而不是IF
散布在UPDATE
语句中的任何位置。 To check that only steps of -1
or 1
are possible, the UPDATE
is wrapped in an IF
, that is only entered, when the absolute value of @Rating
equals 1
. 为了检查仅
-1
或1
步长是可能的,将UPDATE
包裹在IF
,当@Rating
的绝对值等于1
时,才将其输入。 You can also remove the IF
, if you want to allow more than 1
step at once. 如果要一次允许超过
1
步骤,也可以删除IF
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.