---检查数据库是否存在
if exists(select 1 from master..sysdatabases where name='数据库名')
drop database '数据库名'
else
createa database '数据库名';
---检查表是否存在
select count(*) from sysobjects where id = '数据库名.表名'
if exists (select count(*) from sysobjects where id = '数据库名.表名')
create table '表名'(
...
...
...
)
else
drop table '表名'
---检查字段名是否存在
if exists (select * from syscolumns where name='colname1' and id='数据库名.Owner.表名')
dosomething....
else
dosomething.....
----检查表视图或者储存过程是否存在
if object_id('视图或存储过程名') is not null
drop proc/view ..
go
create proc/view ...
或
if Exists(select * from sysobjects where name='视图或存储过程名' AND type = 'P/V')
drop proc/view 。。。
go
create proc/view 。。。