-- B1 DEPENDS: BEFORE:PT:PROCESS_START

CREATE PROCEDURE _TmSp_AliasUpdateBefore
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER
AS
	flag int:=0;
BEGIN
	SELECT COUNT(*) INTO flag FROM "PUBLIC"."TABLES"
		WHERE "TABLE_NAME"='CDRU_TEMP';
	IF :flag>0 THEN
		EXEC 'TRUNCATE TABLE CDRU_TEMP';
		EXEC 'DROP TABLE CDRU_TEMP';
	END IF;
	
	SELECT COUNT(*) INTO flag FROM "PUBLIC"."TABLES"
		WHERE "TABLE_NAME"='OBTD_TEMP';
	IF :flag>0 THEN
		EXEC 'TRUNCATE TABLE OBTD_TEMP';
		DROP TABLE OBTD_TEMP;
	END IF;
	
	
	SELECT COUNT(*) INTO flag FROM "PUBLIC"."TABLES"
		WHERE "TABLE_NAME"='OCHH_TEMP';
	IF :flag>0 THEN
		EXEC 'TRUNCATE TABLE OCHH_TEMP';
		EXEC 'DROP TABLE OCHH_TEMP';
	END IF;
	
	
	SELECT COUNT(*) INTO flag FROM "PUBLIC"."TABLES"
		WHERE "TABLE_NAME"='OIPF_TEMP';
	IF :flag>0 THEN
		EXEC 'TRUNCATE TABLE OIPF_TEMP';
		DROP TABLE OIPF_TEMP;
	END IF;
	
	SELECT COUNT(*) INTO flag FROM "PUBLIC"."TABLES"
		WHERE "TABLE_NAME"='OOPR_TEMP';
	IF :flag>0 THEN
		EXEC 'TRUNCATE TABLE OOPR_TEMP';
		DROP TABLE OOPR_TEMP;
	END IF;
	
	SELECT COUNT(*) INTO flag FROM "PUBLIC"."TABLES"
		WHERE "TABLE_NAME"='OOST_TEMP';
	IF :flag>0 THEN
		EXEC 'TRUNCATE TABLE OOST_TEMP';
		DROP TABLE OOST_TEMP;
	END IF;
	
	SELECT COUNT(*) INTO flag FROM "PUBLIC"."TABLES"
		WHERE "TABLE_NAME"='OCFL_TEMP';
	IF :flag>0 THEN
		EXEC 'TRUNCATE TABLE OCFL_TEMP';
		DROP TABLE OCFL_TEMP;
	END IF;
	
	CREATE GLOBAL TEMPORARY COLUMN TABLE OBTD_TEMP(BatchNum int,_Date timestamp,_Memo nvarchar(50));
	CREATE GLOBAL TEMPORARY COLUMN TABLE OCHH_TEMP(CheckKey int,_User_ smallint);
	CREATE GLOBAL TEMPORARY COLUMN TABLE OIPF_TEMP(DocEntry int,_Comment nvarchar(250));
	CREATE GLOBAL TEMPORARY COLUMN TABLE OCFL_TEMP(Code int,_Date timestamp);

	INSERT INTO OBTD_TEMP SELECT "BatchNum","DateID","MemoID" FROM OBTD;
	INSERT INTO OCHH_TEMP SELECT "CheckKey","UserID" FROM OCHH;
	INSERT INTO OIPF_TEMP SELECT "DocEntry","Descr" FROM OIPF;
	INSERT INTO OCFL_TEMP SELECT "Code","DateID" FROM OCFL;
	
	SELECT COUNT(*) INTO flag FROM "PUBLIC"."TABLES"
		WHERE "TABLE_NAME"='CDRU';
	IF :flag>0 THEN
		CREATE GLOBAL TEMPORARY COLUMN TABLE CDRU_TEMP(objectID nvarchar(4),_User int);
		INSERT INTO CDRU_TEMP SELECT "ObjectId","UserID" FROM CDRU;
	END IF;
	
	SELECT COUNT(*) INTO flag FROM "PUBLIC"."TABLES"
		WHERE "TABLE_NAME"='OOPR';
	IF :flag>0 THEN
		CREATE GLOBAL TEMPORARY COLUMN TABLE OOPR_TEMP(OpprID int,_Percent DECIMAL(21,6));
		INSERT INTO OOPR_TEMP SELECT "OpprId","CloPrcnt" FROM OOPR;
	END IF;
	
	SELECT COUNT(*) INTO flag FROM "PUBLIC"."TABLES"
		WHERE "TABLE_NAME"='OOST';
	IF :flag>0 THEN
		CREATE GLOBAL TEMPORARY COLUMN TABLE OOST_TEMP(Num int,_Percent DECIMAL(21,6));
		INSERT INTO OOST_TEMP SELECT "Num","CloPrcnt" FROM OOST;
	END IF;
	
END;