Oracle对数据库字段类型的可重复提交脚本

DECLARE
	cnt integer; 
	tempCnt integer;
	tempCnt2 integer;
BEGIN
	SELECT COUNT(0)
	INTO cnt
	FROM user_tab_columns a
	WHERE a.Table_name = upper('gdt_apply_intermediary')
		AND a.COLUMN_NAME = upper('INTERMEDIARYCOSTS')
		AND a.DATA_TYPE = upper('number')
		AND a.DATA_SCALE = 2;
		
	SELECT COUNT(0)
	INTO tempCnt
	FROM user_tab_columns a
	WHERE a.Table_name = upper('gdt_apply_intermediary')
		AND a.COLUMN_NAME = upper('INTERMEDIARYCOSTS_1');
		
	IF cnt = 0 THEN 
		IF tempCnt = 1 THEN 
			EXECUTE IMMEDIATE 'SELECT COUNT(0) FROM dual WHERE EXISTS (SELECT NULL FROM gdt_apply_intermediary a WHERE a.INTERMEDIARYCOSTS_1 IS NOT NULL)' INTO tempCnt2;
			IF tempCnt2 = 0 THEN
				EXECUTE IMMEDIATE 'ALTER TABLE gdt_apply_intermediary DROP COLUMN INTERMEDIARYCOSTS_1';
				EXECUTE IMMEDIATE 'ALTER TABLE gdt_apply_intermediary ADD (INTERMEDIARYCOSTS_1 integer)';
			END IF;
		ELSE
			tempCnt2 := 0;
			EXECUTE IMMEDIATE 'ALTER TABLE gdt_apply_intermediary ADD (INTERMEDIARYCOSTS_1 integer)';
		END IF;
		
		IF tempCnt2 = 0 THEN
			EXECUTE IMMEDIATE 'UPDATE gdt_apply_intermediary SET INTERMEDIARYCOSTS_1 = INTERMEDIARYCOSTS';
		END IF;
		
		EXECUTE IMMEDIATE 'update gdt_apply_intermediary set INTERMEDIARYCOSTS = null';
		EXECUTE IMMEDIATE 'ALTER TABLE gdt_apply_intermediary MODIFY (INTERMEDIARYCOSTS number(16,2))';
	END IF;
	SELECT COUNT(0)
	INTO tempCnt
	FROM user_tab_columns a
	WHERE a.Table_name = upper('gdt_apply_intermediary')
		AND a.COLUMN_NAME = upper('INTERMEDIARYCOSTS_1');
	IF tempCnt = 1 THEN
		EXECUTE IMMEDIATE 'UPDATE gdt_apply_intermediary SET INTERMEDIARYCOSTS = INTERMEDIARYCOSTS_1';
		EXECUTE IMMEDIATE 'ALTER TABLE gdt_apply_intermediary DROP COLUMN INTERMEDIARYCOSTS_1';
	END IF;
END;
/

相关逻辑:

cnt表示是该字段类型事都已经修改过了,

tempCnt表示是否有临时字段

tmpCnt表示临时字段中是否有值

 

首先判断该字段是否已经修改过,如果没有:

判断是否有临时字段

如果没有临时字段

新建临时字段

如果有临时字段

判断临时字段中是否有值

如果有不做操作

如果没有就记录tmpCnt2 = 0

如果没有临时字段

创建临时字段 并记录tmpCnt2 = 0

如果tmpCnt2 = 0;

复制原字段到临时字段

 

修改字段为需要的类型

复制临时表中的数据过来

删除临时表

(大概是这个意思,这样逻辑是因为字段中有数据就不可以修改字段类型)

你可能感兴趣的:(Oracle学习笔记)