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