当前位置:Gxlcms > mssql > sql判断数据库,表,存储过程等是否存在的代码

sql判断数据库,表,存储过程等是否存在的代码

时间:2021-07-01 10:21:17 帮助过:106人阅读

代码:

  1. --库是否存在
  2. if exists(select * from master..sysdatabases where name=N'库名')
  3. print 'exists'
  4. else
  5. print 'not exists'
  6. ---------------
  7. -- 判断要创建的表名是否存在
  8. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  9. -- 删除表
  10. drop table [dbo].[表名]
  11. GO
  12. ---------------
  13. -----列是否存在
  14. IF COL_LENGTH( '表名','列名') IS NULL
  15. PRINT 'not exists'
  16. ELSE
  17. PRINT 'exists'
  18. alter table 表名 drop constraint 默认值名称
  19. go
  20. alter table 表名 drop column 列名
  21. go
  22. -----
  23. --判断要创建临时表是否存在
  24. If Object_Id('Tempdb.dbo.#Test') Is Not Null
  25. Begin
  26. print '存在'
  27. End
  28. Else
  29. Begin
  30. print '不存在'
  31. End
  32. ---------------
  33. -- 判断要创建的存储过程名是否存在
  34. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[存储过程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  35. -- 删除存储过程
  36. drop procedure [dbo].[存储过程名]
  37. GO
  38. ---------------
  39. -- 判断要创建的视图名是否存在
  40. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[视图名]') and OBJECTPROPERTY(id, N'IsView') = 1)
  41. -- 删除视图
  42. drop view [dbo].[视图名]
  43. GO
  44. ---------------
  45. -- 判断要创建的函数名是否存在
  46. if exists (select * from sysobjects where xtype='fn' and name='函数名')
  47. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[函数名]') and xtype in (N'FN', N'IF', N'TF'))
  48. -- 删除函数
  49. drop function [dbo].[函数名]
  50. GO
  51. if col_length('表名', '列名') is null
  52. print '不存在'
  53. select 1 from sysobjects where id in (select id from syscolumns where name='列名') and name='表名'

sql判断是否存在

  1. --判断数据库是否存在
  2. if exists(select * from master..sysdatabases where name=N'库名')
  3. print 'exists'
  4. else
  5. print 'not exists'
  6. ---------------
  7. -- 判断要创建的表名是否存在
  8. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  9. -- 删除表
  10. drop table [dbo].[表名]
  11. GO
  12. ---------------
  13. --判断要创建临时表是否存在
  14. If Object_Id('Tempdb.dbo.#Test') Is Not Null
  15. Begin
  16. print '存在'
  17. End
  18. Else
  19. Begin
  20. print '不存在'
  21. End
  22. ---------------
  23. -- 判断要创建的存储过程名是否存在
  24. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[存储过程名]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  25. -- 删除存储过程
  26. drop procedure [dbo].[存储过程名]
  27. GO
  28. ---------------
  29. -- 判断要创建的视图名是否存在
  30. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[视图名]') and OBJECTPROPERTY(id, N'IsView') = 1)
  31. -- 删除视图
  32. drop view [dbo].[视图名]
  33. GO
  34. ---------------
  35. -- 判断要创建的函数名是否存在
  36. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[函数名]') and xtype in (N'FN', N'IF', N'TF'))
  37. -- 删除函数
  38. drop function [dbo].[函数名]
  39. GO
  40. if col_length('表名', '列名') is null
  41. print '不存在'
  42. select 1 from sysobjects where id in (select id from syscolumns where name='列名') and name='表名'

您可能感兴趣的文章:

  • Sql Server中判断表、列不存在则创建的方法
  • Mysql判断表字段或索引是否存在
  • sql server判断数据库、表、列、视图是否存在
  • 用SQL语句查找Access中某表是否存在的小技巧
  • MySql获取某个字段存在于哪个表的sql语句
  • sqlserver 各种判断是否存在(表名、函数、存储过程等)
  • oracle用什么SQL语句判断表存不存在
  • SQL判断字段列是否存在的方法
  • Oracle判断表、列、主键是否存在的方法

人气教程排行