如何判断表已存在?

tongguanyi 2006-03-06 11:24:51
if 表存在
begin
drop table 表
end
...全文
233 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Snow_Ice11111 2006-03-13
  • 打赏
  • 举报
回复
mark
runtoflower 2006-03-12
  • 打赏
  • 举报
回复
明白你的意思:直接DROP TABLE 就行了,不用判断是否存在。然后该做什么就做什么即可。
lane_cn 2006-03-07
  • 打赏
  • 举报
回复
select一下,然后捕捉异常。

try
{
execute(select * from tablename where 1=0)
}
catch (Exception e)
{
//没有表
}
//有表
he_sl 2006-03-07
  • 打赏
  • 举报
回复
/***************************关键词******************************
1、获取所有用户表
(1)SQL Server数据库:select name as Name from sysobjects where xtype = 'U' and name != 'dtproperties' order by Name
(2)Oracle数据库:select table_name as Name from user_all_tables order by Name
(3)DB2数据库:select Name from sysibm.systables where CREATOR = '登录用户' AND TYPE = 'T' order by Name

2、获取指定表的所有字段信息
(1)SQL Server数据库:select a.name as Name from syscolumns a inner join sysobjects b on a.id = b.id where b.name = '表名' order by Name
(2)Oracle数据库:select column_name as Name from user_tab_columns where table_name = '表名' order by Name
(3)DB2数据库:select Name from sysibm.syscolumns where TBName = '表名' order by Name
myh0305 2006-03-07
  • 打赏
  • 举报
回复
select table_name from user_tables
上面的sql语句可以查看库中有多少表
一条晚起的虫 2006-03-07
  • 打赏
  • 举报
回复
读系统表,里面有数据库中所有的表名、视图等信息
ms sqlserver,楼上的正解
syy64 2006-03-06
  • 打赏
  • 举报
回复
BOOL CDlgRecordset::GetMicTable()
{
try
{
m_pRecordset=theApp.m_pConnection->OpenSchema(adSchemaTables);

while(!(m_pRecordset->adoEOF))
{
//»ñÈ¡±í¸ñ
_bstr_t table_name = m_pRecordset->Fields->GetItem("TABLE_NAME")->Value;
//»ñÈ¡±í¸ñÀàÐÍ
_bstr_t table_type = m_pRecordset->Fields->GetItem("TABLE_TYPE")->Value;
//¹ýÂËһϣ¬Ö»Êä³ö±í¸ñÃû³Æ£¬ÆäËûµÄÊ¡ÂÔ
if ( strcmp(((LPCSTR)table_type),"TABLE")==0)
{
m_cmbTable.AddString((LPCSTR)table_name);
}
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
}
catch(_com_error e)///²¶×½Òì³£
{
::MessageBox(NULL,e.Description(),"Ìáʾ",MB_OK);
return FALSE;
}

return true;

}

得到数据库的所有表名后,跟你给的表名进行判断。
jinxuliang 2006-03-06
  • 打赏
  • 举报
回复
我想楼主不是想要那么复杂的东西吧
select * from sysobjects where name='你的表名' and xtype='u'
执行就ok 了

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧