基本上我的更新sql查询列名称将是动态的,如
update bi_employee set <this_is_dynamic_column> where emp_id = 12
以下是我到目前为止所写的存储过程。
CREATE OR replace PROCEDURE Sp_run_employee_updates
IS
CURSOR c_emp IS
SELECT *
FROM BI_EMPLOYEE_UPDATE
WHERE EFFECTIVE_DATE = To_date('30-Apr-2012', 'dd-mm-yy');
BEGIN
FOR employee_update IN c_emp LOOP
declare update_sql varchar2(225);
update_sql := 'UPDATE BI_EMPLOYEE SET '
|| employee_update.column_name
|| '= employee_update.new_value WHERE emp_id = '
|| employee_update.employee_id;
END LOOP;
END;
它给了我相应的错误
错误(17,13):PLS-00103:遇到以下其中一项时遇到符号“=”:常量异常表long double ref char time timestamp interval date二进制国家字符nchar符号“”代替“ =“继续。
错误(22,5):PLS-00103:遇到以下其中一项时遇到符号“UPDATE”:begin function pragma procedure子类型当前游标删除存在先前符号“begin”替换为“UPDATE”继续。
错误(31,6):PLS-00103:遇到符号“;”期待以下之一:loop
答案 0 :(得分:3)
a-这应该是这样的:
to_date('30-Apr-2012','dd-mon-yyyy');
b-你可以这样做:
CREATE OR REPLACE
PROCEDURE SP_RUN_EMPLOYEE_UPDATES IS
update_sql varchar2(225);
CURSOR c_emp IS
SELECT *
FROM BI_EMPLOYEE_UPDATE
WHERE EFFECTIVE_DATE = to_date('30-Apr-2012','dd-mon-yyyy');
BEGIN
FOR employee_update in c_emp LOOP
update_sql := 'UPDATE BI_EMPLOYEE SET ' || employee_update.column_name ||
'= :1 WHERE emp_id = :2' ;
execute immediate update_sql using employee_update.new_value, employee_update.employee_id;
END LOOP;
END SP_RUN_EMPLOYEE_UPDATES;