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

所属分类: 数据库 / MsSql 阅读数: 1139
收藏 0 赞 0 分享

代码:

--库是否存在
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='表名'

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 Server数据库图文教程

如何远程连接SQL Server数据库图文教程
收藏 0 赞 0 分享

一个SQL语句获得某人参与的帖子及在该帖得分总和

一个SQL语句获得某人参与的帖子及在该帖得分总和
收藏 0 赞 0 分享

通用分页存储过程,源码共享,大家共同完善

通用分页存储过程,源码共享,大家共同完善
收藏 0 赞 0 分享

SQL查找某一条记录的方法

SQL查找某一条记录的方法
收藏 0 赞 0 分享

使用 GUID 值来作为数据库行标识讲解

使用 GUID 值来作为数据库行标识讲解
收藏 0 赞 0 分享

非常详细的SQL--JOIN之完全用法

非常详细的SQL--JOIN之完全用法
收藏 0 赞 0 分享

收缩后对数据库的使用有影响吗?

收缩后对数据库的使用有影响吗?
收藏 0 赞 0 分享

mssql server 存储过程里,bulk insert table from '路径+文件',路径固定,文件名不固定的实现方法

mssql server 存储过程里,bulk insert table from '路径+文件',路径固定,文件名不固定的实现方法
收藏 0 赞 0 分享

请问在mssql“SQL事件探查器”里表格的标题,如CPU,Read,Write,Duration,SPID.........的解释

请问在mssql“SQL事件探查器”里表格的标题,如CPU,Read,Write,Duration,SPID.........的解释
收藏 0 赞 0 分享

SQL Server 2000的安全配置

SQL Server 2000的安全配置
收藏 0 赞 0 分享
查看更多