SQL Server sql 判断数据库,表,存储过程等是否存在的代码
2022-11-12 09:53:46
内容摘要
这篇文章主要为大家详细介绍了SQL Server sql 判断数据库,表,存储过程等是否存在的代码,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
代
文章正文
这篇文章主要为大家详细介绍了SQL Server sql 判断数据库,表,存储过程等是否存在的代码,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
代码:
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <code> --库是否存在 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 COL_LENGTH( '表名' , '列名' ) IS NULL PRINT 'not exists' ELSE PRINT 'exists' alter table 表名 drop constraint 默认值名称 go alter table 表名 drop column 列名 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 sysobjects where xtype= 'fn' and name= '函数名' ) 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= '表名' </code> |
sql判断是否存在
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <code> --判断数据库是否存在 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= '表名' </code> |
注:关于SQL Server sql 判断数据库,表,存储过程等是否存在的代码的内容就先介绍到这里,更多相关文章的可以留意
代码注释