服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Sql Server - sql 判断函数、存储过程是否存在的代码整理

sql 判断函数、存储过程是否存在的代码整理

2020-03-25 15:05whsnow Sql Server

本文主要为大家介绍了sql下用来判断各种资源是否存在的代码,希望对大家学习sql的函数及数据库能够有所帮助

下面为您介绍sql下用了判断各种资源是否存在的代码,需要的朋友可以参考下,希望对您学习sql的函数及数据库能够有所帮助。

?
1
2
3
4
5
-- 库是否存在
if exists(select * from master..sysdatabases where name=N'库名')
print 'exists'
else
print 'not exists'
?
1
2
3
4
5
-- 判断要创建的表名是否存在
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
-- 删除表
drop table [dbo].[表名]
GO
?
1
2
3
4
5
6
7
8
9
--判断要创建临时表是否存在
If Object_Id('Tempdb.dbo.#Test') Is Not Null
Begin
print '存在'
End
Else
Begin
print '不存在'
End
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-- 判断要创建的存储过程名是否存在
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='表名'

延伸 · 阅读

精彩推荐