我需要在存储过程中动态创建和执行查询。
我有两个表用户和 user_updates。
员工表有emp_id, username, division, product, region, title etc.
Employee_updates 有类似的列emp_id, effective_date, column_name, new_value etc.
基本上这就是我想要做的。
获取给定生效日期在 user_udates 表中具有更新的所有员工。
循环遍历每个员工。
获取给定生效日期每位员工的所有更新。一个员工在employee_updates 表中可能有一个或多个更新。
根据这些更新创建动态“更新”查询,例如
update employee set col1 = new_val_1, col2 = new_val_2 where emp_id = ?
这是我到目前为止所做的
create or replace
PROCEDURE SP_RUN_EMPLOYEE_UPDATES
(
IN_DATE IN DATE
)
IS
update_sql varchar2(225);
employee_id BI_EMPLOYEE_UPDATE.employee_id%TYPE;
CURSOR employees
IS SELECT distinct(employee_id)
FROM BI_EMPLOYEE_UPDATE
WHERE EFFECTIVE_DATE = to_date(IN_DATE,'dd-mm-yy')
AND EXECUTED = 'N' AND ACTIVITY_ID = '0'
;
CURSOR e_updates
IS SELECT *
FROM BI_EMPLOYEE_UPDATE
WHERE EFFECTIVE_DATE = to_date(IN_DATE,'dd-mm-yy')
AND EXECUTED = 'N'
AND ACTIVITY_ID = '0'
and employee_id = employee_id ;
BEGIN
OPEN employees;
LOOP
FETCH employees into employee_id;
EXIT WHEN employees%NOTFOUND;
FOR e_update in e_updates
update_sql := 'UPDATE BI_EMPLOYEE SET ';
LOOP
-- create dynam,ic update statment
UPDATE BI_EMPLOYEE_UPDATE
SET EXECUTED = 'Y'
WHERE EMPLOYEE_UPDATE_ID = e_update.EMPLOYEE_UPDATE_ID ;
END LOOP;
-- run dynamic sql
END LOOP;
CLOSE employees;
END;
请帮忙。