sql怎么判断数据库、表、存储过程等是否存在

52次阅读
没有评论

共计 2634 个字符,预计需要花费 7 分钟才能阅读完成。

这篇文章主要介绍“sql 怎么判断数据库、表、存储过程等是否存在”,在日常操作中,相信很多人在 sql 怎么判断数据库、表、存储过程等是否存在问题上存在疑惑,丸趣 TV 小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”sql 怎么判断数据库、表、存储过程等是否存在”的疑惑有所帮助!接下来,请跟着丸趣 TV 小编一起来学习吧!

代码:

-- 库是否存在 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 COL_LENGTH(  表名 , 列名) IS NULL PRINT  not exists ELSE PRINT  exists alter table  表名  drop constraint  默认值名称 goalter table  表名  drop column  列名 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 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].[函数名]GOif col_length(表名 ,  列名) is nullprint  不存在 select 1 from sysobjects where id in (select id from syscolumns where name= 列名) and name= 表名 

sql判断是否存在

-- 判断数据库是否存在  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= 表名 

到此,关于“sql 怎么判断数据库、表、存储过程等是否存在”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注丸趣 TV 网站,丸趣 TV 小编会继续努力为大家带来更多实用的文章!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-01发表,共计2634字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)