{$IFNOTEXISTS SELECT 1 FROM RDB$RELATION_FIELDS	WHERE RDB$FIELD_NAME = 'ORDER_DATE' AND RDB$RELATION_NAME = 'DOCUMENTS'}

  ALTER TABLE DOCUMENTS ADD ORDER_DATE DATE;
  ALTER TABLE DOCUMENTS ADD ACTION_DATE DATE;
  ALTER TABLE DOCUMENTS ADD ORDER_NUM VARCHAR(100) CHARACTER SET UTF8 COLLATE UTF8;
  COMMIT WORK;

{$ENDIF}

{$IFNOTEXISTS SELECT 1 FROM RDB$RELATION_FIELDS	WHERE RDB$FIELD_NAME = 'DPL_DATA' AND RDB$RELATION_NAME = 'DOCUMENTS'}

  ALTER TABLE DOCUMENTS ADD DPL_DATA VARCHAR(8000) CHARACTER SET WIN1251;
  COMMIT WORK;

{$ENDIF}

{$IFEXISTS SELECT 1 FROM RDB$RELATION_FIELDS WHERE RDB$FIELD_NAME = 'DPL_DATA2' AND RDB$RELATION_NAME = 'DOCUMENTS'}

  ALTER TABLE DOCUMENTS DROP DPL_DATA2;

{$ENDIF}

{$IFEXISTS SELECT 1 FROM RDB$RELATION_FIELDS F, RDB$FIELDS S WHERE F.RDB$FIELD_NAME = 'DPL_DATA' AND F.RDB$RELATION_NAME = 'DOCUMENTS' 
    AND S.RDB$FIELD_NAME = F.RDB$FIELD_SOURCE AND S.RDB$CHARACTER_LENGTH = 4000}

  DROP PROCEDURE SDOCS_DOCUMENT_DUB;
  DROP PROCEDURE SDOCS_DOCUMENT_GN;
  DROP PROCEDURE SDOCS_BLANK_DISCARD;
  DROP PROCEDURE SDOCS_DOCUMENT_GET;
  DROP PROCEDURE SDOCS_DOCUMENT_SET_SPO;
  COMMIT WORK;

  UPDATE DOCUMENTS SET DPL_DATA = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(DPL_DATA, '«', '\"'), '»', '\"'), 'ˮ', '\"'), '„', '\"'), '×', 'x');
  COMMIT WORK;
  ALTER TABLE DOCUMENTS ADD DPL_DATA2 BLOB SUB_TYPE 1;
  COMMIT WORK;
  UPDATE DOCUMENTS SET DPL_DATA2 = DPL_DATA;
  COMMIT WORK;
  ALTER TABLE DOCUMENTS DROP DPL_DATA;
  COMMIT WORK;
  ALTER TABLE DOCUMENTS ADD DPL_DATA VARCHAR(8000) CHARACTER SET WIN1251;
  COMMIT WORK;
  UPDATE DOCUMENTS SET DPL_DATA = DPL_DATA2;
  COMMIT WORK;
  ALTER TABLE DOCUMENTS DROP DPL_DATA2;
  COMMIT WORK;

SET TERM ^ ;
CREATE OR ALTER PROCEDURE SDOCS_DOCUMENT_GET (
  I$PERSON_ID BIGINT, 
  I$OBJ_ID BIGINT, 
  I$USER_ID VARCHAR(40) CHARACTER SET UTF8 COLLATE UTF8)
RETURNS (
  TAG_NAME VARCHAR(200) CHARACTER SET UTF8 COLLATE UTF8, 
  VAL VARCHAR(8000) CHARACTER SET UTF8 COLLATE UTF8)
AS
BEGIN

  SUSPEND;

END
^
SET TERM ; ^

{$ENDIF}

{$IFNOTEXISTS SELECT 1 FROM RDB$RELATION_FIELDS	WHERE RDB$FIELD_NAME = 'N_PAGES' AND RDB$RELATION_NAME = 'DOCUMENTS'}

  ALTER TABLE DOCUMENTS ADD N_PAGES SMALLINT;
  COMMIT WORK;

{$ENDIF}

{$IFNOTEXISTS SELECT 1 FROM RDB$RELATION_FIELDS	WHERE RDB$FIELD_NAME = 'FRONT_ID' AND RDB$RELATION_NAME = 'BLANKS'}

  ALTER TABLE BLANKS ADD FRONT_ID BIGINT;
  COMMIT WORK;
  CREATE INDEX BLANKS_IDX1 ON BLANKS (FRONT_ID);
  COMMIT WORK;

{$ENDIF}


{$IFNOTEXISTS SELECT 1 FROM RDB$RELATION_FIELDS	WHERE RDB$FIELD_NAME = 'DUB_STATE' AND RDB$RELATION_NAME = 'DOCUMENTS'}

  ALTER TABLE DOCUMENTS ADD DUB_STATE SMALLINT DEFAULT 0;
  COMMIT WORK;

{$ENDIF}

{$IFNOTEXISTS SELECT 1 FROM RDB$RELATION_FIELDS	WHERE RDB$FIELD_NAME = 'SPO_REGNUM' AND RDB$RELATION_NAME = 'DOCUMENTS'}

  ALTER TABLE DOCUMENTS ADD SPO_REGNUM VARCHAR(100) CHARACTER SET UTF8 COLLATE UTF8;
  COMMIT WORK;

{$ENDIF}

{$IFNOTEXISTS SELECT 1 FROM RDB$RELATION_FIELDS	WHERE RDB$FIELD_NAME = 'SUBTYPE_ID' AND RDB$RELATION_NAME = 'BLANK_PACKS'}

  ALTER TABLE BLANK_PACKS ADD SUBTYPE_ID SMALLINT DEFAULT 0 NOT NULL;
  ALTER TABLE BLANK_PACKS ADD COMMENTS VARCHAR(200) CHARACTER SET UTF8 COLLATE UTF8;
  COMMIT WORK;

{$ENDIF}
