sqlserver 各种判断是否存在(表名、函数、存储过程....)

库是否存在
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 = '表名'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值