如何判断数据库中的一个表是否存在(在线等)(10分)

T

tjlys

Unregistered / Unconfirmed
GUEST, unregistred user!
如何判断数据库中的一个表是否存在
 
什么数据库?
 
微软SQL:
if exists (select * from sysobjects where id = object_id('表') and sysstat & 0xf = 3)
drop table 表
 
如果不是SQL Server的系統表,可以使用以下方法
if exists(select name from sysobjects where xtype='U' and name='表名')

用select name 是為了減少網絡流量,而xtype='U'表明是用戶自已建立的表。
 
//得到可用的表到TListstring对象
procedure TDtMdlACC.GetTablesToLstStr(SndLstStr:TStrings);
var
//零时记录集对象
tmpRst: olevariant;
i:integer;
begin

//创建零时记录集对象
tmpRst:= CreateOleObject('ADODB.RecordSet');

try
//将可用表信息放入TmpRSt
tmpRst:= myCon.OpenSchema(adSchemaTables);

//将信息放入TStringList对象
while not tmprst.EOF do
begin
//如果是用户表就放在ListBox中
If tmprst.Fields['TABLE_TYPE'].value = 'TABLE' Then
begin
SndLstStr.add(tmprst.Fields['TABLE_NAME'].value);
end;
tmprst.MoveNext;
end;

except
showmessage('请检查是否打开与数据库的连接');
end;

//关闭tmprst
if tmprst.state=1 then
begin
tmprst.close;
end;

end;

别忘了:
Uses
ComObj,ActiveX;
 
我用的是ORACLE数据库
TO who am i?:使用你说的方法提示表或视图不存在 sysobjects
 
先通过你的控件获取数据库全部表的名称并存储到一个TSTRINGLIST变量中,再拿你的表名
去比较(字符串比较)。
 
TO ieiszwxm:
如何得到全部表的表名称呢
 
oracle自己有一些数据字典,到all_objects或user_objects视图中去找
 
笨办法:
try
select count(*) from table
except
报表不存在
end;
 
谢谢各位了
在user_tables中就可以找到
 
加一个TSession组件,用该组件的GetTableNames属性可获得所有的表名。然后在所有的表
中查找你要的表名。
 
顶部