您好,欢迎访问一九零五行业门户网

SQL Server判断对象是否存在

sqlserver判断对象是否存在 无 1 判断数据库是否存在sql代码 if exists (select * from sys.databases where name = ’数据库名’) drop database [数据库名] if exists (select * from sys.databases where name = ’数据库名’) drop database [数据库名]2
sql server判断对象是否存在 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 2000if exists (select * from sysviews where object_id = ’[dbo].[视图名]’--sql server 2005if 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’ /* xtype 的表示参数类型,通常包括如下这些 c = check 约束 d = 默认值或 default 约束 f = foreign key 约束 l = 日志 fn = 标量函数 if = 内嵌表函数 p = 存储过程 pk = primary key 约束(类型是 k) rf = 复制筛选存储过程 s = 系统表 tf = 表函数 tr = 触发器 u = 用户表 uq = unique 约束(类型是 k) v = 视图 x = 扩展存储过程 */ select [name],[id],crdate from sysobjects where xtype=’u’/*xtype 的表示参数类型,通常包括如下这些c = check 约束d = 默认值或 default 约束f = foreign key 约束l = 日志fn = 标量函数if = 内嵌表函数p = 存储过程pk = primary key 约束(类型是 k)rf = 复制筛选存储过程s = 系统表tf = 表函数tr = 触发器u = 用户表uq = unique 约束(类型是 k)v = 视图x = 扩展存储过程*/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=110 判断表中是否存在索引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=’对象名’
其它类似信息

推荐信息