代码改变世界

mssql,检测数据库表是否存在

2008-07-21 11:06  Virus-BeautyCode  阅读(1008)  评论(0编辑  收藏  举报

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'