3 lines
2.0 KiB
HTML
3 lines
2.0 KiB
HTML
<p>下面为您介绍<a href="http://www.bitscn.com/pdb/" target="_blank">sql</a>下用了判断各种资源是否存在的代码,需要的朋友可以参考下,希望对您学习<a href="http://www.bitscn.com/pdb/" target="_blank">sql</a>的函数及数据库能够有所帮助。<br />库是否存在<br />if exists(select * from master..sysdatabases where name=N'库名')<br />print 'exists'<br />else<br />print 'not exists'<br />---------------<br />-- 判断要创建的表名是否存在<br />if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)<br />-- 删除表<br />drop table [dbo].[表名]<br />GO<br />---------------<br />--判断要创建临时表是否存在<br />If Object_Id('Tempdb.dbo.#Test') Is Not Null<br />Begin<br />print '存在'<br />End<br />Else<br />Begin<br />print '不存在'<br />End<br />---------------<br />-- 判断要创建的存储过程名是否存在<br />if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[存储过程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)<br />-- 删除存储过程<br />drop procedure [dbo].[存储过程名]<br />GO<br />---------------<br />-- 判断要创建的视图名是否存在<br />if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[视图名]') and OBJECTPROPERTY(id, N'IsView') = 1)<br />-- 删除视图<br />drop view [dbo].[视图名]<br />GO<br />---------------<br />-- 判断要创建的函数名是否存在<br />if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[函数名]') and xtype in (N'FN', N'IF', N'TF'))<br />-- 删除函数<br />drop function [dbo].[函数名]<br />GO<br />if col_length('表名', '列名') is null<br />print '不存在'<br />select 1 from sysobjects where id in (select id from syscolumns where name='列名') and name='表名'</p>
|
|
<div> </div>
|
|
<div>本文转自:<a href="http://www.bitscn.com/pdb/mssql/201010/191258.html">http://www.bitscn.com/pdb/mssql/201010/191258.html</a></div> |