1、查看表是否存在:
select * from sysobjects where id = object_id(表名) and OBJECTPROPERTY(id, N'IsUserTable') = 1
2、查看视图是否存在:
select table_name from information_schema.views where table_name = 视图名
或:
SELECT * FROM dbo.sysobjects WHERE id = object_id('视图名') AND OBJECTPROPERTY(id, N'IsView') = 1
3、查看函数是否存在:
select * from sysobjects where xtype='fn' and name='函数名'
或者:
select * from dbo.sysobjects where id = object_id(N'[dbo].[函数名]') and xtype in (N'FN', N'IF', N'TF')
4、查看字段是否存在:
select 1 from syscolumns where id=object_id('表名') and name='字段名'
5、判断索引是否存在:(如果不存在,则创建)
if not exists(select * from sysindexes where id=object_id('表名') and name='索引名')
create nonclustered index [索引名] on [表名](字段 asc,字段 asc)
end
go
6、判断存储过程是否存在:
IF exists
(SELECT * FROM sysobjects
WHERE id=object_id(N'[master].[存储过程名称]')
and xtype='P'
)
begin
drop procedure [master].[存储过程名称]
end
go