SQL判断表不存在创建表,字段不存在,添加字段

判断如果表不存在,就创建表:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tablename]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

begin

CREATE TABLE [dbo].[tablename] (

[FID] [int] NOT NULL ,

[FCode] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,

[FName] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,

[FParentID] [int] NOT NULL ,

[FState] [int] NOT NULL ,

[FType] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL 

) ON [PRIMARY]

end 


判断字段如果不存在,就创建字段:

IF NOT EXISTS (SELECT a.name FROM syscolumns a,sysobjects b

WHERE a.id=b.id

AND LTRIM(a.name)='col_name' AND LTRIM(b.name)='tablename')

ALTER TABLE [tablename] ADD [col_name] int NULL

GO


你可能感兴趣的:(where)