if exists(select * from master..sysdatabases where name=N'库名')
print 'exists'
else
print 'not exists'
-
判断对应数据库中表名是否存在(使用的时候注意进入到对应的数据库中)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
-- 删除表
drop table [dbo].[表名]
GO
IF COL_LENGTH( '表名','列名') IS NULL
PRINT 'not exists'
ELSE
PRINT 'exists'
alter table 表名 drop constraint 默认值名称 --删除列对应的默认值
go
alter table 表名 drop column 列名
go
If Object_Id('Tempdb.dbo.#Test') Is Not Null
print '存在'
Else
print '不存在'
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[存储过程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
-- 删除存储过程
drop procedure [dbo].[存储过程名]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[视图名]') and OBJECTPROPERTY(id, N'IsView') = 1)
--删除视图
drop view [dbo].[视图名]
GO
if exists (select * from sysobjects where xtype='fn' and name='函数名')
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[函数名]') and xtype in (N'FN', N'IF', N'TF'))
-- 删除函数
drop function [dbo].[函数名]
GO