码迷,mamicode.com
首页 > 其他好文 > 详细

判断对象是否存在 (if exists (select * from sysobjec...

时间:2018-03-21 13:47:32      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:dea   用户   cti   自增列   ble   _id   存储过程   tempdb   数据   

1 判断数据库是否存在

Sql代码
if exists (select * from sys.databases where name = ’数据库名’) drop database [数据库名]

if exists (select * from sys.databases where name = ’数据库名’)
drop database [数据库名]

2 判断表是否存在

Sql代码
if exists (select * from sysobjects where id = object_id(N’[表名]’) and OBJECTPROPERTY(id, N’IsUserTable’) = 1)
drop table [表名]

if exists (select * from sysobjects where id = object_id(N’[表名]’) and OBJECTPROPERTY(id, N’IsUserTable’) = 1)
drop table [表名]

3 判断存储过程是否存在

Sql代码
if exists (select * from sysobjects where id = object_id(N’[存储过程名]’) and OBJECTPROPERTY(id, N’IsProcedure’) = 1)
drop procedure [存储过程名]

if exists (select * from sysobjects where id = object_id(N’[存储过程名]’) and OBJECTPROPERTY(id, N’IsProcedure’) = 1)
drop procedure [存储过程名]

4 判断临时表是否存在

Sql代码
if object_id(’tempdb..#临时表名’) is not null
drop table #临时表名

if object_id(’tempdb..#临时表名’) is not null
drop table #临时表名

5 判断视图是否存在

Sql代码
--SQL Server 2000
IF EXISTS (SELECT * FROM sysviews WHERE object_id = ’[dbo].[视图名]’
--SQL Server 2005
IF EXISTS (SELECT * FROM sys.views WHERE object_id = ’[dbo].[视图名]’
--SQL Server 2000
IF EXISTS (SELECT * FROM sysviews WHERE object_id = ’[dbo].[视图名]’
--SQL Server 2005
IF EXISTS (SELECT * FROM sys.views WHERE object_id = ’[dbo].[视图名]’

6 判断函数是否存在

Sql代码
-- 判断要创建的函数名是否存在
if exists (select * from dbo.sysobjects where id = object_id(N’[dbo].[函数名]’) and xtype in (N’FN’, N’IF’, N’TF’))
drop function [dbo].[函数名]
-- 判断要创建的函数名是否存在
if exists (select * from dbo.sysobjects where id = object_id(N’[dbo].[函数名]’) and xtype in (N’FN’, N’IF’, N’TF’))
drop function [dbo].[函数名]

7 获取用户创建的对象信息

Sql代码
SELECT [name],[id],crdate FROM sysobjects where xtype=’U’
SELECT [name],[id],crdate FROM sysobjects where xtype=’U’

8 判断列是否存在

Sql代码
if exists(select * from syscolumns where id=object_id(’表名’) and name=’列名’)
alter table 表名
drop column 列名

if exists(select * from syscolumns where id=object_id(’表名’) and name=’列名’)
alter table 表名 drop column 列名

9 判断列是否自增列

Sql代码
if columnproperty(object_id(’table’),’col’,’IsIdentity’)=1 print ’自增列’ else print ’不是自增列’

SELECT * FROM sys.columns WHERE object_id=OBJECT_ID(’表名’) AND is_identity=1 if columnproperty(object_id(’table’),’col’,’IsIdentity’)=1 print ’自增列’ else print ’不是自增列’

SELECT * FROM sys.columns WHERE object_id=OBJECT_ID(’表名’) AND is_identity=1

10 判断表中是否存在索引

Sql代码
if exists(select * from sysindexes where id=object_id(’表名’) and name=’索引名’)
print ’存在’
else print ’不存在

if exists(select * from sysindexes where id=object_id(’表名’) and name=’索引名’)
print ’存在’
else print ’不存在

11 查看数据库中对象

Sql代码
SELECT * FROM sys.sysobjects WHERE name=’对象名’
SELECT * FROM sys.sysobjects WHERE name=’对象名’

12对于游标的存储,判断是这样写的

if exists(select * from master.dbo.syscursors where cursor_name=‘游标名‘)
begin deallocate 游标名 end

判断对象是否存在 (if exists (select * from sysobjec...

标签:dea   用户   cti   自增列   ble   _id   存储过程   tempdb   数据   

原文地址:https://www.cnblogs.com/--zl--/p/8616116.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!