IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTAPPLYDATE'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTAPPLYDATE DATETIME );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTAFFAIRDISCRIPTION'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTAFFAIRDISCRIPTION NVARCHAR(1024) );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTTOTALAMOUNT'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTTOTALAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTHEADCOUNT'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTHEADCOUNT BIGINT DEFAULT 0 NOT NULL );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTSTARTDATE'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTSTARTDATE DATETIME );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTENDDATE'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTENDDATE DATETIME );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTSTARTTIME'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTSTARTTIME DATETIME );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTENDTIME'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTENDTIME DATETIME );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTCITY'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTCITY NVARCHAR(255) );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTFROM'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTFROM NVARCHAR(255) );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTTO'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTTO NVARCHAR(255) );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTLARGETXT'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTLARGETXT VARCHAR(255) );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTLARGETXT_TAG'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTLARGETXT_TAG NCLOB );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTSOURCE'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTSOURCE VARCHAR(30) DEFAULT ' ' NOT NULL );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_INVOICEATTACHINFO' AND KSQL_COL_NAME IN ('FATTINVOICEENTYID'))
ALTER TABLE T_ER_INVOICEATTACHINFO ADD (FATTINVOICEENTYID BIGINT DEFAULT 0 NOT NULL );

IF NOT EXISTS (SELECT 1 FROM KSQL_USERTABLES WHERE KSQL_TABNAME = 'T_ER_BILLINGATTACHINFO')
CREATE TABLE T_ER_BILLINGATTACHINFO
(
    FID                   BIGINT          DEFAULT 0   NOT NULL,
    FENTRYID              BIGINT          DEFAULT 0   NOT NULL,
    FSEQ                  INT             DEFAULT 0   NOT NULL,
    FATTACHNO             NVARCHAR(80) DEFAULT ' ' NOT NULL,
    FATTACHNAME           VARCHAR(255),
    FORIGINALNAME         VARCHAR(255),
    FATTACHTYPE           VARCHAR(30)     DEFAULT ' ' NOT NULL,
    FGATHERTIME           DATETIME,
    FATTACHREMARK         NVARCHAR(1024),
    FATTACHURL            VARCHAR(512),
    FSNAPSHOTURL          VARCHAR(512),
    FROTATIONANGLE        VARCHAR(30)     DEFAULT ' ' NOT NULL,
    FATTACHSERIALNO       NVARCHAR(255) DEFAULT ' ' NOT NULL,
    FATTAPPLYDATE         DATETIME,
    FATTAFFAIRDISCRIPTION NVARCHAR(1024),
    FATTTOTALAMOUNT       DECIMAL(23, 10) DEFAULT 0   NOT NULL,
    FATTHEADCOUNT         BIGINT          DEFAULT 0   NOT NULL,
    FATTSTARTDATE         DATETIME,
    FATTENDDATE           DATETIME,
    FATTSTARTTIME         DATETIME,
    FATTENDTIME           DATETIME,
    FATTCITY              NVARCHAR(255),
    FATTFROM              NVARCHAR(255),
    FATTTO                NVARCHAR(255),
    FATTLARGETXT          VARCHAR(255),
    FATTLARGETXT_TAG      NCLOB,
    FATTSOURCE            VARCHAR(30)     DEFAULT ' ' NOT NULL,
    FATTINVOICEENTYID     BIGINT          DEFAULT 0   NOT NULL
);

EXEC P_ALTERPK 'PK_T_ER_BILLINGATTACHINFO', 'T_ER_BILLINGATTACHINFO', 'FENTRYID', '0';

IF NOT EXISTS (SELECT 1 FROM KSQL_INDEXES WHERE KSQL_INDNAME = 'IDX_ER_BILLINGATTACHINFO_FID') CREATE  CLUSTERED INDEX IDX_ER_BILLINGATTACHINFO_FID ON T_ER_BILLINGATTACHINFO ( FID );

IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_PUBREIMINVOICEINFO' AND KSQL_COL_NAME IN ('FISMUTILREIMBURSE'))
ALTER TABLE T_ER_PUBREIMINVOICEINFO ADD (FISMUTILREIMBURSE CHAR(1) DEFAULT '0' NOT NULL );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_PUBREIMINVOICEINFO' AND KSQL_COL_NAME IN ('FBILLINGPOOLID'))
ALTER TABLE T_ER_PUBREIMINVOICEINFO ADD (FBILLINGPOOLID BIGINT DEFAULT 0 NOT NULL );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_PUBREIMINVOICEINFO' AND KSQL_COL_NAME IN ('FISPOOL'))
ALTER TABLE T_ER_PUBREIMINVOICEINFO ADD (FISPOOL CHAR(1) DEFAULT '0' NOT NULL );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_PUBREIMINVOICEINFO' AND KSQL_COL_NAME IN ('FORIBALANCEAMOUNT'))
ALTER TABLE T_ER_PUBREIMINVOICEINFO ADD (FORIBALANCEAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_PUBREIMINVOICEINFO' AND KSQL_COL_NAME IN ('FPOOLREIMBURSEAMOUNT'))
ALTER TABLE T_ER_PUBREIMINVOICEINFO ADD (FPOOLREIMBURSEAMOUNT DECIMAL(23,10) DEFAULT 0 NOT NULL );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_PUBREIMINVOICEINFO' AND KSQL_COL_NAME IN ('FEXPIRYPAYDATE'))
ALTER TABLE T_ER_PUBREIMINVOICEINFO ADD (FEXPIRYPAYDATE DATETIME );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_PUBREIMINVOICEINFO' AND KSQL_COL_NAME IN ('FCOUNTRY'))
ALTER TABLE T_ER_PUBREIMINVOICEINFO ADD (FCOUNTRY BIGINT DEFAULT 0 NOT NULL );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_PUBREIMINVOICEINFO' AND KSQL_COL_NAME IN ('FISEXCESSREIM'))
ALTER TABLE T_ER_PUBREIMINVOICEINFO ADD (FISEXCESSREIM CHAR(1) DEFAULT '0' NOT NULL );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_PUBREIMINVOICEINFO' AND KSQL_COL_NAME IN ('FEXCESSDESC'))
ALTER TABLE T_ER_PUBREIMINVOICEINFO ADD (FEXCESSDESC NVARCHAR(512) DEFAULT ' ' NOT NULL );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_PUBREIMINVOICEINFO' AND KSQL_COL_NAME IN ('FTIMEGETON'))
ALTER TABLE T_ER_PUBREIMINVOICEINFO ADD (FTIMEGETON DATETIME );
IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_PUBREIMINVOICEINFO' AND KSQL_COL_NAME IN ('FTIMEGETOFF'))
ALTER TABLE T_ER_PUBREIMINVOICEINFO ADD (FTIMEGETOFF DATETIME );

IF NOT EXISTS (SELECT 1 FROM KSQL_USERCOLUMNS WHERE KSQL_COL_TABNAME = 'T_ER_PUBREIMINVOICEINFO' AND KSQL_COL_NAME IN ('FSELLERORGID'))
ALTER TABLE T_ER_PUBREIMINVOICEINFO ADD (FSELLERORGID BIGINT DEFAULT 0 NOT NULL );