--数据库是否存在 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 Object_Id(‘Tempdb.dbo.#Test‘) Is Not Null Begin PRINT ‘存在‘ End Else Begin PRINT ‘不存在‘ END --------------- -- 判断要创建的存储过程名是否存在 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 dbo.sysobjects WHERE id = object_id(N‘[dbo].[函数名]‘) and xtype in (N‘FN‘, N‘IF‘, N‘TF‘)) -- 删除函数 DROP FUNCTION [dbo].[函数名] GO IF col_length(‘表名‘, ‘列名‘) is null PRINT ‘不存在‘ SELECT 1 FROM sysobjects WHERE id in (SELECT id FROM syscolumns WHERE name=‘列名‘) and name=‘表名‘
转载:http://www.bitscn.com/pdb/mssql/201010/191258.html
时间: 2024-10-10 18:40:47