select * from sys.objects
select * from sys.all_objects
删表:
if exitst (select * from sys.objects where name in ('test') and type='U' and type_desc='USER_TABLE')
drop table table_name
go
删存储过程:
if exitst (select * from sys.objects where name in ('proc_name') and type='P' and type_desc='SQL_STORED_PROCEDURE')
drop table proc_name
go
type | type_desc | 说明 |
AF | AGGREGATE_FUNCTION | 聚合函数(CLR) |
C | CHECK_CONSTRAINT | CHECK 约束 |
D | DEFAULT_CONSTRAINT | DEFAULT(约束或独立) |
F | FOREIGN_KEY_CONSTRAINT | 外键约束 |
FC | CLR_SCALAR_FUNCTION | 程序集 (CLR) 标量函数 |
FN | SQL_SCALAR_FUNCTION | SQL 标量函数 |
FT | CLR_TABLE_VALUED_FUNCTION | 程序集 (CLR) 表值函数 |
IF | SQL_INLINE_TABLE_VALUED_FUNCTION | SQL 内联表值函数 |
IT | INTERNAL_TABLE | 内部表 |
P | SQL_STORED_PROCEDURE | SQL 存储过程 |
PC | CLR_STORED_PROCEDURE | 程序集 (CLR) 存储过程 |
PK | PRIMARY_KEY_CONSTRAINT | 主键约束 |
S | SYSTEM_TABLE | 系统基表 |
SQ | SERVICE_QUEUE | 服务队列 |
TF | SQL_TABLE_VALUED_FUNCTION | SQL 表值函数 |
TR | SQL_TRIGGER | SQL 触发器 |
U | USER_TABLE | 用户表 |
UQ | UNIQUE_CONSTRAINT | UNIQUE 约束 |
V | VIEW | 视图 |
X | EXTENDED_STORED_PROCEDURE | 扩展存储过程 |
--判断指定的数据库是否存在,存在则删除
if exists (select name from master..sysdatabases where name in ('db_name'))
drop database db_name
if exists (select * from sys.sysdatabases where name in ('db_name')
drop database db_name
--判断指定的存储过程是否存在,存在则删除
if exists (select * from sysobjects where objectproperty(object_id('proc_name'), 'IsProcedure')=1)
drop procedure proc_name
if exists (select * from sys.procedures where name in ('proc_name')
drop procedure proc_name
--判断指定的表是否存在,存在则删除
if exists (select * from sysobjects where objectproperty(object_id('table_name'),'istable')=1)
drop table table_name
if exists (select * from sys.tables where name like 'table_name')
drop table table_name
--判断指定的自定义函数是否存在,存在则删除
if exists (select * from sysobjects where objectproperty(object_id('dbo.func_name'), 'isansinullson')=1)
drop function dbo.func_name
--判断指定的临时表是否存在,存在则删除
if exists (select * from tempdb..sysobjects where name like '#table_name%')
drop table #table_name
--判断指定的视图是否存在,存在则删除
if exists (select * from sys.views where name like 'view_name')
drop view view_name