
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_MSPLAN_GTTSOURCE' AND KSQL_COL_NAME ='FTASKENTITYID')
 ALTER TABLE T_MSPLAN_GTTSOURCE ADD (FTASKENTITYID NVARCHAR(255) DEFAULT ' ' NOT NULL );
 
 
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_MSPLAN_GTTSOURCE' AND KSQL_COL_NAME ='FTTIMEFIELD')
 ALTER TABLE T_MSPLAN_GTTSOURCE ADD (FTTIMEFIELD NVARCHAR(50) DEFAULT ' ' NOT NULL );
 
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_MSPLAN_GTTSOURCE' AND KSQL_COL_NAME ='FTTIMEFIELDDESC')
 ALTER TABLE T_MSPLAN_GTTSOURCE ADD (FTTIMEFIELDDESC NVARCHAR(50) DEFAULT ' ' NOT NULL );
 

IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_MSPLAN_ENTITY' AND KSQL_COL_NAME ='FDEFAULTVIEW')
 ALTER TABLE T_MSPLAN_ENTITY ADD (FDEFAULTVIEW CHAR(1) DEFAULT '0' NOT NULL );
 
 
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_MSPLAN_ENTITY_CONDITION' AND KSQL_COL_NAME ='FVERSIONFILED')
 ALTER TABLE T_MSPLAN_ENTITY_CONDITION ADD (FVERSIONFILED NVARCHAR(50) DEFAULT ' ' NOT NULL );
 
 
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_MSPLAN_FILTER' AND KSQL_COL_NAME ='FCONTROLRANGEONE')
 ALTER TABLE T_MSPLAN_FILTER ADD (FCONTROLRANGEONE CHAR(1) DEFAULT '0' NOT NULL );
 
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_MSPLAN_FILTER' AND KSQL_COL_NAME ='FCONTROLRANGETWO')
 ALTER TABLE T_MSPLAN_FILTER ADD (FCONTROLRANGETWO CHAR(1) DEFAULT '0' NOT NULL );
 
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_MSPLAN_FILTER' AND KSQL_COL_NAME ='FCONTROLRANGETHREE')
 ALTER TABLE T_MSPLAN_FILTER ADD (FCONTROLRANGETHREE CHAR(1) DEFAULT '0' NOT NULL );
 
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_MSPLAN_FILTER' AND KSQL_COL_NAME ='FCONTROLRANGEFOUR')
 ALTER TABLE T_MSPLAN_FILTER ADD (FCONTROLRANGEFOUR CHAR(1) DEFAULT '0' NOT NULL );
 

IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_MSPLAN_ENTITY_RELATION')
CREATE TABLE T_MSPLAN_ENTITY_RELATION
(
	  FENTRYID             BIGINT            DEFAULT 0   NOT NULL,          	
	  FDETAILID            BIGINT            DEFAULT 0   NOT NULL,           
	  FSEQ                 INT               DEFAULT 0   NOT NULL,            	
	  FRENTITYID           NVARCHAR(255)     DEFAULT 0   NOT NULL, 
	  FRENTITYFLAG         NVARCHAR(50)      DEFAULT ' ' NOT NULL, 	
	  FRENTITYFLAGDESC     NVARCHAR(50)      DEFAULT ' ' NOT NULL, 	
	  FRENTRYFLAG          NVARCHAR(50)      DEFAULT ' ' NOT NULL, 
	  FRUPLEVELENTITYID    NVARCHAR(255)     DEFAULT 0   NOT NULL
);  

EXEC P_ALTERPK 'PK_MSPLAN_ENTITY_RELATION', 'T_MSPLAN_ENTITY_RELATION', 'FDETAILID', '1';
 
IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_MSPLAN_ENTITY_RELATION_FEQ') CREATE INDEX IDX_MSPLAN_ENTITY_RELATION_FEQ ON T_MSPLAN_ENTITY_RELATION (FENTRYID,FSEQ);


 



