http://blog.csdn.net/wuxiaokaixinguo/article/details/8293048
检查数据库中是否存在某个表,不存在则生成该表方法一:IF NOT EXISTS (select * from dbo.sysobjects where xtype='U' and Name = 'T_Users') BEGIN CREATE TABLE [dbo].[T_Users](userName varchar(20),userPass varchar(20)); END方法二:create procedure sel_tab @table_name varchar(20) AS declare @flag int set @flag=0; set @flag = (select flag from (select count(*) as flag from dbo.sysobjects where xtype='U' and Name = 'T_Users') as sel_count); if @flag = 0 create table T_Users(userName varchar(20),userPass varchar(20)); GO EXEC sel_tab "T_Users"
判断列是否存在
if not exists(select * from syscolumns where id=object_id('TPARTNER') and name='AgencyPCC')
begin
alter table dbo.TPARTNER add AgencyPCC nvarchar(50) null
End
GO
select AgencyPCC,* from dbo.TPARTNER;
if exists(select * from syscolumns where id=object_id('TPARTNER') and name='AgencyPCC')
begin
alter table dbo.TPARTNER drop COLUMN AgencyPCC
End
Create Table table1(ID Int)
GO if not exists (select Name From SysColumns Where ID = OBJECT_ID('table1') And Name = 'New_column') ALTER TABLE table1 ADD New_column VARCHAR(20) NULL GO Select * From table1 GO Drop Table table1 --Result /* ID New_column