码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2020-07-06 16:41:18      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:declare   class   add   --   修改表   字段   iat   tab   creat   

DECLARE

  num NUMBER;
BEGIN

	-- 新增学生表 student
	SELECT
		COUNT (1) INTO num
	FROM
		cols
	WHERE
		table_name = UPPER (‘student‘) ;
	IF num > 0 THEN
		EXECUTE IMMEDIATE ‘DROP TABLE student‘ ; 
		EXECUTE IMMEDIATE ‘CREATE TABLE student (
							id NUMBER NOT NULL,
							name VARCHAR2(40) NULL ,
							xb VARCHAR2(40) NULL ,
							age int(3) NULL ,
							birthday DATE NULL ,
							className VARCHAR2(80) NULL
							)‘ ;
		EXECUTE IMMEDIATE ‘ALTER TABLE student ADD PRIMARY KEY (id)‘ ;
	ELSE
		EXECUTE IMMEDIATE ‘CREATE TABLE student (
							id NUMBER NOT NULL,
							name VARCHAR2(40) NULL ,
							xb VARCHAR2(40) NULL ,
							age int(3) NULL ,
							birthday DATE NULL ,
							className VARCHAR2(80) NULL
							)‘ ;
		EXECUTE IMMEDIATE ‘ALTER TABLE student ADD CHECK (ID IS NOT NULL)‘ ;
		EXECUTE IMMEDIATE ‘ALTER TABLE student ADD PRIMARY KEY (ID)‘ ;
	END IF ;
	
	-- 学生表新增班级 className 字段
	SELECT COUNT(1)
		INTO num
		from cols
		where table_name = upper(‘student‘)
		 and column_name = upper(‘className‘);
		IF num > 0 THEN
	execute immediate ‘alter table student MODIFY (className varchar2(20))‘;
	ELSE
	execute immediate ‘alter table student add className varchar2(40)‘;
	END IF;
	
END;

  

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

标签:declare   class   add   --   修改表   字段   iat   tab   creat   

原文地址:https://www.cnblogs.com/Big-Boss/p/13255130.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!