📄 subject_51398.htm
字号:
<p>
序号:51398 发表者:潘成强 发表日期:2003-09-01 11:15:10
<br>主题:请教oracle高手!急!!!
<br>内容:由于用户下表过多,而好多表是没有数据的空表<BR>问有什么命令可以在sql/plus下显示出这些没有数据的空表???<BR>谢谢高手指点!!!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:Attenborough 回复日期:2003-09-02 10:28:15
<br>内容:这个需求的核心问题是:“表名”是变化的,所以只能用动态sql的方法来实现。<BR>实现动态sql似乎只有2条路可以走<BR>1)如果在d2k中做应用,则可以使用forms_ddl来实现,比较方便<BR>2)如果在sql/plus界面中操作,似乎只有使用DBMS_SQL包来实现,很烦<BR><BR>思路应该是这样的<BR>1.在数据字典中把当前用户下的所有表查出<BR>2.搜索每个表的记录数量<BR>3.把记录数量为0的表drop 掉<BR><BR>难度部分在第二步。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:Attenborough 回复日期:2003-09-02 14:42:30
<br>内容:脚本如下(我调试过了)<BR>在某用户下执行过程 Dbms_sql_test.DropAllEmptyTable 即可。<BR>------------------------------------------------------------------------<BR>CREATE OR REPLACE PACKAGE Dbms_sql_test IS<BR><BR>procedure DropAllEmptyTable;<BR>procedure DropSingleTable(In_table_name varchar2);<BR>function IsTableEmpty(In_table_name varchar2) return boolean;<BR><BR>END;<BR>/<BR><BR><BR><BR>CREATE OR REPLACE PACKAGE BODY Dbms_sql_test IS<BR>/*--package body begin--*/<BR>/*------------------------------------------------------------------------------------------*/<BR>procedure DropAllEmptyTable is /*删除当前用户下的空记录表*/<BR> cursor EmptyTable_cur is select object_name from all_objects<BR> where owner=user and object_type='TABLE';<BR> <BR> <BR> EmptyTable_rec EmptyTable_cur%rowtype;<BR> <BR>begin<BR> open EmptyTable_cur;<BR> loop <BR> fetch EmptyTable_cur into EmptyTable_rec;<BR> exit when EmptyTable_cur%notfound;<BR><BR> if IsTableEmpty(EmptyTable_rec.object_name) then<BR> DropSingleTable(EmptyTable_rec.object_name); /*drop table*/<BR> end if;<BR><BR> end loop;<BR> close EmptyTable_cur;<BR>end;<BR>/*------------------------------------------------------------------------------------------*/<BR>procedure DropSingleTable(In_table_name varchar2) is /*删除某个指定的表*/<BR> v_Cursor number;<BR> v_DropString varchar2(100);<BR>begin<BR> v_Cursor:=DBMS_SQL.Open_Cursor; <BR> v_DropString:='Drop table '||In_table_name;<BR> <BR> begin<BR> DBMS_SQL.Parse(v_Cursor,v_DropString,DBMS_SQL.v7);<BR> <BR> exception<BR> when others then<BR> if sqlcode!=-942 then /*ora-942 : table doesn't exist 当此错误时不报错*/<BR> raise;<BR> end if;<BR> end; <BR><BR> DBMS_SQL.Close_cursor(v_Cursor); <BR>end;<BR><BR>/*------------------------------------------------------------------------------------------*/<BR>function IsTableEmpty(In_table_name varchar2) return boolean is /*是否指定的表为空? true---空 false---非空*/<BR> v_Cursor number;<BR> v_QueryString varchar2(100);<BR> v_NumRows integer;<BR> v_FetchReturn number;<BR> <BR> TableRowCount integer;<BR>begin<BR> v_Cursor:=DBMS_SQL.Open_Cursor; <BR> v_QueryString:='select count(*) from '||In_table_name;<BR><BR> begin<BR> DBMS_SQL.Parse(v_Cursor,v_QueryString,DBMS_SQL.v7);<BR> DBMS_SQL.Define_Column(v_Cursor,1,TableRowCount);<BR> <BR> v_NumRows:=DBMS_SQL.Execute(v_Cursor);<BR> <BR> v_FetchReturn:=DBMS_SQL.fetch_rows(v_Cursor);<BR> <BR> DBMS_SQL.Column_value(v_Cursor,1,TableRowCount);<BR> <BR> DBMS_SQL.Close_cursor(v_Cursor);<BR> <BR> if TableRowCount=0 then<BR> return true;<BR> else<BR> return false;<BR> end if;<BR> <BR> exception<BR> when others then<BR> if sqlcode!=-942 then /*ora-942 : table doesn't exist 当此错误时不报错*/<BR> raise;<BR> else<BR> DBMS_SQL.Close_cursor(v_Cursor);<BR> return false; /*此表不存在则返回 false*/ <BR> end if;<BR> end;<BR><BR>end;<BR><BR>/*--package body end--*/<BR>END;<BR>/
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -