3.14 Oracle之存储过程和MERGE INTO语句
CREATE OR REPLACE PROCEDURE update_emp_salary(p_emp_id IN EMPLOYEES.EMPLOYEE_ID%TYPE, p_new_salary IN EMPLOYEES.SALARY%TYPE) AS
BEGIN
MERGE INTO EMPLOYEES E
USING (SELECT p_emp_id AS emp_id, p_new_salary AS new_salary FROM DUAL) N
ON (E.EMPLOYEE_ID = N.emp_id)
WHEN MATCHED THEN
UPDATE SET E.SALARY = N.new_salary
WHEN NOT MATCHED THEN
INSERT (EMPLOYEE_ID, SALARY) VALUES (N.emp_id, N.new_salary);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END update_emp_salary;
这段代码首先创建或替换一个名为update_emp_salary
的存储过程,它接受两个参数:p_emp_id
(员工ID)和p_new_salary
(新的薪水)。然后使用MERGE INTO
语句来更新EMPLOYEES
表中的员工薪水信息。如果员工ID在EMPLOYEES
表中存在,则更新其薪水;如果不存在,则插入新的记录。如果操作过程中出现异常,例如冲突,它会回滚并重新抛出异常。
评论已关闭