SQLServer 判断表或字段是否存在新增/修改表结构可重复执行sql

-- 新增学生表 student
IF object_id('student') IS NOT NULL
DROP TABLE student;

CREATE TABLE student (
	id bigint NOT NULL IDENTITY(1,1) ,
	name nvarchar(40) NULL ,
	xb nvarchar(40) NULL ,
	age int(3) NULL ,
	birthday datetime NULL ,
	className nvarchar(80) NULL
);
ALTER TABLE student ADD PRIMARY KEY (ID);

-- 学生表新增班级 className 字段
IF NOT EXISTS(SELECT 1 FROM sys.columns WHERE name='className' AND object_id=object_id('student'))
	alter table student add className nvarchar(40);
ELSE
	alter table student alter column className nvarchar(20);

  

posted @ 2020-07-06 15:47  ヤBig、Bossづ  阅读(502)  评论(0编辑  收藏  举报