问题
While trying to create table via procedure, I am facing error like
Error(73,9): PLS-00157: AUTHID only allowed on schema-level programs
PROCEDURE BCKUP
AUTHID CURRENT_USER AS
statusmsg VARCHAR2(400);
BEGIN
--Backup records
EXECUTE IMMEDIATE 'CREATE TABLE schemaname.tabname AS SELECT pgm.* FROM XYZ pgm, IJK prf WHERE prf.col1=pgm.col1 AND prf.ID IN(SELECT ID FROM TAB2)';
COMMIT;
EXCEPTION WHEN OTHERS THEN
statusmsg :='SQL ERRROR CODE ' || SQLCODE|| ' -ERROR- ' ||SQLERRM;
dbms_output.put_line('ERROR : ' || statusmsg);
END BCKUP;
回答1:
Your procedure seems to part of a package. In a package, you can only set invoker rights (AUTHID CURRENT_USER
) at the package level. You cannot set it for each individual procedure.
Either move the invoker rights to the package level:
CREATE OR REPLACE PACKAGE pkg
AUTHID CURRENT_USER
AS
PROCEDURE BCKUP;
-- more types, procedures and functions
END pkg;
CREATE OR REPLACE PACKAGE BODY pkg
AS
PROCEDURE BCKUP
statusmsg VARCHAR2(400);
BEGIN
--Backup records
EXECUTE IMMEDIATE 'CREATE TABLE schemaname.tabname AS SELECT pgm.* FROM XYZ pgm, IJK prf WHERE prf.col1=pgm.col1 AND prf.ID IN(SELECT ID FROM TAB2)';
COMMIT;
EXCEPTION WHEN OTHERS THEN
statusmsg :='SQL ERRROR CODE ' || SQLCODE|| ' -ERROR- ' ||SQLERRM;
dbms_output.put_line('ERROR : ' || statusmsg);
END BCKUP;
END pkg;
Or create a global procedure (i.e. at schema level and not at package level):
CREATE OR REPLACE PROCEDURE BCKUP
AUTHID CURRENT_USER AS
statusmsg VARCHAR2(400);
BEGIN
--Backup records
EXECUTE IMMEDIATE 'CREATE TABLE schemaname.tabname AS SELECT pgm.* FROM XYZ pgm, IJK prf WHERE prf.col1=pgm.col1 AND prf.ID IN(SELECT ID FROM TAB2)';
COMMIT;
EXCEPTION WHEN OTHERS THEN
statusmsg :='SQL ERRROR CODE ' || SQLCODE|| ' -ERROR- ' ||SQLERRM;
dbms_output.put_line('ERROR : ' || statusmsg);
END BCKUP;
来源:https://stackoverflow.com/questions/39113926/oracle-procedure-authid-allowed-only-in-schema-level