欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

判断表存在

程序员文章站 2024-02-03 13:01:46
...
if exists(select * from sysobjects 
	where name= '表名'
	and type = 'U')
drop table 表名
go


临时表要用下面的方法判断
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'
相关标签: SQL Go