--1.判断数据库是否存在 IF EXISTS ( SELECT * FROM sys.databases WHERE NAME = '[数据库名]' ) DROP DATABASE [数据库名] --2.判断表是否存在 IF EXISTS ( SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[表名]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1 ) DROP TABLE [表名] --3.判断存储过程是否存在 IF EXISTS ( SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[存储过程名]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1 ) DROP PROCEDURE [存储过程名] --4.判断函数是否存在 IF OBJECT_ID(N'函数名') IS NOT NULL DROP FUNCTION dnt_split ---5.判断数据库是否开启了全文搜索 SELECT DATABASEPROPERTY('数据库名', 'isfulltextenabled') --6.判断全文目录是否存在 SELECT * FROM sysfulltextcatalogs WHERE NAME = '全文目录名称' --7.判断要创建临时表是否存在 IF OBJECT_ID('临时表') IS NOT NULL BEGIN PRINT '存在' END ELSE BEGIN PRINT '不存在' END -- 8.判断要创建的视图名是否存在 IF EXISTS ( SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[视图名]') AND OBJECTPROPERTY(id, N'IsView') = 1 ) DROP VIEW [dbo].[视图名]
--9.判断table1中是否存在name字段 IF EXISTS( SELECT * FROM syscolumns WHERE id = OBJECT_ID('table1') AND NAME = 'name' ) BEGIN SELECT * FROM table1; END --10.判断table1中是否存在name字段且删除字段 IF EXISTS( SELECT * FROM syscolumns WHERE id = OBJECT_ID('table1') AND NAME = 'name' ) BEGIN SELECT * FROM table1; ALTER TABLE table1 DROP COLUMN NAME END