@PHOLHAD
* ARQUIVO TABELA AUXILIAR
@EXECSQL DROP TABLE PHTTBX;
@EXECSQL DROP TABLE PHTTBX2;

CREATE TABLE "PHTTBX" 
(
  "TABVER"       VARCHAR(4) NOT NULL,
  "TABAUX"       VARCHAR(4) NOT NULL,
  "CODNIV"	 VARCHAR(8) NOT NULL,
  "DESCRI"	 VARCHAR(50),
 PRIMARY KEY ("TABVER", "TABAUX", "CODNIV")
);
@EXECSQL


CREATE TABLE "PHTTBX2" 
(
  "TABVER"       VARCHAR(4) NOT NULL,
  "TABAUX"       VARCHAR(4) NOT NULL,
  "CODNIV"	 VARCHAR(8) NOT NULL,
  "ANOMES"	 VARCHAR(8) NOT NULL,
  "TIPBAS"       VARCHAR(2),
  "BASCAL"       VARCHAR(40),
  "LIMMIN"       VARCHAR(15),
  "LIMMAX"	 VARCHAR(15),
  "QUANIV"	 VARCHAR(15),
  "PRENIV"       VARCHAR(15),
  "VALNIV"       VARCHAR(15),
  "PERNIV"       VARCHAR(15),
  "OBSERV"       VARCHAR(50),
 PRIMARY KEY ("TABVER", "TABAUX", "CODNIV", "ANOMES")
);
@EXECSQL

@PHOLHAD
* ARQUIVO TABELA AUXILIAR
@EXECSQL DROP TABLE PHTTBX;
@EXECSQL DROP TABLE PHTTBX2;

CREATE TABLE "PHTTBX" 
(
  "TABVER"       VARCHAR(4) NOT NULL,
  "TABAUX"       VARCHAR(4) NOT NULL,
  "CODNIV"	 VARCHAR(8) NOT NULL,
  "DESCRI"	 VARCHAR(50),
 PRIMARY KEY ("TABVER", "TABAUX", "CODNIV")
);
@EXECSQL


CREATE TABLE "PHTTBX2" 
(
  "TABVER"       VARCHAR(4) NOT NULL,
  "TABAUX"       VARCHAR(4) NOT NULL,
  "CODNIV"	 VARCHAR(8) NOT NULL,
  "ANOMES"	 VARCHAR(8) NOT NULL,
  "TIPBAS"       VARCHAR(2),
  "BASCAL"       VARCHAR(40),
  "LIMMIN"       VARCHAR(15),
  "LIMMAX"	 VARCHAR(15),
  "QUANIV"	 VARCHAR(15),
  "PRENIV"       VARCHAR(15),
  "VALNIV"       VARCHAR(15),
  "PERNIV"       VARCHAR(15),
  "OBSERV"       VARCHAR(50),
 PRIMARY KEY ("TABVER", "TABAUX", "CODNIV", "ANOMES")
);
@EXECSQL

@PHOLHAD
@EXECSQL CREATE EXCEPTION ERRO_PHCCAD 'ESTOU ZERANDO A DATA DA RESCISAO PHCCAD';
@EXECSQL CREATE EXCEPTION ERRO_PHCRES 'ESTOU ZERANDO A DATA DA RESCISAO PHCRES';

CREATE TRIGGER VERIFICA_PHCCAD FOR PHCCAD
ACTIVE BEFORE UPDATE POSITION 0
AS

begin 
if ((phcCad.DatDem = '00/00/0000' or
     phcCad.DatDem = '' or
     PhcCad.DatDem is null) and
    (PhcCAd.ZerDem <> 'S'))
then
    Exception ERRO_PHCCAD ;
end;
@EXECSQL


CREATE TRIGGER VERIFICA_PHCRES FOR PHCRES
ACTIVE BEFORE UPDATE POSITION 0
AS

begin 
  if  (Phcres.DatDem = '00/00/0000' or
       PhcRes.DatDem = '' or
       Phcres.DatDem is null)
  then
     Exception ERRO_PHCRES;
end;
@EXECSQL


