-- 发票自定义抵扣字段映射表
IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_ER_INVFIELDMAP')
CREATE TABLE T_ER_INVFIELDMAP (
                                  FID BIGINT DEFAULT 0 NOT NULL,
                                  FENTRYID BIGINT DEFAULT 0 NOT NULL,
                                  FSEQ INT DEFAULT 0 NOT NULL,
                                  FMETAFIELD VARCHAR(100) DEFAULT ' ' NOT NULL,
                                  FCODEFIELD VARCHAR(100) DEFAULT ' ' NOT NULL
);


IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_ER_INVFIELDMAPPING')
CREATE TABLE T_ER_INVFIELDMAPPING (
                                      FID BIGINT DEFAULT 0 NOT NULL,
                                      FENTITYKEY VARCHAR(80) DEFAULT ' ' NOT NULL
);


EXEC P_ALTERPK 'PK_T_ER_INVFIELDMAP', 'T_ER_INVFIELDMAP', 'FENTRYID', '1';

IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_TABNAME = 'T_ER_INVFIELDMAP' AND KSQL_INDNAME = 'IDX_ER_INVFIELDMAP_FIELD') CREATE INDEX IDX_ER_INVFIELDMAP_FIELD ON T_ER_INVFIELDMAP ( FMETAFIELD ,FCODEFIELD );

IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_TABNAME = 'T_ER_INVFIELDMAP' AND KSQL_INDNAME = 'IDX_ER_INVFIELDMAP_FID') CREATE INDEX IDX_ER_INVFIELDMAP_FID ON T_ER_INVFIELDMAP ( FID );


EXEC P_ALTERPK 'PK_T_ER_INVFIELDMAPPING', 'T_ER_INVFIELDMAPPING', 'FID', '1';

IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_TABNAME = 'T_ER_INVFIELDMAPPING' AND KSQL_INDNAME = 'IDX_ER_INVFIELDMAPPING_KEY') CREATE INDEX IDX_ER_INVFIELDMAPPING_KEY ON T_ER_INVFIELDMAPPING ( FENTITYKEY );