if exists (select * from sys.databases where name = ’数据库名’) print '存在' -- drop database [数据库名] --如果存在则删除
2、表是否存在
if exists (select * from sysobjects where>print '存在' -- drop table [表名] --如果存在则删除
3、存储过程是否存在
if exists (select * from sysobjects where>print '存在' -- drop procedure [存储过程名] --如果存在则删除
4、临时表是否存在
if object_id(’tempdb..#临时表名’) is not null print '存在' -- drop table #临时表名 --如果存在则删除
If Object_Id('Tempdb.dbo.#临时表') Is Not Null
5、视图是否存在
if exists (select * from dbo.sysobjects where>
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 2008
if exists (select * from sysobjects where>
print '存在'
--或
if exists (select * from sysobjects where>
print '存在'
--drop view dbo.dc_adplan
6、函数是否存在
if exists (select * from dbo.sysobjects where>-- drop function [dbo].[函数名]
7、列是否存在
--if col_length('表名', '列名') is null
if exists(select * from syscolumns where>
-- >
8、判断索引是否存在
IF EXISTS (SELECT * FROM sys.check_constraints WHERE object_id = OBJECT_ID(N'[dbo].[CK_约束名]') AND parent_object_id = OBJECT_ID(N'[dbo].[表名]'))
ALTER TABLE [dbo].[表名] DROP CONSTRAINT [CK_约束名]
9、判断列是否自自增列
if columnproperty(object_id('table'),'col’,’IsIdentity’)=1 print '自增列'
else
print '不是自增列'
10、查看数据库中对象
select * from sys.sysobjects where name='对象名'
11、获取用户创建的对象信息
SELECT [name],[id],crdate FROM sysobjects where xtype='U'