1、SQLServer检查表是否存在,不存在则创建的脚本
if not exists(select * from sysobjects where id=object_Id('dbo.MyTable') ) CREATE TABLE user_id(usermessage VARCHAR(MAX))
或
select count(*) from sysobjects where id = object_id("数据库名.Owner.表名")
通过返回结果来判断是否创建表
2、SQLServer检查表字段是否存在,不存在则创建的脚本
IF NOT EXISTS(SELECT * FROM syscolumns WHERE [ID] = object_id(N'dbo.MyTable') AND [NAME] = N'ID') ALTER TABLE dbo.tempTableRatedElements ADD [ID] INT IDENTITY(1,1) CONSTRAINT [PK_MyTable] PRIMARY KEY NONCLUSTERED GO
评论回复