在存储过程中截断表

问题描述:

当我在Oracle shell中运行以下命令时,它工作正常

When I run the following in an Oracle shell it works fine

truncate table table_name

但是当我尝试将其放入存储过程中

But when I try to put it in a stored procedure

CREATE OR REPLACE PROCEDURE test IS
BEGIN
    truncate table table_name;
END test;
/

失败

ERROR line 3, col 14, ending_line 3, ending_col 18, Found 'table', Expecting:  @   ROW  or   (   or   .   or   ;   :=

为什么?

Oracle PL/SQL中的所有DDL语句均应在该语句之前使用立即执行".因此,您应该使用:

All DDL statements in Oracle PL/SQL should use Execute Immediate before the statement. Hence you should use:

execute immediate 'truncate table schema.tablename';