如何解决存储过程在通过 EXECUTE IMMEDIATE 执行时不执行 dbms_stats.gather_table_stats() 的问题
How to resolve issue of stored proc not executing dbms_stats.gather_table_stats() when executing as it string via EXECUTE IMMEDIATE
在我的存储过程中,我使用“EXECUTE IMMEDIATE”来执行字符串格式的语句,但这并没有生成结果。我在下面添加了 proc:
CREATE OR REPLACE PROCEDURE sp_TEST(tablenamestring Varchar2) IS
ownerName VARCHAR2(5000);
sqlString VARCHAR2(2000);
BEGIN
FOR ROW_IDX IN ( SELECT trim(regexp_substr(tablenamestring, '[^,]+', 1, LEVEL)) TABLE_NAME FROM
dual
CONNECT BY LEVEL <= regexp_count(tablenamestring, ',')+1)
LOOP
select owner INTO ownerName from ALL_OBJECTS where object_name = ROW_IDX.TABLE_NAME;
sqlString :='Exec dbms_stats.gather_table_stats(ownname => ''' || ownerName ||''', tabname =>'''||ROW_IDX.TABLE_NAME ||''', estimate_percent =>DBMS_STATS.AUTO_SAMPLE_SIZE, method_opt => '' FOR ALL COLUMNS SIZE AUTO'' , cascade => TRUE, DEGREE=>4)';
--EXECUTE IMMEDIATE sqlString;
dbms_output.put_line(sqlString);
END LOOP;
END;
我评论了 --EXECUTE IMMEDIATE sqlString;并检查了 dbms_output.put_line 显示的 sqlstring。 SQL字符串如下所示,执行成功
Exec dbms_stats.gather_table_stats(ownname => 'NPSQL11', tabname =>'Customer', estimate_percent =>DBMS_STATS.AUTO_SAMPLE_SIZE, method_opt => ' FOR ALL COLUMNS SIZE AUTO' , cascade => TRUE, DEGREE=>4)
如果我取消注释 EXECUTE IMMEDIATE sqlString;并执行程序,我无法执行上述查询。我收到如下所示的错误:
ORA-00900: invalid SQL statement
ORA-06512: at NPSQL11.sp_TEST", line 12
ORA-06512: at NPSQL11.sp_TEST, line 12
ORA-06512: at line 2
begin
sp_TEST('Customer');
end;
我错过了什么吗? EXECUTE IMMEDIATE 用于创建 table 字符串。
EXEC 是 SQL*Plus 命令,而不是 PL/SQL 关键字。
尝试用 BEGIN ...; END;
替换 EXEC ...
在我的存储过程中,我使用“EXECUTE IMMEDIATE”来执行字符串格式的语句,但这并没有生成结果。我在下面添加了 proc:
CREATE OR REPLACE PROCEDURE sp_TEST(tablenamestring Varchar2) IS
ownerName VARCHAR2(5000);
sqlString VARCHAR2(2000);
BEGIN
FOR ROW_IDX IN ( SELECT trim(regexp_substr(tablenamestring, '[^,]+', 1, LEVEL)) TABLE_NAME FROM
dual
CONNECT BY LEVEL <= regexp_count(tablenamestring, ',')+1)
LOOP
select owner INTO ownerName from ALL_OBJECTS where object_name = ROW_IDX.TABLE_NAME;
sqlString :='Exec dbms_stats.gather_table_stats(ownname => ''' || ownerName ||''', tabname =>'''||ROW_IDX.TABLE_NAME ||''', estimate_percent =>DBMS_STATS.AUTO_SAMPLE_SIZE, method_opt => '' FOR ALL COLUMNS SIZE AUTO'' , cascade => TRUE, DEGREE=>4)';
--EXECUTE IMMEDIATE sqlString;
dbms_output.put_line(sqlString);
END LOOP;
END;
我评论了 --EXECUTE IMMEDIATE sqlString;并检查了 dbms_output.put_line 显示的 sqlstring。 SQL字符串如下所示,执行成功
Exec dbms_stats.gather_table_stats(ownname => 'NPSQL11', tabname =>'Customer', estimate_percent =>DBMS_STATS.AUTO_SAMPLE_SIZE, method_opt => ' FOR ALL COLUMNS SIZE AUTO' , cascade => TRUE, DEGREE=>4)
如果我取消注释 EXECUTE IMMEDIATE sqlString;并执行程序,我无法执行上述查询。我收到如下所示的错误:
ORA-00900: invalid SQL statement
ORA-06512: at NPSQL11.sp_TEST", line 12
ORA-06512: at NPSQL11.sp_TEST, line 12
ORA-06512: at line 2
begin
sp_TEST('Customer');
end;
我错过了什么吗? EXECUTE IMMEDIATE 用于创建 table 字符串。
EXEC 是 SQL*Plus 命令,而不是 PL/SQL 关键字。
尝试用 BEGIN ...; END;
EXEC ...