Sql Server中判断表或者数据库是否存在

转发自:http://hi.baidu.com/sdhhtfly/item/992ad0d87aa44bfcca0c3973

Sql Server中判断表或者数据库是否存在

1.数据库

if exists(select 1 from master..sysdatabases where name='example')
print 'DataBase existed'
else
print 'Database not existed'

2.表

IF Exists(Select 1 From sysObjects Where Name ='表名' And Type In ('S','U'))
Print 'Exists Table'
Else
Print 'Not Exists Table'

在Sql Server2005中可以简化上述语句

如:

use example

go

if object_id('a','U') is not null

drop table a

go

注:a 是一个表,U代表是数据表类型

类似于U的类型代码,如下所示

对象类型:

AF = 聚合函数 (CLR)

C = CHECK 约束

D = DEFAULT(约束或独立)

F = FOREIGN KEY 约束

PK = PRIMARY KEY 约束

P = SQL 存储过程

PC = 程序集 (CLR) 存储过程

FN = SQL 标量函数

FS = 程序集 (CLR) 标量函数

FT = 程序集 (CLR) 表值函数

R = 规则(旧式,独立)

RF = 复制筛选过程

SN = 同义词

SQ = 队列

TA = 程序集 (CLR) DML

TR = SQL DML 触发器

IF = SQL 内联表值函数

TF = SQL 表值函数

U = 表(用户定义类型)

UQ = UNIQUE 约束

V = 视图

X = 扩展存储过程

IT = 内部表

原文地址:https://www.cnblogs.com/middlesummer/p/3754922.html