如何用SQL语句判断SQLserver2000数据库中的表是否存在,有没有这样的函数

wxok 2001-11-03 06:06:01
如何用SQL语句判断SQLserver2000数据库中的表是否存在,有没有这样的函数
...全文
70 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
foolishchao 2001-11-04
  • 打赏
  • 举报
回复
zhuzhichao(竹之草):
1、对象存在
2、对象类型是用户表或系统表
你只判断1,不判断2。这样做思维不严密。
zhuzhichao 2001-11-03
  • 打赏
  • 举报
回复

所有的表都可以用object_id函数判断!

if object_id('tempdb.dbo.#temptable') is null
print 'table not exists'
else
print 'table already exists'
foolishchao 2001-11-03
  • 打赏
  • 举报
回复
MS SQL Server:

临时表要用下面的方法判断
create table #test(name char(8))
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#test') and type='U')
print '#test exists'

用户表和系统表可以表下面的方法判断。
IF OBJECTPROPERTY ( object_id('authors'),'ISTABLE') = 1
print 'Authors is a table'

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[detail]') and OBJECTPROPERTY(id, N'IsTable') = 1)
print 'table [dbo].[detail] esist'

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[detail]') and (type='U' or type='S'))
print 'table [dbo].[detail] esist'

Free_Man 2001-11-03
  • 打赏
  • 举报
回复
select * from sysobjects where id = object_id(table_name)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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