下面为您介绍sql下用了判断各种资源是否存在的代码,需要的朋友可以参考下,希望对您学习sql的函数及数据库能够有所帮助。库是否存在if exists(select * from master..sysdatabases where name=N'库名')PRint 'exists'elseprint '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 NullBeginprint '存在'EndElseBeginprint '不存在'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].[函数名]GOif col_length('表名', '列名') is nullprint '不存在'select 1 from sysobjects where id in (select id from syscolumns where name='列名') and name='表名'
本文转自:http://www.bitscn.com/pdb/mssql/201010/191258.html新闻热点
疑难解答