前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >sqlserver 各种判断是否存在(表名、函数、存储过程.......)

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

作者头像
lpxxn
发布2018-01-31 11:07:54
1.7K0
发布2018-01-31 11:07:54
举报
文章被收录于专栏:技术之路技术之路
代码语言:javascript
复制
库是否存在
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='表名'
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-01-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档