![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
写测试SQL语句时,经常出现对象已经存在这类的问题,很烦,很有必要在创建对象之前判断其是否存在,以下是在网上收集一些判断方法,整理出来,都试验证过。
-- 判断表是否存在,存在删除之
-- 非临时表
if exists ( select * from sysobjects where id = object_id (N ' 表名 ' ) and OBJECTPROPERTY (id, N ' IsUserTable ' ) = 1 )
-- OR if exists(select 1 from sysobjects where id=object_id('表名') and xtype='U')
drop table 表名
-- 临时表
if object_id ( ' tempdb..表名 ' ) is not null
drop table 表名
-- 判断存储过程是否存在
if exists ( select 1 from sysobjects where id = object_id ( ' 存储过程名 ' ) and xtype = ' P ' )
drop procedure 存储过程名
-- 获取用户创建的对象信息
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 = 扩展存储过程
*/
-- 删除表主键
declare @PKName varchar ( 100 )
declare @sql varchar ( 4000 )
-- 获取主键名称
select @PKName = name from sysobjects where xtype = ' PK ' and parent_obj = object_id ( ' 表名 ' )
set @sql = ' alter table prodconstru drop constraint ' + @PKName
exec ( @sql )
-- 添加主键
alter table 表名 add constraint 主键名 primary key (列名);
-- -获取表的列名等信息(在网上当的)
select c.name, t.name as type, c.length
,( case t.name
when ' nvarchar ' then c.length / 2
when ' nchar ' then c.length / 2
else c.length
end )
as reallength
from syscolumns c join systypes t
on c.xtype = t.xtype
where t.name <> ' sysname ' and c.id = object_id ( ' 表名 ' )
-- 判断表是否含有带自动编号的列
select objectproperty ( object_id ( ' 表名 ' ), ' tablehasidentity ' )
-- 判断表是否存在,存在删除之
-- 非临时表
if exists ( select * from sysobjects where id = object_id (N ' 表名 ' ) and OBJECTPROPERTY (id, N ' IsUserTable ' ) = 1 )
-- OR if exists(select 1 from sysobjects where id=object_id('表名') and xtype='U')
drop table 表名
-- 临时表
if object_id ( ' tempdb..表名 ' ) is not null
drop table 表名
-- 判断存储过程是否存在
if exists ( select 1 from sysobjects where id = object_id ( ' 存储过程名 ' ) and xtype = ' P ' )
drop procedure 存储过程名
-- 获取用户创建的对象信息
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 = 扩展存储过程
*/
-- 删除表主键
declare @PKName varchar ( 100 )
declare @sql varchar ( 4000 )
-- 获取主键名称
select @PKName = name from sysobjects where xtype = ' PK ' and parent_obj = object_id ( ' 表名 ' )
set @sql = ' alter table prodconstru drop constraint ' + @PKName
exec ( @sql )
-- 添加主键
alter table 表名 add constraint 主键名 primary key (列名);
-- -获取表的列名等信息(在网上当的)
select c.name, t.name as type, c.length
,( case t.name
when ' nvarchar ' then c.length / 2
when ' nchar ' then c.length / 2
else c.length
end )
as reallength
from syscolumns c join systypes t
on c.xtype = t.xtype
where t.name <> ' sysname ' and c.id = object_id ( ' 表名 ' )
-- 判断表是否含有带自动编号的列
select objectproperty ( object_id ( ' 表名 ' ), ' tablehasidentity ' )