本人有个表tab 有一个字段记录的是存储过程的具体名称,我希望遍历出这个字段,按名称执行存储过程(不存在传参问题)。编译通过,执行时报错:无效的SQL语句。红体字那一行。
CREATE OR REPLACE PROCEDURE SP_LINJIE1 IS
CURSOR CUR_OPT IS
SELECT TB_NAME, STATE, T.STATE2, T.PROCEDURE_NAME FROM V_GRSOURCEVST T;
OPT_VALUE CUR_OPT%ROWTYPE;
BEGIN
OPEN CUR_OPT;
LOOP
FETCH CUR_OPT
INTO OPT_VALUE;
CASE
WHEN OPT_VALUE.STATE = 1 AND OPT_VALUE.STATE2 = 2 THEN
EXECUTE IMMEDIATE OPT_VALUE.PROCEDURE_NAME; ELSE
DBMS_OUTPUT.PUT_LINE('invalid operation');
END CASE;
EXIT WHEN CUR_OPT%NOTFOUND;
END LOOP;
CLOSE CUR_OPT;
END;
------解决方案--------------------
- SQL code
EXECUTE IMMEDIATE OPT_VALUE.PROCEDURE_NAME;--改为 EXECUTE IMMEDIATE 'begin '||OPT_VALUE.PROCEDURE_NAME||'; end;';
------解决方案--------------------
EXECUTE IMMEDIATE OPT_VALUE.PROCEDURE_NAME;
应该为
EXECUTE IMMEDIATE 'call '||OPT_VALUE.PROCEDURE_NAME||'()';
------解决方案--------------------
CREATE OR REPLACE PROCEDURE SP_LINJIE1 IS
CURSOR CUR_OPT IS
SELECT TB_NAME, STATE, T.STATE2, T.PROCEDURE_NAME FROM V_GRSOURCEVST T;
OPT_VALUE CUR_OPT%ROWTYPE;
vi_out pls_integer;
BEGIN
OPEN CUR_OPT;
LOOP
FETCH CUR_OPT
INTO OPT_VALUE;
EXECUTE IMMEDIATE 'begin '|| OPT_VALUE.PROCEDURE_NAME ||'(:1); end;' using out :vi_out;
EXIT WHEN CUR_OPT%NOTFOUND;
END LOOP;
CLOSE CUR_OPT;
END;