同事在問,怎麼在PACKAGE中使用TRUNCATE語法,以下就三種方法提供大家參考,也可以用於一般DDL語法,至於那種好用就由自己決定:
1.Execute Immediate
16:05:16 system@DB> create table test1(A1 varchar2(100));
已建立表格.
16:05:30 system@DB> insert into test1 select OBJECT_NAME from dba_objects;
已建立 54083 個資料列.
16:05:51 system@DB> commit;
確認完成.
16:07:33 system@DB> select count(*) from test1;
COUNT(*)
----------
54083
已選取 1 個資料列.
16:07:40 system@DB> begin truncate table test1; end;
16:07:50 2 /
begin truncate table test1; end;
*
ERROR 在行 1:
ORA-06550: line 1, column 16:
PLS-00103: Encountered the symbol "TABLE" when expecting one of the following:
:= . ( @ % ;
The symbol ":= was inserted before "TABLE" to continue.
16:07:51 system@DB> begin EXECUTE IMMEDIATE 'TRUNCATE TABLE TEST1'; end;
16:08:25 2 /
已順利完成 PL/SQL 程序.
16:08:26 system@DB> select count(*) from test1;
COUNT(*)
----------
0
已選取 1 個資料列.
2.使用dbms_utility
16:25:23 system@DB> insert into test1 select OBJECT_NAME from dba_objects;
已建立 54083 個資料列.
16:25:29 system@DB> commit;
確認完成.
16:25:36 system@DB> begin dbms_utility.exec_ddl_statement('TRUNCATE TABLE TEST1'); end;
16:26:20 2 /
已順利完成 PL/SQL 程序.
16:26:21 system@DB> select count(*) from test1;
COUNT(*)
----------
0
已選取 1 個資料列.
16:26:33 system@DB> commit;
確認完成.
3.使用DBMS_SQL
17:14:52 system@DB> insert into test1 select OBJECT_NAME from dba_objects;
已建立 54083 個資料列.
17:14:59 system@DB> commit;
確認完成.
17:15:02 system@DB> declare
17:21:31 2 v_cursor number;
17:21:31 3 v_string varchar2(200);
17:21:31 4 v_row number;
17:21:31 5 begin
17:21:31 6 v_cursor:=dbms_sql.open_cursor;
17:21:31 7 v_string:='TRUNCATE TABLE TEST1';
17:21:31 8 dbms_sql.parse(v_cursor,v_string,dbms_sql.native);
17:21:31 9 v_row:=dbms_sql.execute(v_cursor);
17:21:31 10 dbms_sql.close_cursor(v_cursor);
17:21:31 11 exception
17:21:31 12 when others then
17:21:31 13 dbms_sql.close_cursor(v_cursor);
17:21:31 14 raise;
17:21:31 15 end;
17:21:32 16 /
已順利完成 PL/SQL 程序.
17:21:33 system@DB> select count(*) from test1;
COUNT(*)
----------
0
已選取 1 個資料列.
沒有留言:
張貼留言