我希望创建一个可以调用的存储过程,以便按传入的数量增加工作人员的工资。首先应创建一个保存点,然后检查新工资是否低于设定的阈值并回滚到保存点。它高于门槛。
-- Raise the Salary of the Staff memeber passed in by the amount specified
CREATE OR REPLACE PROCEDURE RAISE (Selection IN Staff.StaffID%TYPE, amount IN Number)
IS
BEGIN
SAVEPOINT save;
UPDATE Staff
-- increase the salary by amount
SET Staff.Salary = (Staff.Salary + amount)
-- Where StaffID is the one passed in
WHERE Staff.StaffID = Selection;
-- If the new value for salary is greater than 100000
IF ( (Staff.Salary) > 100000 )
-- Rollback and undo the changes
THEN ROLLBACK TO save;
-- Print out a message stating this is against business rules etc...
END IF;
END;
/
上面的代码给了我以下错误:
我用(:NEW.Salary > 100000)
尝试了上面的代码但是这给了我一个关于New.Salary的错误绑定错误。
有没有人有任何建议?我想我最有可能通过在员工表上更改前触发来复制此功能,但为什么上述代码不起作用?
谢谢!
答案 0 :(得分:0)
我最终使用一个程序来创建保存并提高工资,然后是一个函数,它检查传入的工资与阈值并返回0或1到程序,然后回滚到之前的保存如果有必要的话。
-- Raise the Salary of the Staff memeber passed in by the amount specified
CREATE OR REPLACE PROCEDURE Raise1 (Selection IN Staff.StaffID%TYPE, amount IN Number)
-- Procedure that takes in StaffID and an amount to increase the staff salary by
IS
BEGIN
SAVEPOINT save;
UPDATE Staff
-- increase the salary by amount
SET Staff.Salary = (Staff.Salary + amount)
-- Where StaffID is the one passed in
WHERE Staff.StaffID = Selection;
-- Pass control to the chekraise procedure, which will rollback to the
-- SAVEPOINT created above if the wage is above 100000 in accordance with business rules
IF ( CheckRaise(Selection) = 0)
THEN
ROLLBACK TO save;
DBMS_OUTPUT.PUT_LINE('Reverted Raise as wage exceeded 100000.');
END IF;
END;
/
CREATE OR REPLACE Function CheckRaise (Selection IN Staff.StaffID%TYPE)
RETURN NUMBER
IS
-- Create a variable, to hold the salary we're checking
Sal NUMBER(10);
BEGIN
-- Select the Staff.Salary that was just passed in into the sal variable
SELECT Staff.Salary INTO Sal FROM Staff WHERE Staff.StaffID = Selection;
-- If sal is above threshold
IF (Sal >= 100000)
THEN
-- Return 0, which the procedure will interpret as "Rollback"
RETURN 0;
ELSIF (Sal < 100000)
THEN
RETURN 1;
END IF;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001, 'Uh oh Error - '||SQLCODE||' Unexpected error '||SQLERRM);
END;
/