/* ============================================================ */
/*   Database name:  SISTEMAS_TEKNISA_5_01                      */
/*   DBMS name:      Microsoft SQL Server 6.x (double precision */
/*   Created on:     13/08/2008  13:46                          */
/* ============================================================ */

/*  Delete trigger "td_acertoorceve" for table "ACERTOORCEVE"  */
create trigger td_acertoorceve on ACERTOORCEVE for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "ACERTOQUEBRA"  */
    delete ACERTOQUEBRA
    from   ACERTOQUEBRA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
     and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
     and   t2.CDDEPTO = t1.CDDEPTO
    
    /*  Delete all children in "ACERTOSOBRA"  */
    delete ACERTOSOBRA
    from   ACERTOSOBRA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
     and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
     and   t2.CDDEPTO = t1.CDDEPTO
    
    /*  Delete all children in "ACERTOHREXTR"  */
    delete ACERTOHREXTR
    from   ACERTOHREXTR t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
     and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
     and   t2.CDDEPTO = t1.CDDEPTO
    
    /*  Delete all children in "ACERTOEXTRA"  */
    delete ACERTOEXTRA
    from   ACERTOEXTRA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
     and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
     and   t2.CDDEPTO = t1.CDDEPTO

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_agencia" for table "AGENCIA"  */
create trigger td_agencia on AGENCIA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "AGENCIA" if children still exist in "CONTCORR"  */
    if exists (select 1
               from   CONTCORR t2, deleted t1
               where  t2.CDBANCO = t1.CDBANCO
                and   t2.CDAGENCIA = t1.CDAGENCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTCORR". Cannot delete parent "AGENCIA".'
          goto error
       end
    
    /*  Delete all children in "CONTATO_AGEN"  */
    delete CONTATO_AGEN
    from   CONTATO_AGEN t2, deleted t1
    where  t2.CDBANCO = t1.CDBANCO
     and   t2.CDAGENCIA = t1.CDAGENCIA

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_agendaevento" for table "AGENDAEVENTO"  */
create trigger td_agendaevento on AGENDAEVENTO for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "EXECEVENTO"  */
    delete EXECEVENTO
    from   EXECEVENTO t2, deleted t1
    where  t2.NRAGENDAEVEN = t1.NRAGENDAEVEN
    
    /*  Delete all children in "PARAMEXECEVENTO"  */
    delete PARAMEXECEVENTO
    from   PARAMEXECEVENTO t2, deleted t1
    where  t2.NRAGENDAEVEN = t1.NRAGENDAEVEN

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_ajbolpag" for table "AJBOLPAG"  */
create trigger td_ajbolpag on AJBOLPAG for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "MOVFXAJBOLP"  */
    delete MOVFXAJBOLP
    from   MOVFXAJBOLP t2, deleted t1
    where  t2.CDFILIBOLPAG = t1.CDFILIAL
     and   t2.NRLANBOLPAG = t1.NRLANBOLPAG
     and   t2.CDAJUCTPAG = t1.CDAJUCTPAG
    
    /*  Delete all children in "ADIANTAPAGAR"  */
    delete ADIANTAPAGAR
    from   ADIANTAPAGAR t2, deleted t1
    where  t2.CDFILIAJBOLP = t1.CDFILIAL
     and   t2.NRLANBOLPAG = t1.NRLANBOLPAG
     and   t2.CDAJUCTPAG = t1.CDAJUCTPAG

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_ajbolrec" for table "AJBOLREC"  */
create trigger td_ajbolrec on AJBOLREC for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "MOVFXAJBOLR"  */
    delete MOVFXAJBOLR
    from   MOVFXAJBOLR t2, deleted t1
    where  t2.CDFILIBOLREC = t1.CDFILIAL
     and   t2.NRLANBOLREC = t1.NRLANBOLREC
     and   t2.CDAJUCTREC = t1.CDAJUCTREC
    
    /*  Delete all children in "ADIANTARECEB"  */
    delete ADIANTARECEB
    from   ADIANTARECEB t2, deleted t1
    where  t2.CDFILIAJBOLR = t1.CDFILIAL
     and   t2.NRLANBOLREC = t1.NRLANBOLREC
     and   t2.CDAJUCTREC = t1.CDAJUCTREC

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_ala" for table "ALA"  */
create trigger td_ala on ALA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "ALA" if children still exist in "SALA"  */
    if exists (select 1
               from   SALA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALA = t1.CDALA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SALA". Cannot delete parent "ALA".'
          goto error
       end
    
    /*  Cannot delete parent "ALA" if children still exist in "ESTATDIET"  */
    if exists (select 1
               from   ESTATDIET t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALA = t1.CDALA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESTATDIET". Cannot delete parent "ALA".'
          goto error
       end
    
    /*  Delete all children in "ALANDAR"  */
    delete ALANDAR
    from   ALANDAR t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDALA = t1.CDALA

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_aliquota" for table "ALIQUOTA"  */
create trigger td_aliquota on ALIQUOTA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "MENSALIQU"  */
    delete MENSALIQU
    from   MENSALIQU t2, deleted t1
    where  t2.NRALIQUOTA = t1.NRALIQUOTA

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_almoxarifado" for table "ALMOXARIFADO"  */
create trigger td_almoxarifado on ALMOXARIFADO for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "LOCALESTOQUE"  */
    if exists (select 1
               from   LOCALESTOQUE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOCALESTOQUE". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "CAIXA"  */
    if exists (select 1
               from   CAIXA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CAIXA". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "ROTATRANS"  */
    if exists (select 1
               from   ROTATRANS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROTATRANS". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "PARFILIALEMP"  */
    if exists (select 1
               from   PARFILIALEMP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXEMPR = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALEMP". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "PROTESE"  */
    if exists (select 1
               from   PROTESE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROTESE". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "REQUHOSP"  */
    if exists (select 1
               from   REQUHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXASAID = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUHOSP". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "REQUHOSP"  */
    if exists (select 1
               from   REQUHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXAENTR = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUHOSP". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "PARFILIHOSP"  */
    if exists (select 1
               from   PARFILIHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXFARMA = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIHOSP". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "LOJA"  */
    if exists (select 1
               from   LOJA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOJA". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "EQUIPAPROD"  */
    if exists (select 1
               from   EQUIPAPROD t2, deleted t1
               where  t2.CDFILIMOVI = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQUIPAPROD". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "LOGEQUIPPRO"  */
    if exists (select 1
               from   LOGEQUIPPRO t2, deleted t1
               where  t2.CDFILIMOVI = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGEQUIPPRO". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDFILIRECE = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "RESERPRODPED"  */
    if exists (select 1
               from   RESERPRODPED t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESERPRODPED". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "INVEROTA"  */
    if exists (select 1
               from   INVEROTA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVEROTA". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "CURVAABC"  */
    if exists (select 1
               from   CURVAABC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CURVAABC". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "CTRLMOVIESTQ"  */
    if exists (select 1
               from   CTRLMOVIESTQ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXENTR = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLMOVIESTQ". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "CTRLMOVIESTQ"  */
    if exists (select 1
               from   CTRLMOVIESTQ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXSAID = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLMOVIESTQ". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "REQUESTO"  */
    if exists (select 1
               from   REQUESTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXSAID = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUESTO". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "REQUESTO"  */
    if exists (select 1
               from   REQUESTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXENTR = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUESTO". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "PARFILIALVMN"  */
    if exists (select 1
               from   PARFILIALVMN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXPERDA = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALVMN". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "CODIGOBARRA"  */
    if exists (select 1
               from   CODIGOBARRA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODIGOBARRA". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "PRODACABADO"  */
    if exists (select 1
               from   PRODACABADO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODACABADO". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "INVEROTALOTE"  */
    if exists (select 1
               from   INVEROTALOTE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVEROTALOTE". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Cannot delete parent "ALMOXARIFADO" if children still exist in "LANCTOESTOQ"  */
    if exists (select 1
               from   LANCTOESTOQ t2, deleted t1
               where  t2.CDFILIMOVI = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCTOESTOQ". Cannot delete parent "ALMOXARIFADO".'
          goto error
       end
    
    /*  Delete all children in "OPERALMOX"  */
    delete OPERALMOX
    from   OPERALMOX t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDALMOXARIFE = t1.CDALMOXARIFE
    
    /*  Delete all children in "GROPERALMOX"  */
    delete GROPERALMOX
    from   GROPERALMOX t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDALMOXARIFE = t1.CDALMOXARIFE

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_amostraproc" for table "AMOSTRAPROC"  */
create trigger td_amostraproc on AMOSTRAPROC for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "AMOSTRAPROC" if children still exist in "PROCESSAPROD"  */
    if exists (select 1
               from   PROCESSAPROD t2, deleted t1
               where  t2.NRAMOSTRA = t1.NRAMOSTRA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROCESSAPROD". Cannot delete parent "AMOSTRAPROC".'
          goto error
       end
    
    /*  Delete all children in "ITEMAMOSTRA"  */
    delete ITEMAMOSTRA
    from   ITEMAMOSTRA t2, deleted t1
    where  t2.NRAMOSTRA = t1.NRAMOSTRA

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_andar" for table "ANDAR"  */
create trigger td_andar on ANDAR for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "ANDAR" if children still exist in "ANDARSETO"  */
    if exists (select 1
               from   ANDARSETO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDANDAR = t1.CDANDAR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ANDARSETO". Cannot delete parent "ANDAR".'
          goto error
       end
    
    /*  Delete all children in "ALANDAR"  */
    delete ALANDAR
    from   ALANDAR t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDANDAR = t1.CDANDAR

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_aprerece" for table "APRERECE"  */
create trigger td_aprerece on APRERECE for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "APREPROD"  */
    delete APREPROD
    from   APREPROD t2, deleted t1
    where  t2.CDAPRERECE = t1.CDAPRERECE

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_atenhosp" for table "ATENHOSP"  */
create trigger td_atenhosp on ATENHOSP for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "OBSATHOSP"  */
    if exists (select 1
               from   OBSATHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSATHOSP". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "ATENHOSP"  */
    if exists (select 1
               from   ATENHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENDHORIG = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ATENHOSP". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "PROCATHOSP"  */
    if exists (select 1
               from   PROCATHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROCATHOSP". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "FUNCATHOSP"  */
    if exists (select 1
               from   FUNCATHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FUNCATHOSP". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "HONORATHOSP"  */
    if exists (select 1
               from   HONORATHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HONORATHOSP". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "REQUPROC"  */
    if exists (select 1
               from   REQUPROC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUPROC". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "OCUPATHOSP"  */
    if exists (select 1
               from   OCUPATHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OCUPATHOSP". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "TRIAGDOAC"  */
    if exists (select 1
               from   TRIAGDOAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TRIAGDOAC". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "TIPAGDOAC"  */
    if exists (select 1
               from   TIPAGDOAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPAGDOAC". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "COLETDOAC"  */
    if exists (select 1
               from   COLETDOAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COLETDOAC". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "PRODUDOAC"  */
    if exists (select 1
               from   PRODUDOAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUDOAC". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "PREPDOAC"  */
    if exists (select 1
               from   PREPDOAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREPDOAC". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "RECEBDOAC"  */
    if exists (select 1
               from   RECEBDOAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEBDOAC". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "AFERESE"  */
    if exists (select 1
               from   AFERESE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AFERESE". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "ITMOVPROTESE"  */
    if exists (select 1
               from   ITMOVPROTESE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITMOVPROTESE". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "REQUHOSP"  */
    if exists (select 1
               from   REQUHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUHOSP". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "ITAGENFUNC"  */
    if exists (select 1
               from   ITAGENFUNC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATHOSPGERA = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITAGENFUNC". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "ITAGENFUNC"  */
    if exists (select 1
               from   ITAGENFUNC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATHOSPORIG = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITAGENFUNC". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "APURFATUGH"  */
    if exists (select 1
               from   APURFATUGH t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURFATUGH". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "DESPHOSP"  */
    if exists (select 1
               from   DESPHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESPHOSP". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Cannot delete parent "ATENHOSP" if children still exist in "ENDEATEN"  */
    if exists (select 1
               from   ENDEATEN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRATENHOSP = t1.NRATENHOSP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ENDEATEN". Cannot delete parent "ATENHOSP".'
          goto error
       end
    
    /*  Delete all children in "VISITATHOSP"  */
    delete VISITATHOSP
    from   VISITATHOSP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRATENHOSP = t1.NRATENHOSP

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_atenreal" for table "ATENREAL"  */
create trigger td_atenreal on ATENREAL for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "ITATENREAL"  */
    delete ITATENREAL
    from   ITATENREAL t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTCONSPACI = t1.DTCONSPACI

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_ativpessoal" for table "ATIVPESSOAL"  */
create trigger td_ativpessoal on ATIVPESSOAL for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "DESCATIVPESS"  */
    delete DESCATIVPESS
    from   DESCATIVPESS t2, deleted t1
    where  t2.CDOPERADOR = t1.CDOPERADOR
     and   t2.NRATIVPESS = t1.NRATIVPESS

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_auditoria" for table "AUDITORIA"  */
create trigger td_auditoria on AUDITORIA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "AUDITORIA" if children still exist in "TRIAGDOAC"  */
    if exists (select 1
               from   TRIAGDOAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRAUDITORIA = t1.NRAUDITORIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TRIAGDOAC". Cannot delete parent "AUDITORIA".'
          goto error
       end
    
    /*  Cannot delete parent "AUDITORIA" if children still exist in "EXAMDOACAO"  */
    if exists (select 1
               from   EXAMDOACAO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRAUDITORIA = t1.NRAUDITORIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXAMDOACAO". Cannot delete parent "AUDITORIA".'
          goto error
       end
    
    /*  Cannot delete parent "AUDITORIA" if children still exist in "PREPDOAC"  */
    if exists (select 1
               from   PREPDOAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRAUDITORIA = t1.NRAUDITORIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREPDOAC". Cannot delete parent "AUDITORIA".'
          goto error
       end
    
    /*  Cannot delete parent "AUDITORIA" if children still exist in "CANDIDATO"  */
    if exists (select 1
               from   CANDIDATO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRAUDITORIA = t1.NRAUDITORIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CANDIDATO". Cannot delete parent "AUDITORIA".'
          goto error
       end
    
    /*  Cannot delete parent "AUDITORIA" if children still exist in "AUDITCAND"  */
    if exists (select 1
               from   AUDITCAND t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRAUDITORIA = t1.NRAUDITORIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUDITCAND". Cannot delete parent "AUDITORIA".'
          goto error
       end
    
    /*  Delete all children in "INSPECAUDIT"  */
    delete INSPECAUDIT
    from   INSPECAUDIT t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRAUDITORIA = t1.NRAUDITORIA
    
    /*  Delete all children in "DESCAUDIT"  */
    delete DESCAUDIT
    from   DESCAUDIT t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRAUDITORIA = t1.NRAUDITORIA

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_autoforne" for table "AUTOFORNE"  */
create trigger td_autoforne on AUTOFORNE for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "AUTOFORNE" if children still exist in "AUTOFORNE"  */
    if exists (select 1
               from   AUTOFORNE t2, deleted t1
               where  t2.CDFILDESTCON = t1.CDFILDEST
                and   t2.CDFILLANCCON = t1.CDFILLANC
                and   t2.NRAUTOFORCON = t1.NRAUTOFORN)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUTOFORNE". Cannot delete parent "AUTOFORNE".'
          goto error
       end
    
    /*  Cannot delete parent "AUTOFORNE" if children still exist in "ITEMAUTF"  */
    if exists (select 1
               from   ITEMAUTF t2, deleted t1
               where  t2.CDFILDESTOL = t1.CDFILDEST
                and   t2.CDFILLANCOL = t1.CDFILLANC
                and   t2.NRAUTOFORNOL = t1.NRAUTOFORN)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMAUTF". Cannot delete parent "AUTOFORNE".'
          goto error
       end
    
    /*  Delete all children in "MOVFLUXAF"  */
    delete MOVFLUXAF
    from   MOVFLUXAF t2, deleted t1
    where  t2.CDFILDEST = t1.CDFILDEST
     and   t2.CDFILLANC = t1.CDFILLANC
     and   t2.NRAUTOFORN = t1.NRAUTOFORN
    
    /*  Delete all children in "MENSAUTF"  */
    delete MENSAUTF
    from   MENSAUTF t2, deleted t1
    where  t2.CDFILDEST = t1.CDFILDEST
     and   t2.CDFILLANC = t1.CDFILLANC
     and   t2.NRAUTOFORN = t1.NRAUTOFORN
    
    /*  Delete all children in "ITEMAUTF"  */
    delete ITEMAUTF
    from   ITEMAUTF t2, deleted t1
    where  t2.CDFILDEST = t1.CDFILDEST
     and   t2.CDFILLANC = t1.CDFILLANC
     and   t2.NRAUTOFORN = t1.NRAUTOFORN
    
    /*  Delete all children in "PRAZOPGTAF"  */
    delete PRAZOPGTAF
    from   PRAZOPGTAF t2, deleted t1
    where  t2.CDFILDEST = t1.CDFILDEST
     and   t2.CDFILLANC = t1.CDFILLANC
     and   t2.NRAUTOFORN = t1.NRAUTOFORN
    
    /*  Delete all children in "ENCARGOAF"  */
    delete ENCARGOAF
    from   ENCARGOAF t2, deleted t1
    where  t2.CDFILDEST = t1.CDFILDEST
     and   t2.CDFILLANC = t1.CDFILLANC
     and   t2.NRAUTOFORN = t1.NRAUTOFORN
    
    /*  Delete all children in "RELACENTRAF"  */
    delete RELACENTRAF
    from   RELACENTRAF t2, deleted t1
    where  t2.CDFILDEST = t1.CDFILDEST
     and   t2.CDFILLANC = t1.CDFILLANC
     and   t2.NRAUTOFORN = t1.NRAUTOFORN

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_avalcand" for table "AVALCAND"  */
create trigger td_avalcand on AVALCAND for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "PTFORTECAN"  */
    delete PTFORTECAN
    from   PTFORTECAN t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDCAND = t1.CDCAND
     and   t2.DTAVALCAND = t1.DTAVALCAND
    
    /*  Delete all children in "PTFRACOCAN"  */
    delete PTFRACOCAN
    from   PTFRACOCAN t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDCAND = t1.CDCAND
     and   t2.DTAVALCAND = t1.DTAVALCAND

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_baselineproj" for table "BASELINEPROJ"  */
create trigger td_baselineproj on BASELINEPROJ for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "BASELINEPROJ" if children still exist in "TAREFA"  */
    if exists (select 1
               from   TAREFA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPROJETO = t1.CDPROJETO
                and   t2.NRBASELINEPR = t1.NRBASELINEPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAREFA". Cannot delete parent "BASELINEPROJ".'
          goto error
       end
    
    /*  Cannot delete parent "BASELINEPROJ" if children still exist in "ITEMTRABPROJ"  */
    if exists (select 1
               from   ITEMTRABPROJ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPROJETO = t1.CDPROJETO
                and   t2.NRBASELINEPR = t1.NRBASELINEPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMTRABPROJ". Cannot delete parent "BASELINEPROJ".'
          goto error
       end
    
    /*  Cannot delete parent "BASELINEPROJ" if children still exist in "ESCOPOPROJET"  */
    if exists (select 1
               from   ESCOPOPROJET t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPROJETO = t1.CDPROJETO
                and   t2.NRBASELINEPR = t1.NRBASELINEPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESCOPOPROJET". Cannot delete parent "BASELINEPROJ".'
          goto error
       end
    
    /*  Cannot delete parent "BASELINEPROJ" if children still exist in "OPERPROJETO"  */
    if exists (select 1
               from   OPERPROJETO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPROJETO = t1.CDPROJETO
                and   t2.NRBASELINEPR = t1.NRBASELINEPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERPROJETO". Cannot delete parent "BASELINEPROJ".'
          goto error
       end
    
    /*  Cannot delete parent "BASELINEPROJ" if children still exist in "RISCOPROJETO"  */
    if exists (select 1
               from   RISCOPROJETO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPROJETO = t1.CDPROJETO
                and   t2.NRBASELINEPR = t1.NRBASELINEPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RISCOPROJETO". Cannot delete parent "BASELINEPROJ".'
          goto error
       end
    
    /*  Cannot delete parent "BASELINEPROJ" if children still exist in "SOLIBASELINE"  */
    if exists (select 1
               from   SOLIBASELINE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPROJETO = t1.CDPROJETO
                and   t2.NRBASELINEPR = t1.NRBASELINEPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLIBASELINE". Cannot delete parent "BASELINEPROJ".'
          goto error
       end
    
    /*  Cannot delete parent "BASELINEPROJ" if children still exist in "PRODBASELINE"  */
    if exists (select 1
               from   PRODBASELINE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPROJETO = t1.CDPROJETO
                and   t2.NRBASELINEPR = t1.NRBASELINEPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODBASELINE". Cannot delete parent "BASELINEPROJ".'
          goto error
       end
    
    /*  Cannot delete parent "BASELINEPROJ" if children still exist in "ENTBASELINE"  */
    if exists (select 1
               from   ENTBASELINE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPROJETO = t1.CDPROJETO
                and   t2.NRBASELINEPR = t1.NRBASELINEPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ENTBASELINE". Cannot delete parent "BASELINEPROJ".'
          goto error
       end
    
    /*  Cannot delete parent "BASELINEPROJ" if children still exist in "ASSUNTO"  */
    if exists (select 1
               from   ASSUNTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPROJETO = t1.CDPROJETO
                and   t2.NRBASELINEPR = t1.NRBASELINEPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ASSUNTO". Cannot delete parent "BASELINEPROJ".'
          goto error
       end
    
    /*  Cannot delete parent "BASELINEPROJ" if children still exist in "HABILPROJETO"  */
    if exists (select 1
               from   HABILPROJETO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPROJETO = t1.CDPROJETO
                and   t2.NRBASELINEPR = t1.NRBASELINEPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HABILPROJETO". Cannot delete parent "BASELINEPROJ".'
          goto error
       end
    
    /*  Cannot delete parent "BASELINEPROJ" if children still exist in "HABILOPERPRJ"  */
    if exists (select 1
               from   HABILOPERPRJ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPROJETO = t1.CDPROJETO
                and   t2.NRBASELINEPR = t1.NRBASELINEPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HABILOPERPRJ". Cannot delete parent "BASELINEPROJ".'
          goto error
       end
    
    /*  Cannot delete parent "BASELINEPROJ" if children still exist in "MONITORAPROJ"  */
    if exists (select 1
               from   MONITORAPROJ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPROJETO = t1.CDPROJETO
                and   t2.NRBASELINEPR = t1.NRBASELINEPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MONITORAPROJ". Cannot delete parent "BASELINEPROJ".'
          goto error
       end
    
    /*  Cannot delete parent "BASELINEPROJ" if children still exist in "CRONOPROJETO"  */
    if exists (select 1
               from   CRONOPROJETO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPROJETO = t1.CDPROJETO
                and   t2.NRBASELINEPR = t1.NRBASELINEPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CRONOPROJETO". Cannot delete parent "BASELINEPROJ".'
          goto error
       end
    
    /*  Cannot delete parent "BASELINEPROJ" if children still exist in "SERVPREST"  */
    if exists (select 1
               from   SERVPREST t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPROJETO = t1.CDPROJETO
                and   t2.NRBASELINEPR = t1.NRBASELINEPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVPREST". Cannot delete parent "BASELINEPROJ".'
          goto error
       end
    
    /*  Delete all children in "TIPORECPROJ"  */
    delete TIPORECPROJ
    from   TIPORECPROJ t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPROJETO = t1.CDPROJETO
     and   t2.NRBASELINEPR = t1.NRBASELINEPR
    
    /*  Delete all children in "ESTRECHUMPRJ"  */
    delete ESTRECHUMPRJ
    from   ESTRECHUMPRJ t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPROJETO = t1.CDPROJETO
     and   t2.NRBASELINEPR = t1.NRBASELINEPR
    
    /*  Delete all children in "TIPOINDPROJ"  */
    delete TIPOINDPROJ
    from   TIPOINDPROJ t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPROJETO = t1.CDPROJETO
     and   t2.NRBASELINEPR = t1.NRBASELINEPR

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_bkgthread" for table "BKGTHREAD"  */
create trigger td_bkgthread on BKGTHREAD for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "BKGTHREAD" if children still exist in "BKGTHREAD"  */
    if exists (select 1
               from   BKGTHREAD t2, deleted t1
               where  t2.IDREQTHREAD = t1.IDTHREAD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BKGTHREAD". Cannot delete parent "BKGTHREAD".'
          goto error
       end
    
    /*  Cannot delete parent "BKGTHREAD" if children still exist in "EXECEVENTO"  */
    if exists (select 1
               from   EXECEVENTO t2, deleted t1
               where  t2.IDTHREAD = t1.IDTHREAD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXECEVENTO". Cannot delete parent "BKGTHREAD".'
          goto error
       end
    
    /*  Delete all children in "BKGTHREADPARAM"  */
    delete BKGTHREADPARAM
    from   BKGTHREADPARAM t2, deleted t1
    where  t2.IDTHREAD = t1.IDTHREAD
    
    /*  Delete all children in "BKGTHREADSQL"  */
    delete BKGTHREADSQL
    from   BKGTHREADSQL t2, deleted t1
    where  t2.IDTHREAD = t1.IDTHREAD
    
    /*  Delete all children in "BKGTHREADDATA"  */
    delete BKGTHREADDATA
    from   BKGTHREADDATA t2, deleted t1
    where  t2.IDTHREAD = t1.IDTHREAD
    
    /*  Delete all children in "BKGSCREENPARAM"  */
    delete BKGSCREENPARAM
    from   BKGSCREENPARAM t2, deleted t1
    where  t2.IDTHREAD = t1.IDTHREAD

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_boletpagar" for table "BOLETPAGAR"  */
create trigger td_boletpagar on BOLETPAGAR for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "BOLETPAGAR" if children still exist in "CONTAPAGAR"  */
    if exists (select 1
               from   CONTAPAGAR t2, deleted t1
               where  t2.CDFILIBPAG = t1.CDFILIAL
                and   t2.NRLANBOLPAG = t1.NRLANBOLPAG)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTAPAGAR". Cannot delete parent "BOLETPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "BOLETPAGAR" if children still exist in "AJBOLPAG"  */
    if exists (select 1
               from   AJBOLPAG t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANBOLPAG = t1.NRLANBOLPAG)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJBOLPAG". Cannot delete parent "BOLETPAGAR".'
          goto error
       end
    
    /*  Delete all children in "ERRCNABBOLP"  */
    delete ERRCNABBOLP
    from   ERRCNABBOLP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANBOLPAG = t1.NRLANBOLPAG
    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.CDFILIBPAG = t1.CDFILIAL
     and   t2.NRLANBOLPAG = t1.NRLANBOLPAG

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_boletreceber" for table "BOLETRECEBER"  */
create trigger td_boletreceber on BOLETRECEBER for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "BOLETRECEBER" if children still exist in "CONTARECEBER"  */
    if exists (select 1
               from   CONTARECEBER t2, deleted t1
               where  t2.CDFILIBOLREC = t1.CDFILIAL
                and   t2.NRLANBOLREC = t1.NRLANBOLREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTARECEBER". Cannot delete parent "BOLETRECEBER".'
          goto error
       end
    
    /*  Delete all children in "AJBOLREC"  */
    delete AJBOLREC
    from   AJBOLREC t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANBOLREC = t1.NRLANBOLREC
    
    /*  Delete all children in "ERRCNABBOLR"  */
    delete ERRCNABBOLR
    from   ERRCNABBOLR t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANBOLREC = t1.NRLANBOLREC
    
    /*  Delete all children in "TPRECBOLETO"  */
    delete TPRECBOLETO
    from   TPRECBOLETO t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANBOLREC = t1.NRLANBOLREC
    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.CDFILIBREC = t1.CDFILIAL
     and   t2.NRLANBOLREC = t1.NRLANBOLREC

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_candidato" for table "CANDIDATO"  */
create trigger td_candidato on CANDIDATO for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CANDIDATO" if children still exist in "IDIOMACAND"  */
    if exists (select 1
               from   IDIOMACAND t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDCAND = t1.CDCAND)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IDIOMACAND". Cannot delete parent "CANDIDATO".'
          goto error
       end
    
    /*  Cannot delete parent "CANDIDATO" if children still exist in "AUDITCAND"  */
    if exists (select 1
               from   AUDITCAND t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDCAND = t1.CDCAND)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUDITCAND". Cannot delete parent "CANDIDATO".'
          goto error
       end
    
    /*  Cannot delete parent "CANDIDATO" if children still exist in "FOTOCAND"  */
    if exists (select 1
               from   FOTOCAND t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDCAND = t1.CDCAND)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FOTOCAND". Cannot delete parent "CANDIDATO".'
          goto error
       end
    
    /*  Delete all children in "OBSERVCAND"  */
    delete OBSERVCAND
    from   OBSERVCAND t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDCAND = t1.CDCAND
    
    /*  Delete all children in "TESTCAND"  */
    delete TESTCAND
    from   TESTCAND t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDCAND = t1.CDCAND
    
    /*  Delete all children in "EXPEPROFCAND"  */
    delete EXPEPROFCAND
    from   EXPEPROFCAND t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDCAND = t1.CDCAND
    
    /*  Delete all children in "CURSOCAND"  */
    delete CURSOCAND
    from   CURSOCAND t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDCAND = t1.CDCAND
    
    /*  Delete all children in "LIONIBUSCAND"  */
    delete LIONIBUSCAND
    from   LIONIBUSCAND t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDCAND = t1.CDCAND
    
    /*  Delete all children in "AVALCAND"  */
    delete AVALCAND
    from   AVALCAND t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDCAND = t1.CDCAND

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_capalote" for table "CAPALOTE"  */
create trigger td_capalote on CAPALOTE for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "LANCLOTE"  */
    delete LANCLOTE
    from   LANCLOTE t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDLOTE = t1.CDLOTE

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_cardadia" for table "CARDADIA"  */
create trigger td_cardadia on CARDADIA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CARDADIA" if children still exist in "RETIRADA"  */
    if exists (select 1
               from   RETIRADA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRCARDAPIO = t1.NRCARDAPIO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RETIRADA". Cannot delete parent "CARDADIA".'
          goto error
       end
    
    /*  Delete all children in "COMPCARD"  */
    delete COMPCARD
    from   COMPCARD t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRCARDAPIO = t1.NRCARDAPIO
    
    /*  Delete all children in "CARDACLI"  */
    delete CARDACLI
    from   CARDACLI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRCARDAPIO = t1.NRCARDAPIO

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_cardpaci" for table "CARDPACI"  */
create trigger td_cardpaci on CARDPACI for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "NUTRNECE"  */
    delete NUTRNECE
    from   NUTRNECE t2, deleted t1
    where  t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTCONSPACI = t1.DTCONSPACI
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDSERVFILI = t1.CDSERVFILI
     and   t2.DTCARDPACI = t1.DTCARDPACI
    
    /*  Delete all children in "COMCADIAPACI"  */
    delete COMCADIAPACI
    from   COMCADIAPACI t2, deleted t1
    where  t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTCONSPACI = t1.DTCONSPACI
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDSERVFILI = t1.CDSERVFILI
     and   t2.DTCARDPACI = t1.DTCARDPACI

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_cargo" for table "CARGO"  */
create trigger td_cargo on CARGO for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CARGO" if children still exist in "CANDIDATO"  */
    if exists (select 1
               from   CANDIDATO t2, deleted t1
               where  t2.CDCARGO = t1.CDCARGO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CANDIDATO". Cannot delete parent "CARGO".'
          goto error
       end
    
    /*  Cannot delete parent "CARGO" if children still exist in "FUNCIONARIO"  */
    if exists (select 1
               from   FUNCIONARIO t2, deleted t1
               where  t2.CDCARGO = t1.CDCARGO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FUNCIONARIO". Cannot delete parent "CARGO".'
          goto error
       end
    
    /*  Cannot delete parent "CARGO" if children still exist in "MOVI_PESSOAL"  */
    if exists (select 1
               from   MOVI_PESSOAL t2, deleted t1
               where  t2.CDCARGO = t1.CDCARGO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVI_PESSOAL". Cannot delete parent "CARGO".'
          goto error
       end
    
    /*  Cannot delete parent "CARGO" if children still exist in "CPREV_TEMP"  */
    if exists (select 1
               from   CPREV_TEMP t2, deleted t1
               where  t2.CDCARGO = t1.CDCARGO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CPREV_TEMP". Cannot delete parent "CARGO".'
          goto error
       end
    
    /*  Cannot delete parent "CARGO" if children still exist in "TALENTO"  */
    if exists (select 1
               from   TALENTO t2, deleted t1
               where  t2.CDCARGO = t1.CDCARGO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TALENTO". Cannot delete parent "CARGO".'
          goto error
       end
    
    /*  Cannot delete parent "CARGO" if children still exist in "MOVFUNCRH"  */
    if exists (select 1
               from   MOVFUNCRH t2, deleted t1
               where  t2.CDCARGO = t1.CDCARGO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVFUNCRH". Cannot delete parent "CARGO".'
          goto error
       end
    
    /*  Cannot delete parent "CARGO" if children still exist in "CLIENTE"  */
    if exists (select 1
               from   CLIENTE t2, deleted t1
               where  t2.CDCARGOCNJCL = t1.CDCARGO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENTE". Cannot delete parent "CARGO".'
          goto error
       end
    
    /*  Cannot delete parent "CARGO" if children still exist in "CLIENTE"  */
    if exists (select 1
               from   CLIENTE t2, deleted t1
               where  t2.CDCARGOCLIE = t1.CDCARGO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENTE". Cannot delete parent "CARGO".'
          goto error
       end
    
    /*  Cannot delete parent "CARGO" if children still exist in "OPERADOR"  */
    if exists (select 1
               from   OPERADOR t2, deleted t1
               where  t2.CDCARGO = t1.CDCARGO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERADOR". Cannot delete parent "CARGO".'
          goto error
       end
    
    /*  Cannot delete parent "CARGO" if children still exist in "INDICADOR"  */
    if exists (select 1
               from   INDICADOR t2, deleted t1
               where  t2.CDCARGO = t1.CDCARGO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INDICADOR". Cannot delete parent "CARGO".'
          goto error
       end
    
    /*  Delete all children in "INSPRECRCARG"  */
    delete INSPRECRCARG
    from   INSPRECRCARG t2, deleted t1
    where  t2.CDCARGO = t1.CDCARGO

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_centcust" for table "CENTCUST"  */
create trigger td_centcust on CENTCUST for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CENTCUST" if children still exist in "RATECCUS"  */
    if exists (select 1
               from   RATECCUS t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATECCUS". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "ITEMRATEIO"  */
    if exists (select 1
               from   ITEMRATEIO t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMRATEIO". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "LANCCCUSTO"  */
    if exists (select 1
               from   LANCCCUSTO t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCCCUSTO". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "SALDCCUS"  */
    if exists (select 1
               from   SALDCCUS t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SALDCCUS". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "ITEMORCA"  */
    if exists (select 1
               from   ITEMORCA t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMORCA". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "RATEIONF"  */
    if exists (select 1
               from   RATEIONF t2, deleted t1
               where  t2.CDCENTCUSNF = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATEIONF". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "PRODUTO"  */
    if exists (select 1
               from   PRODUTO t2, deleted t1
               where  t2.CDCCUSTENTR = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTO". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "PRODUTO"  */
    if exists (select 1
               from   PRODUTO t2, deleted t1
               where  t2.CDCCUSTSAID = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTO". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "PRODFILI"  */
    if exists (select 1
               from   PRODFILI t2, deleted t1
               where  t2.CDCCUSTENTRF = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFILI". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "PRODFILI"  */
    if exists (select 1
               from   PRODFILI t2, deleted t1
               where  t2.CDCCUSTSAIDF = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFILI". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "EMPRESA"  */
    if exists (select 1
               from   EMPRESA t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMPRESA". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "RATCTRECEBER"  */
    if exists (select 1
               from   RATCTRECEBER t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATCTRECEBER". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "RATCTPAGAR"  */
    if exists (select 1
               from   RATCTPAGAR t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATCTPAGAR". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "RATTPDEBI"  */
    if exists (select 1
               from   RATTPDEBI t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATTPDEBI". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "RATTPCRED"  */
    if exists (select 1
               from   RATTPCRED t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATTPCRED". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "FILIAL"  */
    if exists (select 1
               from   FILIAL t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIAL". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "SETOR"  */
    if exists (select 1
               from   SETOR t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SETOR". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "RETIRADAESTO"  */
    if exists (select 1
               from   RETIRADAESTO t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RETIRADAESTO". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "REQUHOSP"  */
    if exists (select 1
               from   REQUHOSP t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUHOSP". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "RETIHOSP"  */
    if exists (select 1
               from   RETIHOSP t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RETIHOSP". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "APURFATUGH"  */
    if exists (select 1
               from   APURFATUGH t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURFATUGH". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "ITREQUHOSP"  */
    if exists (select 1
               from   ITREQUHOSP t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITREQUHOSP". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "ITLANCTOEST"  */
    if exists (select 1
               from   ITLANCTOEST t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITLANCTOEST". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "DESPHOSP"  */
    if exists (select 1
               from   DESPHOSP t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESPHOSP". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "REQUESTO"  */
    if exists (select 1
               from   REQUESTO t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUESTO". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "ALMOXARIFADO"  */
    if exists (select 1
               from   ALMOXARIFADO t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ALMOXARIFADO". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "LOCALESTOQUE"  */
    if exists (select 1
               from   LOCALESTOQUE t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOCALESTOQUE". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "PRODCENTCUST"  */
    if exists (select 1
               from   PRODCENTCUST t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODCENTCUST". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "CENTCUSTBEM"  */
    if exists (select 1
               from   CENTCUSTBEM t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CENTCUSTBEM". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "APRCUSTOP"  */
    if exists (select 1
               from   APRCUSTOP t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APRCUSTOP". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "IMPOCONTRETI"  */
    if exists (select 1
               from   IMPOCONTRETI t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOCONTRETI". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "LOJA"  */
    if exists (select 1
               from   LOJA t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOJA". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Cannot delete parent "CENTCUST" if children still exist in "SOLICITASUPRI"  */
    if exists (select 1
               from   SOLICITASUPRI t2, deleted t1
               where  t2.CDCENTCUST = t1.CDCENTCUST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICITASUPRI". Cannot delete parent "CENTCUST".'
          goto error
       end
    
    /*  Delete all children in "RATTPAJR"  */
    delete RATTPAJR
    from   RATTPAJR t2, deleted t1
    where  t2.CDCENTCUST = t1.CDCENTCUST
    
    /*  Delete all children in "RATTPAJP"  */
    delete RATTPAJP
    from   RATTPAJP t2, deleted t1
    where  t2.CDCENTCUST = t1.CDCENTCUST
    
    /*  Delete all children in "RATTPCTP"  */
    delete RATTPCTP
    from   RATTPCTP t2, deleted t1
    where  t2.CDCENTCUST = t1.CDCENTCUST
    
    /*  Delete all children in "RATTPCTR"  */
    delete RATTPCTR
    from   RATTPCTR t2, deleted t1
    where  t2.CDCENTCUST = t1.CDCENTCUST

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_ciafili" for table "CIAFILI"  */
create trigger td_ciafili on CIAFILI for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CIAFILI" if children still exist in "PREFIXOCIA"  */
    if exists (select 1
               from   PREFIXOCIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREFIXOCIA". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "CICLOCIA"  */
    if exists (select 1
               from   CICLOCIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CICLOCIA". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "FRCICLOCIA"  */
    if exists (select 1
               from   FRCICLOCIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FRCICLOCIA". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "PRODUTOCIA"  */
    if exists (select 1
               from   PRODUTOCIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTOCIA". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "PERFECHAM"  */
    if exists (select 1
               from   PERFECHAM t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERFECHAM". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "CUSTORECIA"  */
    if exists (select 1
               from   CUSTORECIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTORECIA". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "TABPRECOSE"  */
    if exists (select 1
               from   TABPRECOSE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TABPRECOSE". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "VOO"  */
    if exists (select 1
               from   VOO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VOO". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "CLASSECIA"  */
    if exists (select 1
               from   CLASSECIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLASSECIA". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "EQUIPCIA"  */
    if exists (select 1
               from   EQUIPCIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQUIPCIA". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "DESCMENSCIA"  */
    if exists (select 1
               from   DESCMENSCIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESCMENSCIA". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "DESPERCIA"  */
    if exists (select 1
               from   DESPERCIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESPERCIA". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "PRECOSECIA"  */
    if exists (select 1
               from   PRECOSECIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRECOSECIA". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "SERVPLANO"  */
    if exists (select 1
               from   SERVPLANO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVPLANO". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "PADEQUIP"  */
    if exists (select 1
               from   PADEQUIP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PADEQUIP". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "PADPREFIXO"  */
    if exists (select 1
               from   PADPREFIXO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PADPREFIXO". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "PADVOOTRE"  */
    if exists (select 1
               from   PADVOOTRE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PADVOOTRE". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "FRCICLOSER"  */
    if exists (select 1
               from   FRCICLOSER t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FRCICLOSER". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "PROPSERVICO"  */
    if exists (select 1
               from   PROPSERVICO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPSERVICO". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "BREAKVOOTRE"  */
    if exists (select 1
               from   BREAKVOOTRE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BREAKVOOTRE". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "CUSTOPROCIA"  */
    if exists (select 1
               from   CUSTOPROCIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOPROCIA". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "ESPEPROD"  */
    if exists (select 1
               from   ESPEPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESPEPROD". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "MANUALCIA"  */
    if exists (select 1
               from   MANUALCIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MANUALCIA". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Cannot delete parent "CIAFILI" if children still exist in "LOCALFATUCIA"  */
    if exists (select 1
               from   LOCALFATUCIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOCALFATUCIA". Cannot delete parent "CIAFILI".'
          goto error
       end
    
    /*  Delete all children in "TRABNUTRIREC"  */
    delete TRABNUTRIREC
    from   TRABNUTRIREC t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDCIA = t1.CDCIA

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_clienfilial" for table "CLIENFILIAL"  */
create trigger td_clienfilial on CLIENFILIAL for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CLIENFILIAL" if children still exist in "GRUPSERVFAT"  */
    if exists (select 1
               from   GRUPSERVFAT t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GRUPSERVFAT". Cannot delete parent "CLIENFILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENFILIAL" if children still exist in "SERVCLIE"  */
    if exists (select 1
               from   SERVCLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCLIE". Cannot delete parent "CLIENFILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENFILIAL" if children still exist in "EFECLIFAT"  */
    if exists (select 1
               from   EFECLIFAT t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFECLIFAT". Cannot delete parent "CLIENFILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENFILIAL" if children still exist in "RESTITREFTUR"  */
    if exists (select 1
               from   RESTITREFTUR t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESTITREFTUR". Cannot delete parent "CLIENFILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENFILIAL" if children still exist in "CLIENROTA"  */
    if exists (select 1
               from   CLIENROTA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENROTA". Cannot delete parent "CLIENFILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENFILIAL" if children still exist in "EQUIPAPROD"  */
    if exists (select 1
               from   EQUIPAPROD t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQUIPAPROD". Cannot delete parent "CLIENFILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENFILIAL" if children still exist in "EFECLIFATTUR"  */
    if exists (select 1
               from   EFECLIFATTUR t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFECLIFATTUR". Cannot delete parent "CLIENFILIAL".'
          goto error
       end
    
    /*  Delete all children in "ITEMREFECLIE"  */
    delete ITEMREFECLIE
    from   ITEMREFECLIE t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "ITEMPRECOCLI"  */
    delete ITEMPRECOCLI
    from   ITEMPRECOCLI t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "CALEFATURVMN"  */
    delete CALEFATURVMN
    from   CALEFATURVMN t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDFILIAL = t1.CDFILIAL

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_clienrota" for table "CLIENROTA"  */
create trigger td_clienrota on CLIENROTA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "EQUIPCLIROTA"  */
    delete EQUIPCLIROTA
    from   EQUIPCLIROTA t2, deleted t1
    where  t2.CDROTATRANS = t1.CDROTATRANS
     and   t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDFILIAL = t1.CDFILIAL

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_cliente" for table "CLIENTE"  */
create trigger td_cliente on CLIENTE for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CLIENTE" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "EFETCLIE"  */
    if exists (select 1
               from   EFETCLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFETCLIE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "BOLETRECEBER"  */
    if exists (select 1
               from   BOLETRECEBER t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BOLETRECEBER". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "CLIENFILIAL"  */
    if exists (select 1
               from   CLIENFILIAL t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENFILIAL". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "CTRLAPURFAT"  */
    if exists (select 1
               from   CTRLAPURFAT t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLAPURFAT". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "LICENCAUSO"  */
    if exists (select 1
               from   LICENCAUSO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LICENCAUSO". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "SISTEMACLIE"  */
    if exists (select 1
               from   SISTEMACLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SISTEMACLIE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PEDIDO"  */
    if exists (select 1
               from   PEDIDO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDO". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "VENDA"  */
    if exists (select 1
               from   VENDA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "CONTATO"  */
    if exists (select 1
               from   CONTATO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTATO". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "SERVCONTRAT"  */
    if exists (select 1
               from   SERVCONTRAT t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCONTRAT". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "EXPORTCLIE"  */
    if exists (select 1
               from   EXPORTCLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXPORTCLIE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "DUPLICATA"  */
    if exists (select 1
               from   DUPLICATA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DUPLICATA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "CONSUMIDOR"  */
    if exists (select 1
               from   CONSUMIDOR t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSUMIDOR". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "MOVICLIE"  */
    if exists (select 1
               from   MOVICLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVICLIE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PARAVEND"  */
    if exists (select 1
               from   PARAVEND t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAVEND". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PARAMINTEGRA"  */
    if exists (select 1
               from   PARAMINTEGRA t2, deleted t1
               where  t2.CDCLIENTEPDV = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMINTEGRA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "ACAO"  */
    if exists (select 1
               from   ACAO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACAO". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "BANCOPREF"  */
    if exists (select 1
               from   BANCOPREF t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BANCOPREF". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PREVENTRADA"  */
    if exists (select 1
               from   PREVENTRADA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVENTRADA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "FREQALUNO"  */
    if exists (select 1
               from   FREQALUNO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FREQALUNO". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "ESCOCLIE"  */
    if exists (select 1
               from   ESCOCLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESCOCLIE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "EMPRESTPROD"  */
    if exists (select 1
               from   EMPRESTPROD t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMPRESTPROD". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "RESERVAPROD"  */
    if exists (select 1
               from   RESERVAPROD t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESERVAPROD". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "OPERADOR"  */
    if exists (select 1
               from   OPERADOR t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERADOR". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "MATRICULA"  */
    if exists (select 1
               from   MATRICULA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MATRICULA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PARTATIVSALA"  */
    if exists (select 1
               from   PARTATIVSALA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARTATIVSALA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "RESERVATURMA"  */
    if exists (select 1
               from   RESERVATURMA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESERVATURMA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "MOVOCORR"  */
    if exists (select 1
               from   MOVOCORR t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOCORR". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "ESTINTERECLI"  */
    if exists (select 1
               from   ESTINTERECLI t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESTINTERECLI". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "DEPENDCLIE"  */
    if exists (select 1
               from   DEPENDCLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DEPENDCLIE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "NEGOCIACA"  */
    if exists (select 1
               from   NEGOCIACA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NEGOCIACA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "COMENSAL"  */
    if exists (select 1
               from   COMENSAL t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMENSAL". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "LOGEQUIPPRO"  */
    if exists (select 1
               from   LOGEQUIPPRO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGEQUIPPRO". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "MAPABASEV"  */
    if exists (select 1
               from   MAPABASEV t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPABASEV". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "DESTSRVCLI"  */
    if exists (select 1
               from   DESTSRVCLI t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESTSRVCLI". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "CONVENIO"  */
    if exists (select 1
               from   CONVENIO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONVENIO". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "AUDITORIA"  */
    if exists (select 1
               from   AUDITORIA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUDITORIA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "CARDACLI"  */
    if exists (select 1
               from   CARDACLI t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDACLI". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "SERVTRAN"  */
    if exists (select 1
               from   SERVTRAN t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVTRAN". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "ETIQUETRAN"  */
    if exists (select 1
               from   ETIQUETRAN t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETIQUETRAN". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "TIPOCONSCLI"  */
    if exists (select 1
               from   TIPOCONSCLI t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOCONSCLI". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PARCLIEPRVMN"  */
    if exists (select 1
               from   PARCLIEPRVMN t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARCLIEPRVMN". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "REFECLIE"  */
    if exists (select 1
               from   REFECLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REFECLIE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PREVENDA"  */
    if exists (select 1
               from   PREVENDA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVENDA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "MOVCAIXA"  */
    if exists (select 1
               from   MOVCAIXA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVCAIXA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PROPOSTA"  */
    if exists (select 1
               from   PROPOSTA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPOSTA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PARFILIALLOJ"  */
    if exists (select 1
               from   PARFILIALLOJ t2, deleted t1
               where  t2.CDCLIEPADORC = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALLOJ". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "RELACOPER"  */
    if exists (select 1
               from   RELACOPER t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELACOPER". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "TAREFA"  */
    if exists (select 1
               from   TAREFA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAREFA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PRODSUBCLIE"  */
    if exists (select 1
               from   PRODSUBCLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODSUBCLIE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "VENDAVMN"  */
    if exists (select 1
               from   VENDAVMN t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDAVMN". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PARGERALVMN"  */
    if exists (select 1
               from   PARGERALVMN t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARGERALVMN". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PREVENDAEQU"  */
    if exists (select 1
               from   PREVENDAEQU t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVENDAEQU". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "CHEQUERECE"  */
    if exists (select 1
               from   CHEQUERECE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CHEQUERECE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "FORNECEDOR"  */
    if exists (select 1
               from   FORNECEDOR t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORNECEDOR". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PRECOBASECLI"  */
    if exists (select 1
               from   PRECOBASECLI t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRECOBASECLI". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "RELAT_MANDATO"  */
    if exists (select 1
               from   RELAT_MANDATO t2, deleted t1
               where  t2.CDCLIENTEMA = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELAT_MANDATO". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "CTRLTPRECEB"  */
    if exists (select 1
               from   CTRLTPRECEB t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLTPRECEB". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "TROCA"  */
    if exists (select 1
               from   TROCA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TROCA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "SOCIOCLIENTE"  */
    if exists (select 1
               from   SOCIOCLIENTE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOCIOCLIENTE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "LOJA"  */
    if exists (select 1
               from   LOJA t2, deleted t1
               where  t2.CDCLIENTERAT = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOJA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "REAJSERVFAT"  */
    if exists (select 1
               from   REAJSERVFAT t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REAJSERVFAT". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "CXAIMPFIS"  */
    if exists (select 1
               from   CXAIMPFIS t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CXAIMPFIS". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PRODCLIE"  */
    if exists (select 1
               from   PRODCLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODCLIE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PROSPECT"  */
    if exists (select 1
               from   PROSPECT t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROSPECT". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "SERVCLIEPME"  */
    if exists (select 1
               from   SERVCLIEPME t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCLIEPME". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "SLACLIENTE"  */
    if exists (select 1
               from   SLACLIENTE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SLACLIENTE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PARAMPEDIDO"  */
    if exists (select 1
               from   PARAMPEDIDO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPEDIDO". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PAR_FILIAL_GEP"  */
    if exists (select 1
               from   PAR_FILIAL_GEP t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PAR_FILIAL_GEP". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "LOGSOLISLA"  */
    if exists (select 1
               from   LOGSOLISLA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGSOLISLA". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "TRADIDICLIE"  */
    if exists (select 1
               from   TRADIDICLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TRADIDICLIE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "INVOICE"  */
    if exists (select 1
               from   INVOICE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVOICE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "VENDAREST"  */
    if exists (select 1
               from   VENDAREST t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDAREST". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PROPOSTAFAS"  */
    if exists (select 1
               from   PROPOSTAFAS t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPOSTAFAS". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "CONTRATOLINP"  */
    if exists (select 1
               from   CONTRATOLINP t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTRATOLINP". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "CONTRATOSERV"  */
    if exists (select 1
               from   CONTRATOSERV t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTRATOSERV". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PEDILINPCLIE"  */
    if exists (select 1
               from   PEDILINPCLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDILINPCLIE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PEDISERVCLIE"  */
    if exists (select 1
               from   PEDISERVCLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDISERVCLIE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "EFETDIET"  */
    if exists (select 1
               from   EFETDIET t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFETDIET". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "APURFATUFAS"  */
    if exists (select 1
               from   APURFATUFAS t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURFATUFAS". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "EMPENHO"  */
    if exists (select 1
               from   EMPENHO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMPENHO". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "ORCAEVENTO"  */
    if exists (select 1
               from   ORCAEVENTO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAEVENTO". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PARCLIEFAS"  */
    if exists (select 1
               from   PARCLIEFAS t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARCLIEFAS". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "ETLRESULTDES"  */
    if exists (select 1
               from   ETLRESULTDES t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLRESULTDES". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "ORCAEVENTO"  */
    if exists (select 1
               from   ORCAEVENTO t2, deleted t1
               where  t2.CDCLICONTORC = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAEVENTO". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "APROATIVTARE"  */
    if exists (select 1
               from   APROATIVTARE t2, deleted t1
               where  t2.CDCLIEATEND = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APROATIVTARE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "AGENDASALAO"  */
    if exists (select 1
               from   AGENDASALAO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENDASALAO". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PARAMDESCOREV"  */
    if exists (select 1
               from   PARAMDESCOREV t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMDESCOREV". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PERMUTAORE"  */
    if exists (select 1
               from   PERMUTAORE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERMUTAORE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "UTILBANCSOLU"  */
    if exists (select 1
               from   UTILBANCSOLU t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "UTILBANCSOLU". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "ETLFATUINVOICE"  */
    if exists (select 1
               from   ETLFATUINVOICE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLFATUINVOICE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "MOVCREDBANC"  */
    if exists (select 1
               from   MOVCREDBANC t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVCREDBANC". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "MOVDEBBANC"  */
    if exists (select 1
               from   MOVDEBBANC t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVDEBBANC". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "ETLCONTAREC"  */
    if exists (select 1
               from   ETLCONTAREC t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCONTAREC". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "OBSRELACCLIE"  */
    if exists (select 1
               from   OBSRELACCLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSRELACCLIE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PARAMINTEGRAIBB"  */
    if exists (select 1
               from   PARAMINTEGRAIBB t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMINTEGRAIBB". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "PROJETO"  */
    if exists (select 1
               from   PROJETO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROJETO". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "NFRELACCDRS"  */
    if exists (select 1
               from   NFRELACCDRS t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFRELACCDRS". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "SOLICITASUPRI"  */
    if exists (select 1
               from   SOLICITASUPRI t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICITASUPRI". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "MOVCAIXADLV"  */
    if exists (select 1
               from   MOVCAIXADLV t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVCAIXADLV". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "ITCOMANDAPEND"  */
    if exists (select 1
               from   ITCOMANDAPEND t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITCOMANDAPEND". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "CLIENTE" if children still exist in "CLIENTE"  */
    if exists (select 1
               from   CLIENTE t2, deleted t1
               where  t2.CDCLIEPRINCIPAL = t1.CDCLIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENTE". Cannot delete parent "CLIENTE".'
          goto error
       end
    
    /*  Delete all children in "COMPANHIA"  */
    delete COMPANHIA
    from   COMPANHIA t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
    
    /*  Delete all children in "ENDECLIE"  */
    delete ENDECLIE
    from   ENDECLIE t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
    
    /*  Delete all children in "INTECLIE"  */
    delete INTECLIE
    from   INTECLIE t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
    
    /*  Delete all children in "OBSERVCLIE"  */
    delete OBSERVCLIE
    from   OBSERVCLIE t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
    
    /*  Delete all children in "PRAZOPGTCLIE"  */
    delete PRAZOPGTCLIE
    from   PRAZOPGTCLIE t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
    
    /*  Delete all children in "CCUSCLIE"  */
    delete CCUSCLIE
    from   CCUSCLIE t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
    
    /*  Delete all children in "PARCLIEVMN"  */
    delete PARCLIEVMN
    from   PARCLIEVMN t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
    
    /*  Delete all children in "OPERCLIE"  */
    delete OPERCLIE
    from   OPERCLIE t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
    
    /*  Delete all children in "ITEMORCACLIE"  */
    delete ITEMORCACLIE
    from   ITEMORCACLIE t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
    
    /*  Delete all children in "HISTQUALCLIE"  */
    delete HISTQUALCLIE
    from   HISTQUALCLIE t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
    
    /*  Delete all children in "EXPFILCLIE"  */
    delete EXPFILCLIE
    from   EXPFILCLIE t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
    
    /*  Delete all children in "RESTPRODCLIE"  */
    delete RESTPRODCLIE
    from   RESTPRODCLIE t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
    
    /*  Delete all children in "PREFPRODCLIE"  */
    delete PREFPRODCLIE
    from   PREFPRODCLIE t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_codfiscoper" for table "CODFISCOPER"  */
create trigger td_codfiscoper on CODFISCOPER for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "MENSCFOP"  */
    delete MENSCFOP
    from   MENSCFOP t2, deleted t1
    where  t2.NRINCIDCFOP = t1.NRINCIDCFOP

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_codsitutrib" for table "CODSITUTRIB"  */
create trigger td_codsitutrib on CODSITUTRIB for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CODSITUTRIB" if children still exist in "CODSITUTRIBNFE"  */
    if exists (select 1
               from   CODSITUTRIBNFE t2, deleted t1
               where  t2.NRINCIDCST = t1.NRINCIDCST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODSITUTRIBNFE". Cannot delete parent "CODSITUTRIB".'
          goto error
       end
    
    /*  Delete all children in "MENSCST"  */
    delete MENSCST
    from   MENSCST t2, deleted t1
    where  t2.NRINCIDCST = t1.NRINCIDCST

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_coletapr" for table "COLETAPR"  */
create trigger td_coletapr on COLETAPR for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "COLETAPR" if children still exist in "PARAMPRODSUP"  */
    if exists (select 1
               from   PARAMPRODSUP t2, deleted t1
               where  t2.CDFILCOLETA = t1.CDFILIAL
                and   t2.NRCOLEPRECO = t1.NRCOLEPRECO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPRODSUP". Cannot delete parent "COLETAPR".'
          goto error
       end
    
    /*  Cannot delete parent "COLETAPR" if children still exist in "PARTFILI"  */
    if exists (select 1
               from   PARTFILI t2, deleted t1
               where  t2.CDFILCOTA = t1.CDFILIAL
                and   t2.NRCOLEPRECO = t1.NRCOLEPRECO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARTFILI". Cannot delete parent "COLETAPR".'
          goto error
       end
    
    /*  Delete all children in "ITEMCOLE"  */
    delete ITEMCOLE
    from   ITEMCOLE t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRCOLEPRECO = t1.NRCOLEPRECO

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_compra" for table "COMPRA"  */
create trigger td_compra on COMPRA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "COMPRA" if children still exist in "ITMAPURPREC"  */
    if exists (select 1
               from   ITMAPURPREC t2, deleted t1
               where  t2.NRCOMPRA = t1.NRCOMPRA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITMAPURPREC". Cannot delete parent "COMPRA".'
          goto error
       end
    
    /*  Cannot delete parent "COMPRA" if children still exist in "COLETAPR"  */
    if exists (select 1
               from   COLETAPR t2, deleted t1
               where  t2.NRCOMPRA = t1.NRCOMPRA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COLETAPR". Cannot delete parent "COMPRA".'
          goto error
       end
    
    /*  Delete all children in "PRODCOMPRA"  */
    delete PRODCOMPRA
    from   PRODCOMPRA t2, deleted t1
    where  t2.NRCOMPRA = t1.NRCOMPRA
    
    /*  Delete all children in "FORPRCOMPRA"  */
    delete FORPRCOMPRA
    from   FORPRCOMPRA t2, deleted t1
    where  t2.NRCOMPRA = t1.NRCOMPRA

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_comprador" for table "COMPRADOR"  */
create trigger td_comprador on COMPRADOR for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "COMPRADOR" if children still exist in "AUTOFORNE"  */
    if exists (select 1
               from   AUTOFORNE t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUTOFORNE". Cannot delete parent "COMPRADOR".'
          goto error
       end
    
    /*  Cannot delete parent "COMPRADOR" if children still exist in "COLETAPR"  */
    if exists (select 1
               from   COLETAPR t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COLETAPR". Cannot delete parent "COMPRADOR".'
          goto error
       end
    
    /*  Cannot delete parent "COMPRADOR" if children still exist in "SOLICITA"  */
    if exists (select 1
               from   SOLICITA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICITA". Cannot delete parent "COMPRADOR".'
          goto error
       end
    
    /*  Cannot delete parent "COMPRADOR" if children still exist in "LIMPRODAF"  */
    if exists (select 1
               from   LIMPRODAF t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LIMPRODAF". Cannot delete parent "COMPRADOR".'
          goto error
       end
    
    /*  Cannot delete parent "COMPRADOR" if children still exist in "PREVLISTA"  */
    if exists (select 1
               from   PREVLISTA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVLISTA". Cannot delete parent "COMPRADOR".'
          goto error
       end
    
    /*  Cannot delete parent "COMPRADOR" if children still exist in "PEDCOLETA"  */
    if exists (select 1
               from   PEDCOLETA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDCOLETA". Cannot delete parent "COMPRADOR".'
          goto error
       end
    
    /*  Cannot delete parent "COMPRADOR" if children still exist in "COMPROPER"  */
    if exists (select 1
               from   COMPROPER t2, deleted t1
               where  t2.CDCOMPRADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPROPER". Cannot delete parent "COMPRADOR".'
          goto error
       end
    
    /*  Cannot delete parent "COMPRADOR" if children still exist in "APURPRECO"  */
    if exists (select 1
               from   APURPRECO t2, deleted t1
               where  t2.CDCOMPRADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURPRECO". Cannot delete parent "COMPRADOR".'
          goto error
       end
    
    /*  Cannot delete parent "COMPRADOR" if children still exist in "COMPRA"  */
    if exists (select 1
               from   COMPRA t2, deleted t1
               where  t2.CDCOMPRADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPRA". Cannot delete parent "COMPRADOR".'
          goto error
       end
    
    /*  Cannot delete parent "COMPRADOR" if children still exist in "PEDCOLGERAL"  */
    if exists (select 1
               from   PEDCOLGERAL t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDCOLGERAL". Cannot delete parent "COMPRADOR".'
          goto error
       end
    
    /*  Cannot delete parent "COMPRADOR" if children still exist in "LOCK_LISTA"  */
    if exists (select 1
               from   LOCK_LISTA t2, deleted t1
               where  t2.CDCOMPRADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOCK_LISTA". Cannot delete parent "COMPRADOR".'
          goto error
       end
    
    /*  Delete all children in "PRODCOMP"  */
    delete PRODCOMP
    from   PRODCOMP t2, deleted t1
    where  t2.CDOPERADOR = t1.CDOPERADOR
    
    /*  Delete all children in "GERECOMP"  */
    delete GERECOMP
    from   GERECOMP t2, deleted t1
    where  t2.CDOPERCOMP = t1.CDOPERADOR

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_compreluser" for table "COMPRELUSER"  */
create trigger td_compreluser on COMPRELUSER for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "COMPRELUSER" if children still exist in "RELACCAMPO"  */
    if exists (select 1
               from   RELACCAMPO t2, deleted t1
               where  t2.CDRELATORIO = t1.CDRELATORIO
                and   t2.CDCAMPRELAC = t1.CDCAMPO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELACCAMPO". Cannot delete parent "COMPRELUSER".'
          goto error
       end
    
    /*  Delete all children in "CONSRELUSER"  */
    delete CONSRELUSER
    from   CONSRELUSER t2, deleted t1
    where  t2.CDRELATORIO = t1.CDRELATORIO
     and   t2.CDCAMPO = t1.CDCAMPO
    
    /*  Delete all children in "VALIDRELUSER"  */
    delete VALIDRELUSER
    from   VALIDRELUSER t2, deleted t1
    where  t2.CDRELATORIO = t1.CDRELATORIO
     and   t2.CDCAMPO = t1.CDCAMPO
    
    /*  Delete all children in "RELACCAMPO"  */
    delete RELACCAMPO
    from   RELACCAMPO t2, deleted t1
    where  t2.CDRELATORIO = t1.CDRELATORIO
     and   t2.CDCAMPO = t1.CDCAMPO

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_confequippro" for table "CONFEQUIPPRO"  */
create trigger td_confequippro on CONFEQUIPPRO for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "ITEQUIPPRO"  */
    delete ITEQUIPPRO
    from   ITEQUIPPRO t2, deleted t1
    where  t2.CDEQUIPPRO = t1.CDEQUIPPRO
     and   t2.DTINVIGCNFEP = t1.DTINVIGCNFEP

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_conspaci" for table "CONSPACI"  */
create trigger td_conspaci on CONSPACI for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CONSPACI" if children still exist in "METOAVAL"  */
    if exists (select 1
               from   METOAVAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE
                and   t2.DTCONSPACI = t1.DTCONSPACI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "METOAVAL". Cannot delete parent "CONSPACI".'
          goto error
       end
    
    /*  Cannot delete parent "CONSPACI" if children still exist in "PLAEXERFISI"  */
    if exists (select 1
               from   PLAEXERFISI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE
                and   t2.DTCONSPACI = t1.DTCONSPACI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PLAEXERFISI". Cannot delete parent "CONSPACI".'
          goto error
       end
    
    /*  Cannot delete parent "CONSPACI" if children still exist in "PLCALORICO"  */
    if exists (select 1
               from   PLCALORICO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE
                and   t2.DTCONSPACI = t1.DTCONSPACI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PLCALORICO". Cannot delete parent "CONSPACI".'
          goto error
       end
    
    /*  Cannot delete parent "CONSPACI" if children still exist in "NUTRECOMPACI"  */
    if exists (select 1
               from   NUTRECOMPACI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE
                and   t2.DTCONSPACI = t1.DTCONSPACI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NUTRECOMPACI". Cannot delete parent "CONSPACI".'
          goto error
       end
    
    /*  Delete all children in "ATENREAL"  */
    delete ATENREAL
    from   ATENREAL t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTCONSPACI = t1.DTCONSPACI
    
    /*  Delete all children in "EXAMREAL"  */
    delete EXAMREAL
    from   EXAMREAL t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTCONSPACI = t1.DTCONSPACI
    
    /*  Delete all children in "AVALNUTR"  */
    delete AVALNUTR
    from   AVALNUTR t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTCONSPACI = t1.DTCONSPACI
    
    /*  Delete all children in "DIAGPACI"  */
    delete DIAGPACI
    from   DIAGPACI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTCONSPACI = t1.DTCONSPACI
    
    /*  Delete all children in "CARDPACI"  */
    delete CARDPACI
    from   CARDPACI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTCONSPACI = t1.DTCONSPACI
    
    /*  Delete all children in "FREQALIMREAL"  */
    delete FREQALIMREAL
    from   FREQALIMREAL t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTCONSPACI = t1.DTCONSPACI
    
    /*  Delete all children in "LAUDO"  */
    delete LAUDO
    from   LAUDO t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTCONSPACI = t1.DTCONSPACI

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_consreluser" for table "CONSRELUSER"  */
create trigger td_consreluser on CONSRELUSER for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "CAMPCONSREL"  */
    delete CAMPCONSREL
    from   CAMPCONSREL t2, deleted t1
    where  t2.CDRELATORIO = t1.CDRELATORIO
     and   t2.CDCAMPO = t1.CDCAMPO

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_conssolol" for table "CONSSOLOL"  */
create trigger td_conssolol on CONSSOLOL for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CONSSOLOL" if children still exist in "ITEMSOLI"  */
    if exists (select 1
               from   ITEMSOLI t2, deleted t1
               where  t2.NRSEQUCONSOL = t1.NRSEQUCONSOL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMSOLI". Cannot delete parent "CONSSOLOL".'
          goto error
       end
    
    /*  Delete all children in "PRODCONSSOLOL"  */
    delete PRODCONSSOLOL
    from   PRODCONSSOLOL t2, deleted t1
    where  t2.NRSEQUCONSOL = t1.NRSEQUCONSOL
    
    /*  Delete all children in "JUSTCONSSOLOL"  */
    delete JUSTCONSSOLOL
    from   JUSTCONSSOLOL t2, deleted t1
    where  t2.NRSEQUCONSOL = t1.NRSEQUCONSOL

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_consultadw" for table "CONSULTADW"  */
create trigger td_consultadw on CONSULTADW for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CONSULTADW" if children still exist in "CONFIGURADW"  */
    if exists (select 1
               from   CONFIGURADW t2, deleted t1
               where  t2.CDCONSULTADW = t1.CDCONSULTADW)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONFIGURADW". Cannot delete parent "CONSULTADW".'
          goto error
       end
    
    /*  Delete all children in "CONSDWGRUPOP"  */
    delete CONSDWGRUPOP
    from   CONSDWGRUPOP t2, deleted t1
    where  t2.CDCONSULTADW = t1.CDCONSULTADW

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_consumidor" for table "CONSUMIDOR"  */
create trigger td_consumidor on CONSUMIDOR for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CONSUMIDOR" if children still exist in "VENDA"  */
    if exists (select 1
               from   VENDA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDCONSUMIDOR = t1.CDCONSUMIDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDA". Cannot delete parent "CONSUMIDOR".'
          goto error
       end
    
    /*  Cannot delete parent "CONSUMIDOR" if children still exist in "MOVICLIE"  */
    if exists (select 1
               from   MOVICLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDCONSUMIDOR = t1.CDCONSUMIDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVICLIE". Cannot delete parent "CONSUMIDOR".'
          goto error
       end
    
    /*  Cannot delete parent "CONSUMIDOR" if children still exist in "PREVENDA"  */
    if exists (select 1
               from   PREVENDA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDCONSUMIDOR = t1.CDCONSUMIDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVENDA". Cannot delete parent "CONSUMIDOR".'
          goto error
       end
    
    /*  Cannot delete parent "CONSUMIDOR" if children still exist in "SALDOCONS"  */
    if exists (select 1
               from   SALDOCONS t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDCONSUMIDOR = t1.CDCONSUMIDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SALDOCONS". Cannot delete parent "CONSUMIDOR".'
          goto error
       end
    
    /*  Cannot delete parent "CONSUMIDOR" if children still exist in "EXTRATOCONS"  */
    if exists (select 1
               from   EXTRATOCONS t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDCONSUMIDOR = t1.CDCONSUMIDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXTRATOCONS". Cannot delete parent "CONSUMIDOR".'
          goto error
       end
    
    /*  Cannot delete parent "CONSUMIDOR" if children still exist in "PEDICREDCONS"  */
    if exists (select 1
               from   PEDICREDCONS t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDCONSUMIDOR = t1.CDCONSUMIDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDICREDCONS". Cannot delete parent "CONSUMIDOR".'
          goto error
       end
    
    /*  Cannot delete parent "CONSUMIDOR" if children still exist in "VENDA"  */
    if exists (select 1
               from   VENDA t2, deleted t1
               where  t2.CDCLIAUXVEND = t1.CDCLIENTE
                and   t2.CDCONAUXVEND = t1.CDCONSUMIDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDA". Cannot delete parent "CONSUMIDOR".'
          goto error
       end
    
    /*  Cannot delete parent "CONSUMIDOR" if children still exist in "RALIMENT"  */
    if exists (select 1
               from   RALIMENT t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDCONSUMIDOR = t1.CDCONSUMIDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RALIMENT". Cannot delete parent "CONSUMIDOR".'
          goto error
       end
    
    /*  Cannot delete parent "CONSUMIDOR" if children still exist in "VENDAREST"  */
    if exists (select 1
               from   VENDAREST t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDCONSUMIDOR = t1.CDCONSUMIDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDAREST". Cannot delete parent "CONSUMIDOR".'
          goto error
       end
    
    /*  Cannot delete parent "CONSUMIDOR" if children still exist in "MOVCAIXADLV"  */
    if exists (select 1
               from   MOVCAIXADLV t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDCONSUMIDOR = t1.CDCONSUMIDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVCAIXADLV". Cannot delete parent "CONSUMIDOR".'
          goto error
       end
    
    /*  Cannot delete parent "CONSUMIDOR" if children still exist in "ITCOMANDAPEND"  */
    if exists (select 1
               from   ITCOMANDAPEND t2, deleted t1
               where  t2.CON_CDCLIENTE = t1.CDCLIENTE
                and   t2.CDCONSUMIDOR = t1.CDCONSUMIDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITCOMANDAPEND". Cannot delete parent "CONSUMIDOR".'
          goto error
       end
    
    /*  Delete all children in "LOGTRMOVCONS"  */
    delete LOGTRMOVCONS
    from   LOGTRMOVCONS t2, deleted t1
    where  t2.CDCLIENDE = t1.CDCLIENTE
     and   t2.CDCONSDE = t1.CDCONSUMIDOR
    
    /*  Delete all children in "LOGTRMOVCONS"  */
    delete LOGTRMOVCONS
    from   LOGTRMOVCONS t2, deleted t1
    where  t2.CDCLIENPARA = t1.CDCLIENTE
     and   t2.CDCONSPARA = t1.CDCONSUMIDOR

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_contapagar" for table "CONTAPAGAR"  */
create trigger td_contapagar on CONTAPAGAR for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CONTAPAGAR" if children still exist in "AJCTPAGAR"  */
    if exists (select 1
               from   AJCTPAGAR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF
                and   t2.NRSEQVENPAG = t1.NRSEQVENPAG
                and   t2.NRPARCELAPAG = t1.NRPARCELAPAG)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJCTPAGAR". Cannot delete parent "CONTAPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTAPAGAR" if children still exist in "ADIANTAPAGAR"  */
    if exists (select 1
               from   ADIANTAPAGAR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF
                and   t2.NRSEQVENPAG = t1.NRSEQVENPAG
                and   t2.NRPARCELAPAG = t1.NRPARCELAPAG)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ADIANTAPAGAR". Cannot delete parent "CONTAPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTAPAGAR" if children still exist in "CONTAGERADA"  */
    if exists (select 1
               from   CONTAGERADA t2, deleted t1
               where  t2.CDFILIALPAG = t1.CDFILIAL
                and   t2.NRLANCTOPAG = t1.NRLANCTONF
                and   t2.NRSEQVENPAG = t1.NRSEQVENPAG
                and   t2.NRPARCELAPAG = t1.NRPARCELAPAG)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTAGERADA". Cannot delete parent "CONTAPAGAR".'
          goto error
       end
    
    /*  Delete all children in "RATCTPAGAR"  */
    delete RATCTPAGAR
    from   RATCTPAGAR t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
     and   t2.NRSEQVENPAG = t1.NRSEQVENPAG
     and   t2.NRPARCELAPAG = t1.NRPARCELAPAG
    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
     and   t2.NRSEQVENPAG = t1.NRSEQVENPAG
     and   t2.NRPARCELAPAG = t1.NRPARCELAPAG

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_contareceber" for table "CONTARECEBER"  */
create trigger td_contareceber on CONTARECEBER for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CONTARECEBER" if children still exist in "AJCTRECEBER"  */
    if exists (select 1
               from   AJCTRECEBER t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF
                and   t2.NRSEQVENREC = t1.NRSEQVENREC
                and   t2.NRPARCELAREC = t1.NRPARCELAREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJCTRECEBER". Cannot delete parent "CONTARECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "CONTARECEBER" if children still exist in "PARCELAVENDA"  */
    if exists (select 1
               from   PARCELAVENDA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF
                and   t2.NRSEQVENREC = t1.NRSEQVENREC
                and   t2.NRPARCELAREC = t1.NRPARCELAREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARCELAVENDA". Cannot delete parent "CONTARECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "CONTARECEBER" if children still exist in "ADIANTARECEB"  */
    if exists (select 1
               from   ADIANTARECEB t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF
                and   t2.NRSEQVENREC = t1.NRSEQVENREC
                and   t2.NRPARCELAREC = t1.NRPARCELAREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ADIANTARECEB". Cannot delete parent "CONTARECEBER".'
          goto error
       end
    
    /*  Delete all children in "RATCTRECEBER"  */
    delete RATCTRECEBER
    from   RATCTRECEBER t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
     and   t2.NRSEQVENREC = t1.NRSEQVENREC
     and   t2.NRPARCELAREC = t1.NRPARCELAREC
    
    /*  Delete all children in "CONTAGERADA"  */
    delete CONTAGERADA
    from   CONTAGERADA t2, deleted t1
    where  t2.CDFILIALREC = t1.CDFILIAL
     and   t2.NRLANCTOREC = t1.NRLANCTONF
     and   t2.NRSEQVENREC = t1.NRSEQVENREC
     and   t2.NRPARCELAREC = t1.NRPARCELAREC
    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
     and   t2.NRSEQVENREC = t1.NRSEQVENREC
     and   t2.NRPARCELAREC = t1.NRPARCELAREC

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_contcorr" for table "CONTCORR"  */
create trigger td_contcorr on CONTCORR for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CONTCORR" if children still exist in "BAIXARECEBIDA"  */
    if exists (select 1
               from   BAIXARECEBIDA t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BAIXARECEBIDA". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "ITTRANSFBANC"  */
    if exists (select 1
               from   ITTRANSFBANC t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITTRANSFBANC". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "SDOCCORR"  */
    if exists (select 1
               from   SDOCCORR t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SDOCCORR". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "MOVBANCARIO"  */
    if exists (select 1
               from   MOVBANCARIO t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVBANCARIO". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "MOVDEBBANC"  */
    if exists (select 1
               from   MOVDEBBANC t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVDEBBANC". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "MOVCREDBANC"  */
    if exists (select 1
               from   MOVCREDBANC t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVCREDBANC". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "BAIXAPAGA"  */
    if exists (select 1
               from   BAIXAPAGA t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BAIXAPAGA". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "CHEQUE"  */
    if exists (select 1
               from   CHEQUE t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CHEQUE". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "TALAOCHEQUE"  */
    if exists (select 1
               from   TALAOCHEQUE t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TALAOCHEQUE". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "PARAMFILIAL"  */
    if exists (select 1
               from   PARAMFILIAL t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMFILIAL". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "EXTRBANCO"  */
    if exists (select 1
               from   EXTRBANCO t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXTRBANCO". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "BOLETPAGAR"  */
    if exists (select 1
               from   BOLETPAGAR t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BOLETPAGAR". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "BOLETRECEBER"  */
    if exists (select 1
               from   BOLETRECEBER t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BOLETRECEBER". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "CNABPAG"  */
    if exists (select 1
               from   CNABPAG t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CNABPAG". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "CNABREC"  */
    if exists (select 1
               from   CNABREC t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CNABREC". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "SERVCONTRAT"  */
    if exists (select 1
               from   SERVCONTRAT t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCONTRAT". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "PARAMEMPRESA"  */
    if exists (select 1
               from   PARAMEMPRESA t2, deleted t1
               where  t2.CDREDUGBEMPR = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMEMPRESA". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "PARAMFILIAL"  */
    if exists (select 1
               from   PARAMFILIAL t2, deleted t1
               where  t2.CDREDUGBFILI = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMFILIAL". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "BANCOPREF"  */
    if exists (select 1
               from   BANCOPREF t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BANCOPREF". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "TIPOCTRECFIL"  */
    if exists (select 1
               from   TIPOCTRECFIL t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOCTRECFIL". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "CODBARBOLETO"  */
    if exists (select 1
               from   CODBARBOLETO t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODBARBOLETO". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "CHEQUERECE"  */
    if exists (select 1
               from   CHEQUERECE t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CHEQUERECE". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "PARCTEMISBOL"  */
    if exists (select 1
               from   PARCTEMISBOL t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARCTEMISBOL". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "CTRLEMISBOL"  */
    if exists (select 1
               from   CTRLEMISBOL t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLEMISBOL". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "CONCIMOVBANC"  */
    if exists (select 1
               from   CONCIMOVBANC t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONCIMOVBANC". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "TIPORECE"  */
    if exists (select 1
               from   TIPORECE t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPORECE". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "BANCO"  */
    if exists (select 1
               from   BANCO t2, deleted t1
               where  t2.CDCONTCORVND = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BANCO". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "PARAVEND"  */
    if exists (select 1
               from   PARAVEND t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAVEND". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "FILIAL"  */
    if exists (select 1
               from   FILIAL t2, deleted t1
               where  t2.CDREDUCONT = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIAL". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Cannot delete parent "CONTCORR" if children still exist in "CNABREC"  */
    if exists (select 1
               from   CNABREC t2, deleted t1
               where  t2.CDREDUCONTCOBR = t1.CDREDUCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CNABREC". Cannot delete parent "CONTCORR".'
          goto error
       end
    
    /*  Delete all children in "JUROSCHEQUE"  */
    delete JUROSCHEQUE
    from   JUROSCHEQUE t2, deleted t1
    where  t2.CDREDUCONT = t1.CDREDUCONT
    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.CDREDUCONT = t1.CDREDUCONT

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_contratolinp" for table "CONTRATOLINP"  */
create trigger td_contratolinp on CONTRATOLINP for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CONTRATOLINP" if children still exist in "PEDILINPPROP"  */
    if exists (select 1
               from   PEDILINPPROP t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDLINPCLIE = t1.CDLINPCLIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDILINPPROP". Cannot delete parent "CONTRATOLINP".'
          goto error
       end
    
    /*  Cannot delete parent "CONTRATOLINP" if children still exist in "LINPLUSO"  */
    if exists (select 1
               from   LINPLUSO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDLINPCLIE = t1.CDLINPCLIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LINPLUSO". Cannot delete parent "CONTRATOLINP".'
          goto error
       end
    
    /*  Cannot delete parent "CONTRATOLINP" if children still exist in "PEDILINPCLIE"  */
    if exists (select 1
               from   PEDILINPCLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDLINPCLIE = t1.CDLINPCLIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDILINPCLIE". Cannot delete parent "CONTRATOLINP".'
          goto error
       end
    
    /*  Cannot delete parent "CONTRATOLINP" if children still exist in "MENSLINPCLIE"  */
    if exists (select 1
               from   MENSLINPCLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDLINPCLIE = t1.CDLINPCLIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MENSLINPCLIE". Cannot delete parent "CONTRATOLINP".'
          goto error
       end
    
    /*  Cannot delete parent "CONTRATOLINP" if children still exist in "APURFATUFAS"  */
    if exists (select 1
               from   APURFATUFAS t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDLINPCLIE = t1.CDLINPCLIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURFATUFAS". Cannot delete parent "CONTRATOLINP".'
          goto error
       end
    
    /*  Delete all children in "SISTCONTLINP"  */
    delete SISTCONTLINP
    from   SISTCONTLINP t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDLINPCLIE = t1.CDLINPCLIE
    
    /*  Delete all children in "VALORLINP"  */
    delete VALORLINP
    from   VALORLINP t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDLINPCLIE = t1.CDLINPCLIE
    
    /*  Delete all children in "VENCLUSOCLIE"  */
    delete VENCLUSOCLIE
    from   VENCLUSOCLIE t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDLINPCLIE = t1.CDLINPCLIE

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_contratoserv" for table "CONTRATOSERV"  */
create trigger td_contratoserv on CONTRATOSERV for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CONTRATOSERV" if children still exist in "AGENDASERV"  */
    if exists (select 1
               from   AGENDASERV t2, deleted t1
               where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
                and   t2.NRVERPROPFAS = t1.NRVERPROPFAS
                and   t2.CDTIPOSERV = t1.CDTIPOSERV
                and   t2.NRSERVCONT = t1.NRSERVCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENDASERV". Cannot delete parent "CONTRATOSERV".'
          goto error
       end
    
    /*  Cannot delete parent "CONTRATOSERV" if children still exist in "CHT"  */
    if exists (select 1
               from   CHT t2, deleted t1
               where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
                and   t2.NRVERPROPFAS = t1.NRVERPROPFAS
                and   t2.CDTIPOSERV = t1.CDTIPOSERV
                and   t2.NRSERVCONT = t1.NRSERVCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CHT". Cannot delete parent "CONTRATOSERV".'
          goto error
       end
    
    /*  Delete all children in "VALORSERV"  */
    delete VALORSERV
    from   VALORSERV t2, deleted t1
    where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
     and   t2.NRVERPROPFAS = t1.NRVERPROPFAS
     and   t2.CDTIPOSERV = t1.CDTIPOSERV
     and   t2.NRSERVCONT = t1.NRSERVCONT

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_critavafinal" for table "CRITAVAFINAL"  */
create trigger td_critavafinal on CRITAVAFINAL for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CRITAVAFINAL" if children still exist in "AUDITORIA"  */
    if exists (select 1
               from   AUDITORIA t2, deleted t1
               where  t2.CDTIPAUDIT = t1.CDTIPAUDIT
                and   t2.CDCRITAVAFIN = t1.CDCRITAVAFIN)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUDITORIA". Cannot delete parent "CRITAVAFINAL".'
          goto error
       end
    
    /*  Delete all children in "INSPAVAFINAL"  */
    delete INSPAVAFINAL
    from   INSPAVAFINAL t2, deleted t1
    where  t2.CDTIPAUDIT = t1.CDTIPAUDIT
     and   t2.CDCRITAVAFIN = t1.CDCRITAVAFIN

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_ctrlapurfat" for table "CTRLAPURFAT"  */
create trigger td_ctrlapurfat on CTRLAPURFAT for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "CTRLAPURFAT" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDFILIAPFATU = t1.CDFILIAL
                and   t2.NRAPFATUTNF = t1.NRAPURACFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "CTRLAPURFAT".'
          goto error
       end
    
    /*  Cannot delete parent "CTRLAPURFAT" if children still exist in "EFECLIFAT"  */
    if exists (select 1
               from   EFECLIFAT t2, deleted t1
               where  t2.CDFILIFATU = t1.CDFILIAL
                and   t2.NRAPURACFAT = t1.NRAPURACFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFECLIFAT". Cannot delete parent "CTRLAPURFAT".'
          goto error
       end
    
    /*  Cannot delete parent "CTRLAPURFAT" if children still exist in "EFETCLIE"  */
    if exists (select 1
               from   EFETCLIE t2, deleted t1
               where  t2.CDFILIFATU = t1.CDFILIAL
                and   t2.NRAPURACFAT = t1.NRAPURACFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFETCLIE". Cannot delete parent "CTRLAPURFAT".'
          goto error
       end
    
    /*  Cannot delete parent "CTRLAPURFAT" if children still exist in "APURFATUGH"  */
    if exists (select 1
               from   APURFATUGH t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRAPURACFAT = t1.NRAPURACFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURFATUGH". Cannot delete parent "CTRLAPURFAT".'
          goto error
       end
    
    /*  Cannot delete parent "CTRLAPURFAT" if children still exist in "MOVICLIE"  */
    if exists (select 1
               from   MOVICLIE t2, deleted t1
               where  t2.CDFILIAPFATU = t1.CDFILIAL
                and   t2.NRAPURACFATU = t1.NRAPURACFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVICLIE". Cannot delete parent "CTRLAPURFAT".'
          goto error
       end
    
    /*  Cannot delete parent "CTRLAPURFAT" if children still exist in "RELAT_MANDATO"  */
    if exists (select 1
               from   RELAT_MANDATO t2, deleted t1
               where  t2.CDFILIAPURMA = t1.CDFILIAL
                and   t2.NRAPURACFAMA = t1.NRAPURACFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELAT_MANDATO". Cannot delete parent "CTRLAPURFAT".'
          goto error
       end
    
    /*  Cannot delete parent "CTRLAPURFAT" if children still exist in "EFETCDRS"  */
    if exists (select 1
               from   EFETCDRS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRAPURACFAT = t1.NRAPURACFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFETCDRS". Cannot delete parent "CTRLAPURFAT".'
          goto error
       end
    
    /*  Cannot delete parent "CTRLAPURFAT" if children still exist in "EFETRATCDRS"  */
    if exists (select 1
               from   EFETRATCDRS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRAPURACFAT = t1.NRAPURACFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFETRATCDRS". Cannot delete parent "CTRLAPURFAT".'
          goto error
       end
    
    /*  Cannot delete parent "CTRLAPURFAT" if children still exist in "EFETCLIEPME"  */
    if exists (select 1
               from   EFETCLIEPME t2, deleted t1
               where  t2.CDFILIFATU = t1.CDFILIAL
                and   t2.NRAPURACFAT = t1.NRAPURACFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFETCLIEPME". Cannot delete parent "CTRLAPURFAT".'
          goto error
       end
    
    /*  Cannot delete parent "CTRLAPURFAT" if children still exist in "CTRLAPCDFR"  */
    if exists (select 1
               from   CTRLAPCDFR t2, deleted t1
               where  t2.CDFILIALCENT = t1.CDFILIAL
                and   t2.NRAPURACFATU = t1.NRAPURACFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLAPCDFR". Cannot delete parent "CTRLAPURFAT".'
          goto error
       end
    
    /*  Cannot delete parent "CTRLAPURFAT" if children still exist in "ITEMPRODUTO"  */
    if exists (select 1
               from   ITEMPRODUTO t2, deleted t1
               where  t2.CDFILIAPFATU = t1.CDFILIAL
                and   t2.NRAPFATUTNF = t1.NRAPURACFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPRODUTO". Cannot delete parent "CTRLAPURFAT".'
          goto error
       end
    
    /*  Cannot delete parent "CTRLAPURFAT" if children still exist in "ROMANEIO"  */
    if exists (select 1
               from   ROMANEIO t2, deleted t1
               where  t2.CDFILIFATUR = t1.CDFILIAL
                and   t2.NRAPURACFAT = t1.NRAPURACFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROMANEIO". Cannot delete parent "CTRLAPURFAT".'
          goto error
       end
    
    /*  Cannot delete parent "CTRLAPURFAT" if children still exist in "ITEMROMANEIO"  */
    if exists (select 1
               from   ITEMROMANEIO t2, deleted t1
               where  t2.CDFILIFATUR = t1.CDFILIAL
                and   t2.NRAPURACFAT = t1.NRAPURACFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMROMANEIO". Cannot delete parent "CTRLAPURFAT".'
          goto error
       end
    
    /*  Cannot delete parent "CTRLAPURFAT" if children still exist in "NFRELACCDRS"  */
    if exists (select 1
               from   NFRELACCDRS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRAPURACFAT = t1.NRAPURACFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFRELACCDRS". Cannot delete parent "CTRLAPURFAT".'
          goto error
       end
    
    /*  Delete all children in "NOTAFISCAL"  */
    delete NOTAFISCAL
    from   NOTAFISCAL t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRAPURACFAT = t1.NRAPURACFAT
    
    /*  Delete all children in "MRDPAPUR"  */
    delete MRDPAPUR
    from   MRDPAPUR t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRAPURACFAT = t1.NRAPURACFAT
    
    /*  Delete all children in "MRDPNTFI"  */
    delete MRDPNTFI
    from   MRDPNTFI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRAPURACFAT = t1.NRAPURACFAT
    
    /*  Delete all children in "CTRAPUITPROD"  */
    delete CTRAPUITPROD
    from   CTRAPUITPROD t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRAPURACFAT = t1.NRAPURACFAT
    
    /*  Delete all children in "RELAT_CDRS"  */
    delete RELAT_CDRS
    from   RELAT_CDRS t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRAPURACFAT = t1.NRAPURACFAT

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_custpadr" for table "CUSTPADR"  */
create trigger td_custpadr on CUSTPADR for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "CUSTPADRTP"  */
    delete CUSTPADRTP
    from   CUSTPADRTP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDSERVFILI = t1.CDSERVFILI
     and   t2.DTREFCUSPADR = t1.DTREFCUSPADR

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_departamento" for table "DEPARTAMENTO"  */
create trigger td_departamento on DEPARTAMENTO for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "DEPARTAMENTO" if children still exist in "MOVFUNCRH"  */
    if exists (select 1
               from   MOVFUNCRH t2, deleted t1
               where  t2.CDDEPTO = t1.CDDEPTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVFUNCRH". Cannot delete parent "DEPARTAMENTO".'
          goto error
       end
    
    /*  Cannot delete parent "DEPARTAMENTO" if children still exist in "SETOR"  */
    if exists (select 1
               from   SETOR t2, deleted t1
               where  t2.CDDEPTO = t1.CDDEPTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SETOR". Cannot delete parent "DEPARTAMENTO".'
          goto error
       end
    
    /*  Cannot delete parent "DEPARTAMENTO" if children still exist in "CANDIDATO"  */
    if exists (select 1
               from   CANDIDATO t2, deleted t1
               where  t2.CDDEPTO = t1.CDDEPTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CANDIDATO". Cannot delete parent "DEPARTAMENTO".'
          goto error
       end
    
    /*  Cannot delete parent "DEPARTAMENTO" if children still exist in "FUNCIONARIO"  */
    if exists (select 1
               from   FUNCIONARIO t2, deleted t1
               where  t2.CDDEPTO = t1.CDDEPTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FUNCIONARIO". Cannot delete parent "DEPARTAMENTO".'
          goto error
       end
    
    /*  Cannot delete parent "DEPARTAMENTO" if children still exist in "OBSINTORCAEV"  */
    if exists (select 1
               from   OBSINTORCAEV t2, deleted t1
               where  t2.CDDEPTO = t1.CDDEPTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSINTORCAEV". Cannot delete parent "DEPARTAMENTO".'
          goto error
       end
    
    /*  Cannot delete parent "DEPARTAMENTO" if children still exist in "PRODUTO"  */
    if exists (select 1
               from   PRODUTO t2, deleted t1
               where  t2.CDDEPTPEDORC = t1.CDDEPTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTO". Cannot delete parent "DEPARTAMENTO".'
          goto error
       end
    
    /*  Cannot delete parent "DEPARTAMENTO" if children still exist in "PEDIDO"  */
    if exists (select 1
               from   PEDIDO t2, deleted t1
               where  t2.CDDEPTO = t1.CDDEPTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDO". Cannot delete parent "DEPARTAMENTO".'
          goto error
       end
    
    /*  Cannot delete parent "DEPARTAMENTO" if children still exist in "ACERTOORCEVE"  */
    if exists (select 1
               from   ACERTOORCEVE t2, deleted t1
               where  t2.CDDEPTO = t1.CDDEPTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACERTOORCEVE". Cannot delete parent "DEPARTAMENTO".'
          goto error
       end
    
    /*  Cannot delete parent "DEPARTAMENTO" if children still exist in "NFRELACORC"  */
    if exists (select 1
               from   NFRELACORC t2, deleted t1
               where  t2.CDDEPTO = t1.CDDEPTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFRELACORC". Cannot delete parent "DEPARTAMENTO".'
          goto error
       end
    
    /*  Cannot delete parent "DEPARTAMENTO" if children still exist in "DEVOLORCAEVEN"  */
    if exists (select 1
               from   DEVOLORCAEVEN t2, deleted t1
               where  t2.CDDEPTO = t1.CDDEPTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DEVOLORCAEVEN". Cannot delete parent "DEPARTAMENTO".'
          goto error
       end
    
    /*  Delete all children in "INSPRECRDEPT"  */
    delete INSPRECRDEPT
    from   INSPRECRDEPT t2, deleted t1
    where  t2.CDDEPTO = t1.CDDEPTO

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_diagnost" for table "DIAGNOST"  */
create trigger td_diagnost on DIAGNOST for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "DIAGNOST" if children still exist in "DIAGPACI"  */
    if exists (select 1
               from   DIAGPACI t2, deleted t1
               where  t2.CDTABECDI = t1.CDTABECDI
                and   t2.CDDIAGNOST = t1.CDDIAGNOST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIAGPACI". Cannot delete parent "DIAGNOST".'
          goto error
       end
    
    /*  Cannot delete parent "DIAGNOST" if children still exist in "DIAGINTER"  */
    if exists (select 1
               from   DIAGINTER t2, deleted t1
               where  t2.CDTABECDI = t1.CDTABECDI
                and   t2.CDDIAGNOST = t1.CDDIAGNOST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIAGINTER". Cannot delete parent "DIAGNOST".'
          goto error
       end
    
    /*  Cannot delete parent "DIAGNOST" if children still exist in "RESTDIAG"  */
    if exists (select 1
               from   RESTDIAG t2, deleted t1
               where  t2.CDTABECDI = t1.CDTABECDI
                and   t2.CDDIAGNOST = t1.CDDIAGNOST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESTDIAG". Cannot delete parent "DIAGNOST".'
          goto error
       end
    
    /*  Cannot delete parent "DIAGNOST" if children still exist in "AGRUCARD"  */
    if exists (select 1
               from   AGRUCARD t2, deleted t1
               where  t2.CDTABECDI = t1.CDTABECDI
                and   t2.CDDIAGNOST = t1.CDDIAGNOST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGRUCARD". Cannot delete parent "DIAGNOST".'
          goto error
       end
    
    /*  Cannot delete parent "DIAGNOST" if children still exist in "CONSNUTR"  */
    if exists (select 1
               from   CONSNUTR t2, deleted t1
               where  t2.CDTABECDI = t1.CDTABECDI
                and   t2.CDDIAGNOST = t1.CDDIAGNOST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSNUTR". Cannot delete parent "DIAGNOST".'
          goto error
       end
    
    /*  Cannot delete parent "DIAGNOST" if children still exist in "PARADIETO"  */
    if exists (select 1
               from   PARADIETO t2, deleted t1
               where  t2.CDTABECDI = t1.CDTABECDI
                and   t2.CDDIAGNOST = t1.CDDIAGNOST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARADIETO". Cannot delete parent "DIAGNOST".'
          goto error
       end
    
    /*  Delete all children in "DIAGNUTR"  */
    delete DIAGNUTR
    from   DIAGNUTR t2, deleted t1
    where  t2.CDTABECDI = t1.CDTABECDI
     and   t2.CDDIAGNOST = t1.CDDIAGNOST

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_dietpadr" for table "DIETPADR"  */
create trigger td_dietpadr on DIETPADR for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "DIETPADR" if children still exist in "DIETSERV"  */
    if exists (select 1
               from   DIETSERV t2, deleted t1
               where  t2.CDDIETPAD = t1.CDDIETPAD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIETSERV". Cannot delete parent "DIETPADR".'
          goto error
       end
    
    /*  Cannot delete parent "DIETPADR" if children still exist in "CARDADIA"  */
    if exists (select 1
               from   CARDADIA t2, deleted t1
               where  t2.CDDIETPAD = t1.CDDIETPAD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDADIA". Cannot delete parent "DIETPADR".'
          goto error
       end
    
    /*  Cannot delete parent "DIETPADR" if children still exist in "PRESDIET"  */
    if exists (select 1
               from   PRESDIET t2, deleted t1
               where  t2.CDDIETPAD = t1.CDDIETPAD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRESDIET". Cannot delete parent "DIETPADR".'
          goto error
       end
    
    /*  Cannot delete parent "DIETPADR" if children still exist in "DIETPADR"  */
    if exists (select 1
               from   DIETPADR t2, deleted t1
               where  t2.CDDIETGRUP = t1.CDDIETPAD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIETPADR". Cannot delete parent "DIETPADR".'
          goto error
       end
    
    /*  Cannot delete parent "DIETPADR" if children still exist in "SERVINTE"  */
    if exists (select 1
               from   SERVINTE t2, deleted t1
               where  t2.CDDIETPAD = t1.CDDIETPAD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVINTE". Cannot delete parent "DIETPADR".'
          goto error
       end
    
    /*  Cannot delete parent "DIETPADR" if children still exist in "SERVINTE"  */
    if exists (select 1
               from   SERVINTE t2, deleted t1
               where  t2.CDDIETGRUPSI = t1.CDDIETPAD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVINTE". Cannot delete parent "DIETPADR".'
          goto error
       end
    
    /*  Cannot delete parent "DIETPADR" if children still exist in "SETOPRODCARD"  */
    if exists (select 1
               from   SETOPRODCARD t2, deleted t1
               where  t2.CDDIETPAD = t1.CDDIETPAD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SETOPRODCARD". Cannot delete parent "DIETPADR".'
          goto error
       end
    
    /*  Cannot delete parent "DIETPADR" if children still exist in "CONSPACI"  */
    if exists (select 1
               from   CONSPACI t2, deleted t1
               where  t2.CDDIETPAD = t1.CDDIETPAD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSPACI". Cannot delete parent "DIETPADR".'
          goto error
       end
    
    /*  Cannot delete parent "DIETPADR" if children still exist in "EFETDIET"  */
    if exists (select 1
               from   EFETDIET t2, deleted t1
               where  t2.CDDIETPAD = t1.CDDIETPAD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFETDIET". Cannot delete parent "DIETPADR".'
          goto error
       end
    
    /*  Cannot delete parent "DIETPADR" if children still exist in "ESTATDIET"  */
    if exists (select 1
               from   ESTATDIET t2, deleted t1
               where  t2.CDDIETPAD = t1.CDDIETPAD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESTATDIET". Cannot delete parent "DIETPADR".'
          goto error
       end
    
    /*  Delete all children in "RECDIEPAD"  */
    delete RECDIEPAD
    from   RECDIEPAD t2, deleted t1
    where  t2.CDDIETPAD = t1.CDDIETPAD
    
    /*  Delete all children in "CARACTPDIE"  */
    delete CARACTPDIE
    from   CARACTPDIE t2, deleted t1
    where  t2.CDDIETPAD = t1.CDDIETPAD

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_dimedwgrupop" for table "DIMEDWGRUPOP"  */
create trigger td_dimedwgrupop on DIMEDWGRUPOP for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "FILDIMEGRUPOP"  */
    delete FILDIMEGRUPOP
    from   FILDIMEGRUPOP t2, deleted t1
    where  t2.NRSEQUEDIME = t1.NRSEQUEDIME
     and   t2.CDGRUPOPER = t1.CDGRUPOPER

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_dimensaodime" for table "DIMENSAODIME"  */
create trigger td_dimensaodime on DIMENSAODIME for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "DIMENSAODIME" if children still exist in "DIMENSAODW"  */
    if exists (select 1
               from   DIMENSAODW t2, deleted t1
               where  t2.NRSEQUEDIME = t1.NRSEQUEDIME)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIMENSAODW". Cannot delete parent "DIMENSAODIME".'
          goto error
       end
    
    /*  Cannot delete parent "DIMENSAODIME" if children still exist in "DIMENSAODRILL"  */
    if exists (select 1
               from   DIMENSAODRILL t2, deleted t1
               where  t2.NRSEQUEDIME = t1.NRSEQUEDIME)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIMENSAODRILL". Cannot delete parent "DIMENSAODIME".'
          goto error
       end
    
    /*  Cannot delete parent "DIMENSAODIME" if children still exist in "ATRIBEXTRADIME"  */
    if exists (select 1
               from   ATRIBEXTRADIME t2, deleted t1
               where  t2.NRSEQUEDIME = t1.NRSEQUEDIME)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ATRIBEXTRADIME". Cannot delete parent "DIMENSAODIME".'
          goto error
       end
    
    /*  Delete all children in "DIMEODWOPER"  */
    delete DIMEODWOPER
    from   DIMEODWOPER t2, deleted t1
    where  t2.NRSEQUEDIME = t1.NRSEQUEDIME
    
    /*  Delete all children in "DIMEDWGRUPOP"  */
    delete DIMEDWGRUPOP
    from   DIMEDWGRUPOP t2, deleted t1
    where  t2.NRSEQUEDIME = t1.NRSEQUEDIME

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_dimeodwoper" for table "DIMEODWOPER"  */
create trigger td_dimeodwoper on DIMEODWOPER for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "FILDIMEOPER"  */
    delete FILDIMEOPER
    from   FILDIMEOPER t2, deleted t1
    where  t2.NRSEQUEDIME = t1.NRSEQUEDIME
     and   t2.CDOPERADOR = t1.CDOPERADOR

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_duplicata" for table "DUPLICATA"  */
create trigger td_duplicata on DUPLICATA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "DUPLICATA" if children still exist in "CONTARECEBER"  */
    if exists (select 1
               from   CONTARECEBER t2, deleted t1
               where  t2.CDFILIALDUP = t1.CDFILIAL
                and   t2.NRLANCTODUP = t1.NRLANCTODUP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTARECEBER". Cannot delete parent "DUPLICATA".'
          goto error
       end
    
    /*  Cannot delete parent "DUPLICATA" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDFILIALDUP = t1.CDFILIAL
                and   t2.NRLANCTODUP = t1.NRLANCTODUP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "DUPLICATA".'
          goto error
       end
    
    /*  Delete all children in "AJDUPBOLET"  */
    delete AJDUPBOLET
    from   AJDUPBOLET t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTODUP = t1.NRLANCTODUP

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_empresa" for table "EMPRESA"  */
create trigger td_empresa on EMPRESA for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "EMPRESA" if children still exist in "FILIAL"  */
    if exists (select 1
               from   FILIAL t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIAL". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "INCIDENCIA"  */
    if exists (select 1
               from   INCIDENCIA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INCIDENCIA". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "AUTOFORNE"  */
    if exists (select 1
               from   AUTOFORNE t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUTOFORNE". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "ALIQUOTA"  */
    if exists (select 1
               from   ALIQUOTA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ALIQUOTA". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "INSCRMUNICIP"  */
    if exists (select 1
               from   INSCRMUNICIP t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSCRMUNICIP". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "INSCRESTAD"  */
    if exists (select 1
               from   INSCRESTAD t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSCRESTAD". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "CODFISCOPER"  */
    if exists (select 1
               from   CODFISCOPER t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODFISCOPER". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "CODSITUTRIB"  */
    if exists (select 1
               from   CODSITUTRIB t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODSITUTRIB". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "SALDEMPR"  */
    if exists (select 1
               from   SALDEMPR t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SALDEMPR". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "ORCAMENT"  */
    if exists (select 1
               from   ORCAMENT t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAMENT". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "TERMOABERT"  */
    if exists (select 1
               from   TERMOABERT t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TERMOABERT". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "SALDCCUS"  */
    if exists (select 1
               from   SALDCCUS t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SALDCCUS". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "LANCCONT"  */
    if exists (select 1
               from   LANCCONT t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCCONT". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "CONTOPECTBL"  */
    if exists (select 1
               from   CONTOPECTBL t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTOPECTBL". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "SALDCOME"  */
    if exists (select 1
               from   SALDCOME t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SALDCOME". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "CAPALOTE"  */
    if exists (select 1
               from   CAPALOTE t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CAPALOTE". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "MOVFLUXO"  */
    if exists (select 1
               from   MOVFLUXO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVFLUXO". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "TRANSFBANC"  */
    if exists (select 1
               from   TRANSFBANC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TRANSFBANC". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "BAIXARECEBIDA"  */
    if exists (select 1
               from   BAIXARECEBIDA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BAIXARECEBIDA". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "BOLETRECEBER"  */
    if exists (select 1
               from   BOLETRECEBER t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BOLETRECEBER". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "CONTCORR"  */
    if exists (select 1
               from   CONTCORR t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTCORR". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "BAIXAPAGA"  */
    if exists (select 1
               from   BAIXAPAGA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BAIXAPAGA". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "BOLETPAGAR"  */
    if exists (select 1
               from   BOLETPAGAR t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BOLETPAGAR". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "TERMABERENC"  */
    if exists (select 1
               from   TERMABERENC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TERMABERENC". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "MAPAAPURAC"  */
    if exists (select 1
               from   MAPAAPURAC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPAAPURAC". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "INVENTFISC"  */
    if exists (select 1
               from   INVENTFISC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVENTFISC". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "PARAMEMPRESA"  */
    if exists (select 1
               from   PARAMEMPRESA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMEMPRESA". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "EMITENTEAF"  */
    if exists (select 1
               from   EMITENTEAF t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMITENTEAF". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "ITEMAUTF"  */
    if exists (select 1
               from   ITEMAUTF t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMAUTF". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "PARCMPSUP"  */
    if exists (select 1
               from   PARCMPSUP t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARCMPSUP". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "SALDTEMP"  */
    if exists (select 1
               from   SALDTEMP t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SALDTEMP". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "FUNCIONARIO"  */
    if exists (select 1
               from   FUNCIONARIO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FUNCIONARIO". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "EMPRCNTRFIL"  */
    if exists (select 1
               from   EMPRCNTRFIL t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMPRCNTRFIL". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "CNABPAG"  */
    if exists (select 1
               from   CNABPAG t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CNABPAG". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "CNABREC"  */
    if exists (select 1
               from   CNABREC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CNABREC". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "VENDA"  */
    if exists (select 1
               from   VENDA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDA". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "CANDIDATO"  */
    if exists (select 1
               from   CANDIDATO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CANDIDATO". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "PRECOUTI"  */
    if exists (select 1
               from   PRECOUTI t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRECOUTI". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "DUPLICATA"  */
    if exists (select 1
               from   DUPLICATA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DUPLICATA". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "SERVCONTRAT"  */
    if exists (select 1
               from   SERVCONTRAT t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCONTRAT". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "SERVFATU"  */
    if exists (select 1
               from   SERVFATU t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVFATU". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "EMPRBANC"  */
    if exists (select 1
               from   EMPRBANC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMPRBANC". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "EVENTO_EMPR"  */
    if exists (select 1
               from   EVENTO_EMPR t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EVENTO_EMPR". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "FOLHA"  */
    if exists (select 1
               from   FOLHA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FOLHA". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "PARAMINTEGRA"  */
    if exists (select 1
               from   PARAMINTEGRA t2, deleted t1
               where  t2.CDEMPRESAPDV = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMINTEGRA". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "LANCCEXC"  */
    if exists (select 1
               from   LANCCEXC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCCEXC". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "LANCONTEX"  */
    if exists (select 1
               from   LANCONTEX t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCONTEX". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "LANCOMEEX"  */
    if exists (select 1
               from   LANCOMEEX t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCOMEEX". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "ORCAFINA"  */
    if exists (select 1
               from   ORCAFINA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAFINA". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "PREVENTRADA"  */
    if exists (select 1
               from   PREVENTRADA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVENTRADA". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "PREVSAIDA"  */
    if exists (select 1
               from   PREVSAIDA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVSAIDA". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "LOGCOPECTBL"  */
    if exists (select 1
               from   LOGCOPECTBL t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGCOPECTBL". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "EMITAFFORN"  */
    if exists (select 1
               from   EMITAFFORN t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMITAFFORN". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "BEM"  */
    if exists (select 1
               from   BEM t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BEM". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "VRPRODBCIMPO"  */
    if exists (select 1
               from   VRPRODBCIMPO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VRPRODBCIMPO". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "CHEQUERECE"  */
    if exists (select 1
               from   CHEQUERECE t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CHEQUERECE". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "GSCRESULTADO"  */
    if exists (select 1
               from   GSCRESULTADO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GSCRESULTADO". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "INVOICE"  */
    if exists (select 1
               from   INVOICE t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVOICE". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "APURFATUFAS"  */
    if exists (select 1
               from   APURFATUFAS t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURFATUFAS". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "PEDIDOEXOL"  */
    if exists (select 1
               from   PEDIDOEXOL t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDOEXOL". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "COMPRA"  */
    if exists (select 1
               from   COMPRA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPRA". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "FATORESUFAT"  */
    if exists (select 1
               from   FATORESUFAT t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FATORESUFAT". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "ETL_RESUGER"  */
    if exists (select 1
               from   ETL_RESUGER t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUGER". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "ETL_RESUFOOD"  */
    if exists (select 1
               from   ETL_RESUFOOD t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUFOOD". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "ETL_RESUESTO"  */
    if exists (select 1
               from   ETL_RESUESTO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUESTO". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "ETL_RESUSERV"  */
    if exists (select 1
               from   ETL_RESUSERV t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUSERV". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "CONTRATOORCEVE"  */
    if exists (select 1
               from   CONTRATOORCEVE t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTRATOORCEVE". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "ETLFATUINVOICE"  */
    if exists (select 1
               from   ETLFATUINVOICE t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLFATUINVOICE". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "PARAMSERIE"  */
    if exists (select 1
               from   PARAMSERIE t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMSERIE". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "PARCLIEFAS"  */
    if exists (select 1
               from   PARCLIEFAS t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARCLIEFAS". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "ETLDRO"  */
    if exists (select 1
               from   ETLDRO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLDRO". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "ETLCOMPRASGER"  */
    if exists (select 1
               from   ETLCOMPRASGER t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCOMPRASGER". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "ETLCONTAPAG"  */
    if exists (select 1
               from   ETLCONTAPAG t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCONTAPAG". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "ETLCONTAREC"  */
    if exists (select 1
               from   ETLCONTAREC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCONTAREC". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Cannot delete parent "EMPRESA" if children still exist in "LOTENFE"  */
    if exists (select 1
               from   LOTENFE t2, deleted t1
               where  t2.CDEMPLOTENFE = t1.CDEMPRESA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOTENFE". Cannot delete parent "EMPRESA".'
          goto error
       end
    
    /*  Delete all children in "PARAMFOPAG"  */
    delete PARAMFOPAG
    from   PARAMFOPAG t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
    
    /*  Delete all children in "IMPOSEMPRE"  */
    delete IMPOSEMPRE
    from   IMPOSEMPRE t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_entregamerc" for table "ENTREGAMERC"  */
create trigger td_entregamerc on ENTREGAMERC for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "ENTREGAMERC" if children still exist in "ITEMENTREGA"  */
    if exists (select 1
               from   ITEMENTREGA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRENTREGA = t1.NRENTREGA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMENTREGA". Cannot delete parent "ENTREGAMERC".'
          goto error
       end
    
    /*  Delete all children in "RELACENTRAF"  */
    delete RELACENTRAF
    from   RELACENTRAF t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRENTREGA = t1.NRENTREGA

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_equimedi" for table "EQUIMEDI"  */
create trigger td_equimedi on EQUIMEDI for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "EQUIMEDI" if children still exist in "INTERNAC"  */
    if exists (select 1
               from   INTERNAC t2, deleted t1
               where  t2.CDEQUIMED = t1.CDEQUIMED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INTERNAC". Cannot delete parent "EQUIMEDI".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIMEDI" if children still exist in "CONSPACI"  */
    if exists (select 1
               from   CONSPACI t2, deleted t1
               where  t2.CDEQUIMED = t1.CDEQUIMED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSPACI". Cannot delete parent "EQUIMEDI".'
          goto error
       end
    
    /*  Delete all children in "EQUIMEDICO"  */
    delete EQUIMEDICO
    from   EQUIMEDICO t2, deleted t1
    where  t2.CDEQUIMED = t1.CDEQUIMED

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_equipaprod" for table "EQUIPAPROD"  */
create trigger td_equipaprod on EQUIPAPROD for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "CONFEQUIPPRO"  */
    if exists (select 1
               from   CONFEQUIPPRO t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONFEQUIPPRO". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "DISPONIBEQUI"  */
    if exists (select 1
               from   DISPONIBEQUI t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DISPONIBEQUI". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "EQUIPAPROC"  */
    if exists (select 1
               from   EQUIPAPROC t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQUIPAPROC". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "AGENEQUI"  */
    if exists (select 1
               from   AGENEQUI t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENEQUI". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "MAPABASEV"  */
    if exists (select 1
               from   MAPABASEV t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPABASEV". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "LOGEQUIPPRO"  */
    if exists (select 1
               from   LOGEQUIPPRO t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGEQUIPPRO". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "COLETDOAC"  */
    if exists (select 1
               from   COLETDOAC t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COLETDOAC". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "EQUIPCLIROTA"  */
    if exists (select 1
               from   EQUIPCLIROTA t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQUIPCLIROTA". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "INSPETAPA"  */
    if exists (select 1
               from   INSPETAPA t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPETAPA". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "LOGUTEQUIP"  */
    if exists (select 1
               from   LOGUTEQUIP t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGUTEQUIP". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "AUDITORIA"  */
    if exists (select 1
               from   AUDITORIA t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUDITORIA". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "PERDAOP"  */
    if exists (select 1
               from   PERDAOP t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERDAOP". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "AGENTARE"  */
    if exists (select 1
               from   AGENTARE t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENTARE". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "PERDAETAPA"  */
    if exists (select 1
               from   PERDAETAPA t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERDAETAPA". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "RECURETAPA"  */
    if exists (select 1
               from   RECURETAPA t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECURETAPA". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "RECURETAPAOP"  */
    if exists (select 1
               from   RECURETAPAOP t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECURETAPAOP". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "COMPOSINSPEC"  */
    if exists (select 1
               from   COMPOSINSPEC t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPOSINSPEC". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "PREVENDAEQU"  */
    if exists (select 1
               from   PREVENDAEQU t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVENDAEQU". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "MOVIVALOR"  */
    if exists (select 1
               from   MOVIVALOR t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVIVALOR". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "EQUIPAPROD"  */
    if exists (select 1
               from   EQUIPAPROD t2, deleted t1
               where  t2.CDEQUIPPAI = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQUIPAPROD". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "PAREQUIPAPRO"  */
    if exists (select 1
               from   PAREQUIPAPRO t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PAREQUIPAPRO". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EQUIPAPROD" if children still exist in "VENDAVMN"  */
    if exists (select 1
               from   VENDAVMN t2, deleted t1
               where  t2.CDEQUIPPRO = t1.CDEQUIPPRO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDAVMN". Cannot delete parent "EQUIPAPROD".'
          goto error
       end
    
    /*  Delete all children in "EQUITPCONT"  */
    delete EQUITPCONT
    from   EQUITPCONT t2, deleted t1
    where  t2.CDEQUIPPRO = t1.CDEQUIPPRO
    
    /*  Delete all children in "COLETPCONT"  */
    delete COLETPCONT
    from   COLETPCONT t2, deleted t1
    where  t2.CDEQUIPPRO = t1.CDEQUIPPRO
    
    /*  Delete all children in "DESEQUIPAPRO"  */
    delete DESEQUIPAPRO
    from   DESEQUIPAPRO t2, deleted t1
    where  t2.CDEQUIPPRO = t1.CDEQUIPPRO

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_espeprod" for table "ESPEPROD"  */
create trigger td_espeprod on ESPEPROD for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "ESPEPROD" if children still exist in "EQESPEPROD"  */
    if exists (select 1
               from   EQESPEPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO
                and   t2.CDESPEPROD = t1.CDESPEPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQESPEPROD". Cannot delete parent "ESPEPROD".'
          goto error
       end
    
    /*  Cannot delete parent "ESPEPROD" if children still exist in "COMPPROD"  */
    if exists (select 1
               from   COMPPROD t2, deleted t1
               where  t2.CDPRODUTOAUX = t1.CDPRODUTO
                and   t2.CDESPPRODAUX = t1.CDESPEPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPPROD". Cannot delete parent "ESPEPROD".'
          goto error
       end
    
    /*  Delete all children in "DOCUPROD"  */
    delete DOCUPROD
    from   DOCUPROD t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDESPEPROD = t1.CDESPEPROD
    
    /*  Delete all children in "TCOMPPRO"  */
    delete TCOMPPRO
    from   TCOMPPRO t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDESPEPROD = t1.CDESPEPROD
    
    /*  Delete all children in "VISESPEPRO"  */
    delete VISESPEPRO
    from   VISESPEPRO t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDESPEPROD = t1.CDESPEPROD
    
    /*  Delete all children in "FOTOESPEPROD"  */
    delete FOTOESPEPROD
    from   FOTOESPEPROD t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDESPEPROD = t1.CDESPEPROD

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_estruaten" for table "ESTRUATEN"  */
create trigger td_estruaten on ESTRUATEN for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "ESTRUATEN" if children still exist in "EXTRMARC"  */
    if exists (select 1
               from   EXTRMARC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC
                and   t2.CDESTRUATEN = t1.CDESTRUATEN)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXTRMARC". Cannot delete parent "ESTRUATEN".'
          goto error
       end
    
    /*  Delete all children in "ETAPATEN"  */
    delete ETAPATEN
    from   ETAPATEN t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDFUNC = t1.CDFUNC
     and   t2.CDESTRUATEN = t1.CDESTRUATEN
    
    /*  Delete all children in "RESTESTR"  */
    delete RESTESTR
    from   RESTESTR t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDFUNC = t1.CDFUNC
     and   t2.CDESTRUATEN = t1.CDESTRUATEN
    
    /*  Delete all children in "ATENREALIZ"  */
    delete ATENREALIZ
    from   ATENREALIZ t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDFUNC = t1.CDFUNC
     and   t2.CDESTRUATEN = t1.CDESTRUATEN
    
    /*  Delete all children in "RETOPROG"  */
    delete RETOPROG
    from   RETOPROG t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDFUNC = t1.CDFUNC
     and   t2.CDESTRUATEN = t1.CDESTRUATEN
    
    /*  Delete all children in "LISTAESPR"  */
    delete LISTAESPR
    from   LISTAESPR t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDFUNC = t1.CDFUNC
     and   t2.CDESTRUATEN = t1.CDESTRUATEN

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_etafabric" for table "ETAFABRIC"  */
create trigger td_etafabric on ETAFABRIC for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "INSUMETAPA"  */
    delete INSUMETAPA
    from   INSUMETAPA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDPROCFABRIC = t1.CDPROCFABRIC
     and   t2.CDETAPAPROD = t1.CDETAPAPROD
    
    /*  Delete all children in "CONETAPA"  */
    delete CONETAPA
    from   CONETAPA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDPROCFABRIC = t1.CDPROCFABRIC
     and   t2.CDETAPAPROD = t1.CDETAPAPROD
    
    /*  Delete all children in "PERDAETAPA"  */
    delete PERDAETAPA
    from   PERDAETAPA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDPROCFABRIC = t1.CDPROCFABRIC
     and   t2.CDETAPAPROD = t1.CDETAPAPROD
    
    /*  Delete all children in "INSPETAPA"  */
    delete INSPETAPA
    from   INSPETAPA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDPROCFABRIC = t1.CDPROCFABRIC
     and   t2.CDETAPAPROD = t1.CDETAPAPROD
    
    /*  Delete all children in "TEMPOETAFABR"  */
    delete TEMPOETAFABR
    from   TEMPOETAFABR t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDPROCFABRIC = t1.CDPROCFABRIC
     and   t2.CDETAPAPROD = t1.CDETAPAPROD

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_etafabricop" for table "ETAFABRICOP"  */
create trigger td_etafabricop on ETAFABRICOP for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "ETAFABRICOP" if children still exist in "SUBPRETAPAOP"  */
    if exists (select 1
               from   SUBPRETAPAOP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRORDPROD = t1.NRORDPROD
                and   t2.NRSUBORDPROD = t1.NRSUBORDPROD
                and   t2.CDETAPAPROD = t1.CDETAPAPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SUBPRETAPAOP". Cannot delete parent "ETAFABRICOP".'
          goto error
       end
    
    /*  Delete all children in "CONETAPAOP"  */
    delete CONETAPAOP
    from   CONETAPAOP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORDPROD = t1.NRORDPROD
     and   t2.NRSUBORDPROD = t1.NRSUBORDPROD
     and   t2.CDETAPAPROD = t1.CDETAPAPROD
    
    /*  Delete all children in "PERDAOP"  */
    delete PERDAOP
    from   PERDAOP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORDPROD = t1.NRORDPROD
     and   t2.NRSUBORDPROD = t1.NRSUBORDPROD
     and   t2.CDETAPAPROD = t1.CDETAPAPROD
    
    /*  Delete all children in "AUDITORIA"  */
    delete AUDITORIA
    from   AUDITORIA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORDPROD = t1.NRORDPROD
     and   t2.NRSUBORDPROD = t1.NRSUBORDPROD
     and   t2.CDETAPAPROD = t1.CDETAPAPROD
    
    /*  Delete all children in "RETIRADAESTO"  */
    delete RETIRADAESTO
    from   RETIRADAESTO t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORDPROD = t1.NRORDPROD
     and   t2.NRSUBORDPROD = t1.NRSUBORDPROD
     and   t2.CDETAPAPROD = t1.CDETAPAPROD
    
    /*  Delete all children in "APRCUSTOP"  */
    delete APRCUSTOP
    from   APRCUSTOP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORDPROD = t1.NRORDPROD
     and   t2.NRSUBORDPROD = t1.NRSUBORDPROD
     and   t2.CDETAPAPROD = t1.CDETAPAPROD

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_evolreqprod" for table "EVOLREQPROD"  */
create trigger td_evolreqprod on EVOLREQPROD for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "EVOLREQPROD" if children still exist in "TAREFA"  */
    if exists (select 1
               from   TAREFA t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODGRUP
                and   t2.CDREQPROD = t1.CDREQPROD
                and   t2.NRSEQREQPROD = t1.NRSEQREQPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAREFA". Cannot delete parent "EVOLREQPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EVOLREQPROD" if children still exist in "EVOLREQSOLI"  */
    if exists (select 1
               from   EVOLREQSOLI t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODGRUP
                and   t2.CDREQPROD = t1.CDREQPROD
                and   t2.NRSEQREQPROD = t1.NRSEQREQPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EVOLREQSOLI". Cannot delete parent "EVOLREQPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EVOLREQPROD" if children still exist in "ORCAREQPROD"  */
    if exists (select 1
               from   ORCAREQPROD t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODGRUP
                and   t2.CDREQPROD = t1.CDREQPROD
                and   t2.NRSEQREQPROD = t1.NRSEQREQPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAREQPROD". Cannot delete parent "EVOLREQPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EVOLREQPROD" if children still exist in "ACORDOREQPRO"  */
    if exists (select 1
               from   ACORDOREQPRO t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODGRUP
                and   t2.CDREQPROD = t1.CDREQPROD
                and   t2.NRSEQREQPROD = t1.NRSEQREQPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACORDOREQPRO". Cannot delete parent "EVOLREQPROD".'
          goto error
       end
    
    /*  Cannot delete parent "EVOLREQPROD" if children still exist in "LOGREQPROD"  */
    if exists (select 1
               from   LOGREQPROD t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODGRUP
                and   t2.CDREQPROD = t1.CDREQPROD
                and   t2.NRSEQREQPROD = t1.NRSEQREQPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGREQPROD". Cannot delete parent "EVOLREQPROD".'
          goto error
       end
    
    /*  Delete all children in "DESCREQPROD"  */
    delete DESCREQPROD
    from   DESCREQPROD t2, deleted t1
    where  t2.CDPRODGRUP = t1.CDPRODGRUP
     and   t2.CDREQPROD = t1.CDREQPROD
     and   t2.NRSEQREQPROD = t1.NRSEQREQPROD
    
    /*  Delete all children in "IMPACREQPROD"  */
    delete IMPACREQPROD
    from   IMPACREQPROD t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODGRUP
     and   t2.CDREQPROD = t1.CDREQPROD
     and   t2.NRSEQREQPROD = t1.NRSEQREQPROD

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_extrbanco" for table "EXTRBANCO"  */
create trigger td_extrbanco on EXTRBANCO for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Delete all children in "MOVREAL"  */
    delete MOVREAL
    from   MOVREAL t2, deleted t1
    where  t2.CDREDUCONT = t1.CDREDUCONT
     and   t2.CDSEQUEXTR = t1.CDSEQUEXTR

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_fasetram" for table "FASETRAM"  */
create trigger td_fasetram on FASETRAM for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "FASETRAM" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDFASETRAM = t1.CDFASETRAM)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "FASETRAM".'
          goto error
       end
    
    /*  Cannot delete parent "FASETRAM" if children still exist in "LOGFASETRAM"  */
    if exists (select 1
               from   LOGFASETRAM t2, deleted t1
               where  t2.CDFASETRAM = t1.CDFASETRAM)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGFASETRAM". Cannot delete parent "FASETRAM".'
          goto error
       end
    
    /*  Cannot delete parent "FASETRAM" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDFASETRAM = t1.CDFASETRAM)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "FASETRAM".'
          goto error
       end
    
    /*  Cannot delete parent "FASETRAM" if children still exist in "FASETRAMDEST"  */
    if exists (select 1
               from   FASETRAMDEST t2, deleted t1
               where  t2.CDFASTRAMDES = t1.CDFASETRAM)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FASETRAMDEST". Cannot delete parent "FASETRAM".'
          goto error
       end
    
    /*  Cannot delete parent "FASETRAM" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDFATRAMIWEB = t1.CDFASETRAM)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "FASETRAM".'
          goto error
       end
    
    /*  Cannot delete parent "FASETRAM" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDFATRAMENCE = t1.CDFASETRAM)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "FASETRAM".'
          goto error
       end
    
    /*  Cannot delete parent "FASETRAM" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDFATRAMASSU = t1.CDFASETRAM)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "FASETRAM".'
          goto error
       end
    
    /*  Delete all children in "FASETRAMDEST"  */
    delete FASETRAMDEST
    from   FASETRAMDEST t2, deleted t1
    where  t2.CDFASETRAM = t1.CDFASETRAM

    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go

/*  Delete trigger "td_filial" for table "FILIAL"  */
create trigger td_filial on FILIAL for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

    
    /*  Cannot delete parent "FILIAL" if children still exist in "FILIAL"  */
    if exists (select 1
               from   FILIAL t2, deleted t1
               where  t2.CDFILIFISC = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMFILIAL"  */
    if exists (select 1
               from   PARAMFILIAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMFILIAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRODFILI"  */
    if exists (select 1
               from   PRODFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "AUTOFORNE"  */
    if exists (select 1
               from   AUTOFORNE t2, deleted t1
               where  t2.CDFILLANC = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUTOFORNE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "INSCRESTAD"  */
    if exists (select 1
               from   INSCRESTAD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSCRESTAD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "INSCRMUNICIP"  */
    if exists (select 1
               from   INSCRMUNICIP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSCRMUNICIP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LANCTOESTOQ"  */
    if exists (select 1
               from   LANCTOESTOQ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCTOESTOQ". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "INVEROTA"  */
    if exists (select 1
               from   INVEROTA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVEROTA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FERIFILI"  */
    if exists (select 1
               from   FERIFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FERIFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FERICOMP"  */
    if exists (select 1
               from   FERICOMP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FERICOMP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMPLANEJA"  */
    if exists (select 1
               from   PARAMPLANEJA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPLANEJA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SERVICO"  */
    if exists (select 1
               from   SERVICO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVICO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RATECCUS"  */
    if exists (select 1
               from   RATECCUS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATECCUS". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ITEMRATEIO"  */
    if exists (select 1
               from   ITEMRATEIO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMRATEIO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LANCCCUSTO"  */
    if exists (select 1
               from   LANCCCUSTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCCCUSTO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SALDCCUS"  */
    if exists (select 1
               from   SALDCCUS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SALDCCUS". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "METARISL"  */
    if exists (select 1
               from   METARISL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "METARISL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CARDADIA"  */
    if exists (select 1
               from   CARDADIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDADIA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRATO"  */
    if exists (select 1
               from   PRATO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRATO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RECEFILI"  */
    if exists (select 1
               from   RECEFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FOTOPRAFIL"  */
    if exists (select 1
               from   FOTOPRAFIL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FOTOPRAFIL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRATFILI"  */
    if exists (select 1
               from   PRATFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRATFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PACIENTE"  */
    if exists (select 1
               from   PACIENTE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PACIENTE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FILIOPER"  */
    if exists (select 1
               from   FILIOPER t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIOPER". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CUSTNESTO"  */
    if exists (select 1
               from   CUSTNESTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTNESTO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREPPRAFIL"  */
    if exists (select 1
               from   PREPPRAFIL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREPPRAFIL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ITEMORCA"  */
    if exists (select 1
               from   ITEMORCA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMORCA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVFLUXO"  */
    if exists (select 1
               from   MOVFLUXO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVFLUXO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "BAIXARECEBIDA"  */
    if exists (select 1
               from   BAIXARECEBIDA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BAIXARECEBIDA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "BOLETRECEBER"  */
    if exists (select 1
               from   BOLETRECEBER t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BOLETRECEBER". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "BAIXAPAGA"  */
    if exists (select 1
               from   BAIXAPAGA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BAIXAPAGA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "BOLETPAGAR"  */
    if exists (select 1
               from   BOLETPAGAR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BOLETPAGAR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SETORCOZ"  */
    if exists (select 1
               from   SETORCOZ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SETORCOZ". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "VAGA"  */
    if exists (select 1
               from   VAGA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VAGA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TIPONOTADEB"  */
    if exists (select 1
               from   TIPONOTADEB t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPONOTADEB". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CLIENFILIAL"  */
    if exists (select 1
               from   CLIENFILIAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENFILIAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVOCORR"  */
    if exists (select 1
               from   MOVOCORR t2, deleted t1
               where  t2.CDFILILANCMO = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOCORR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "AUTOFORNE"  */
    if exists (select 1
               from   AUTOFORNE t2, deleted t1
               where  t2.CDFILDEST = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUTOFORNE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRODCMPFILI"  */
    if exists (select 1
               from   PRODCMPFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODCMPFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVOPSOL"  */
    if exists (select 1
               from   MOVOPSOL t2, deleted t1
               where  t2.CDFILLANMOV = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOPSOL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREVCOMPRA"  */
    if exists (select 1
               from   PREVCOMPRA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCOMPRA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PERFILPROD"  */
    if exists (select 1
               from   PERFILPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERFILPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "EMITENTEAF"  */
    if exists (select 1
               from   EMITENTEAF t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMITENTEAF". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRODNCMP"  */
    if exists (select 1
               from   PRODNCMP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODNCMP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RELFILTRAS"  */
    if exists (select 1
               from   RELFILTRAS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELFILTRAS". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SETOTIPOPRAT"  */
    if exists (select 1
               from   SETOTIPOPRAT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SETOTIPOPRAT". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MEDSOLI"  */
    if exists (select 1
               from   MEDSOLI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MEDSOLI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVOPSOL"  */
    if exists (select 1
               from   MOVOPSOL t2, deleted t1
               where  t2.CDFILSOLMOV = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOPSOL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOGNECECOMP"  */
    if exists (select 1
               from   LOGNECECOMP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGNECECOMP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CTRLAPURFAT"  */
    if exists (select 1
               from   CTRLAPURFAT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLAPURFAT". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SOLICITA"  */
    if exists (select 1
               from   SOLICITA t2, deleted t1
               where  t2.CDFILSOLI = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICITA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SOLICITA"  */
    if exists (select 1
               from   SOLICITA t2, deleted t1
               where  t2.CDFILLANCA = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICITA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SOLICITA"  */
    if exists (select 1
               from   SOLICITA t2, deleted t1
               where  t2.CDFILCENT = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICITA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREVLISTA"  */
    if exists (select 1
               from   PREVLISTA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVLISTA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CANDIDATO"  */
    if exists (select 1
               from   CANDIDATO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CANDIDATO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FUNCIONARIO"  */
    if exists (select 1
               from   FUNCIONARIO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FUNCIONARIO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ESCALAPADRAO"  */
    if exists (select 1
               from   ESCALAPADRAO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESCALAPADRAO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SIND_FILIAL"  */
    if exists (select 1
               from   SIND_FILIAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SIND_FILIAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PAR_FILIAL_RH"  */
    if exists (select 1
               from   PAR_FILIAL_RH t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PAR_FILIAL_RH". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVI_PESSOAL"  */
    if exists (select 1
               from   MOVI_PESSOAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVI_PESSOAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CUSTOREAL"  */
    if exists (select 1
               from   CUSTOREAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOREAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CUSTOPREVISTO"  */
    if exists (select 1
               from   CUSTOPREVISTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOPREVISTO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TALENTO"  */
    if exists (select 1
               from   TALENTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TALENTO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FERIBANC"  */
    if exists (select 1
               from   FERIBANC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FERIBANC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PEDIDO"  */
    if exists (select 1
               from   PEDIDO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DESTSOL"  */
    if exists (select 1
               from   DESTSOL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESTSOL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "META_FILIAL"  */
    if exists (select 1
               from   META_FILIAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "META_FILIAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SERVREAL"  */
    if exists (select 1
               from   SERVREAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVREAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RETIRADA_CONS"  */
    if exists (select 1
               from   RETIRADA_CONS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RETIRADA_CONS". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "HORBASFI"  */
    if exists (select 1
               from   HORBASFI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HORBASFI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RATEIONF"  */
    if exists (select 1
               from   RATEIONF t2, deleted t1
               where  t2.CDFILICCUST = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATEIONF". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "INVFISCFIL"  */
    if exists (select 1
               from   INVFISCFIL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVFISCFIL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CAIXA"  */
    if exists (select 1
               from   CAIXA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CAIXA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CONTATO"  */
    if exists (select 1
               from   CONTATO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTATO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "BEM"  */
    if exists (select 1
               from   BEM t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BEM". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "REPLICDOC"  */
    if exists (select 1
               from   REPLICDOC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REPLICDOC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ITREPLICDOC"  */
    if exists (select 1
               from   ITREPLICDOC t2, deleted t1
               where  t2.CDFILIDESTRD = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITREPLICDOC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ITREPLICDOC"  */
    if exists (select 1
               from   ITREPLICDOC t2, deleted t1
               where  t2.CDFILIORIGRD = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITREPLICDOC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRECOUTI"  */
    if exists (select 1
               from   PRECOUTI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRECOUTI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CATRSERV"  */
    if exists (select 1
               from   CATRSERV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CATRSERV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "COMENSAL"  */
    if exists (select 1
               from   COMENSAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMENSAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DUPLICATA"  */
    if exists (select 1
               from   DUPLICATA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DUPLICATA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRODUTOAP"  */
    if exists (select 1
               from   PRODUTOAP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTOAP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SERVCONTRAT"  */
    if exists (select 1
               from   SERVCONTRAT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCONTRAT". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PEDCOLETA"  */
    if exists (select 1
               from   PEDCOLETA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDCOLETA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ROMANEIO"  */
    if exists (select 1
               from   ROMANEIO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROMANEIO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ROMANEIO"  */
    if exists (select 1
               from   ROMANEIO t2, deleted t1
               where  t2.CDFILIRECERO = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROMANEIO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RATTPAJR"  */
    if exists (select 1
               from   RATTPAJR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATTPAJR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RATTPAJP"  */
    if exists (select 1
               from   RATTPAJP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATTPAJP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RATTPCTR"  */
    if exists (select 1
               from   RATTPCTR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATTPCTR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RATTPCTP"  */
    if exists (select 1
               from   RATTPCTP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATTPCTP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RATCTRECEBER"  */
    if exists (select 1
               from   RATCTRECEBER t2, deleted t1
               where  t2.CDFIRATCTREC = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATCTRECEBER". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RATCTPAGAR"  */
    if exists (select 1
               from   RATCTPAGAR t2, deleted t1
               where  t2.CDFIRATCTPAG = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATCTPAGAR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ITEMREFE"  */
    if exists (select 1
               from   ITEMREFE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMREFE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "GERANFROMA"  */
    if exists (select 1
               from   GERANFROMA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERANFROMA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "GERANFROMA"  */
    if exists (select 1
               from   GERANFROMA t2, deleted t1
               where  t2.CDFILIRECEGE = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERANFROMA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "GERANFROMA"  */
    if exists (select 1
               from   GERANFROMA t2, deleted t1
               where  t2.CDFILIEMITGE = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERANFROMA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRODPREFMED"  */
    if exists (select 1
               from   PRODPREFMED t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODPREFMED". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ORDPRODUCAO"  */
    if exists (select 1
               from   ORDPRODUCAO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORDPRODUCAO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RETIRADAESTO"  */
    if exists (select 1
               from   RETIRADAESTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RETIRADAESTO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMFILIAL"  */
    if exists (select 1
               from   PARAMFILIAL t2, deleted t1
               where  t2.CDFCUSNFROMA = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMFILIAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RETIRADAPCP"  */
    if exists (select 1
               from   RETIRADAPCP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RETIRADAPCP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "REQPROD"  */
    if exists (select 1
               from   REQPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CTRLAPURFAT"  */
    if exists (select 1
               from   CTRLAPURFAT t2, deleted t1
               where  t2.CDFILIAPUR = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLAPURFAT". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOGPRODUTOAP"  */
    if exists (select 1
               from   LOGPRODUTOAP t2, deleted t1
               where  t2.CDFILIAL1 = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGPRODUTOAP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVFUNCRH"  */
    if exists (select 1
               from   MOVFUNCRH t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVFUNCRH". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMINTEGRA"  */
    if exists (select 1
               from   PARAMINTEGRA t2, deleted t1
               where  t2.CDFILIALPDV = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMINTEGRA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PROCESSAPROD"  */
    if exists (select 1
               from   PROCESSAPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROCESSAPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ACAO"  */
    if exists (select 1
               from   ACAO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACAO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DIAINVEFILI"  */
    if exists (select 1
               from   DIAINVEFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIAINVEFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVOCORR"  */
    if exists (select 1
               from   MOVOCORR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOCORR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CLIEFILISAP"  */
    if exists (select 1
               from   CLIEFILISAP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIEFILISAP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PEDIDO"  */
    if exists (select 1
               from   PEDIDO t2, deleted t1
               where  t2.CDFILIALPED = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "VISTEQFORN"  */
    if exists (select 1
               from   VISTEQFORN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VISTEQFORN". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOGAPURQFO"  */
    if exists (select 1
               from   LOGAPURQFO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGAPURQFO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "AVISQUALFOR"  */
    if exists (select 1
               from   AVISQUALFOR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AVISQUALFOR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "AVISO"  */
    if exists (select 1
               from   AVISO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AVISO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ALMOXARIFADO"  */
    if exists (select 1
               from   ALMOXARIFADO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ALMOXARIFADO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "APRHRTRAB"  */
    if exists (select 1
               from   APRHRTRAB t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APRHRTRAB". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LANCDESPFATU"  */
    if exists (select 1
               from   LANCDESPFATU t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCDESPFATU". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "NUMERORNF"  */
    if exists (select 1
               from   NUMERORNF t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NUMERORNF". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CIAFILI"  */
    if exists (select 1
               from   CIAFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CIAFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "OBSDELIVTMP"  */
    if exists (select 1
               from   OBSDELIVTMP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSDELIVTMP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREVPAXTEMP"  */
    if exists (select 1
               from   PREVPAXTEMP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVPAXTEMP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RELPLANOVOO"  */
    if exists (select 1
               from   RELPLANOVOO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELPLANOVOO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RELPREVPAX"  */
    if exists (select 1
               from   RELPREVPAX t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELPREVPAX". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RELAT_VOOPER"  */
    if exists (select 1
               from   RELAT_VOOPER t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELAT_VOOPER". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOGPREVPAX2"  */
    if exists (select 1
               from   LOGPREVPAX2 t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGPREVPAX2". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOGPREVPAX"  */
    if exists (select 1
               from   LOGPREVPAX t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGPREVPAX". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOGSERVICO"  */
    if exists (select 1
               from   LOGSERVICO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGSERVICO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOGVOO"  */
    if exists (select 1
               from   LOGVOO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGVOO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DELIVERY"  */
    if exists (select 1
               from   DELIVERY t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DELIVERY". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAVEND"  */
    if exists (select 1
               from   PARAVEND t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAVEND". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RECEITA2"  */
    if exists (select 1
               from   RECEITA2 t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEITA2". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMCOMIS"  */
    if exists (select 1
               from   PARAMCOMIS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMCOMIS". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CUSTOPRODUTO"  */
    if exists (select 1
               from   CUSTOPRODUTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOPRODUTO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRECOMATDIDA"  */
    if exists (select 1
               from   PRECOMATDIDA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRECOMATDIDA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SALA"  */
    if exists (select 1
               from   SALA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SALA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MATRICULA"  */
    if exists (select 1
               from   MATRICULA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MATRICULA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RESERVAPROD"  */
    if exists (select 1
               from   RESERVAPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESERVAPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "EMPRESTPROD"  */
    if exists (select 1
               from   EMPRESTPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMPRESTPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TURMAPERIODO"  */
    if exists (select 1
               from   TURMAPERIODO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TURMAPERIODO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TIPOCTRECFIL"  */
    if exists (select 1
               from   TIPOCTRECFIL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOCTRECFIL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PROSPFILIAL"  */
    if exists (select 1
               from   PROSPFILIAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROSPFILIAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RATTPDEBI"  */
    if exists (select 1
               from   RATTPDEBI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATTPDEBI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RATTPCRED"  */
    if exists (select 1
               from   RATTPCRED t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATTPCRED". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVDEBBANC"  */
    if exists (select 1
               from   MOVDEBBANC t2, deleted t1
               where  t2.CDFILILANDEB = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVDEBBANC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVCREDBANC"  */
    if exists (select 1
               from   MOVCREDBANC t2, deleted t1
               where  t2.CDFILILANCRE = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVCREDBANC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MATRICULA"  */
    if exists (select 1
               from   MATRICULA t2, deleted t1
               where  t2.CDFILORIGTRF = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MATRICULA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MATRICULA"  */
    if exists (select 1
               from   MATRICULA t2, deleted t1
               where  t2.CDFILDESTTRF = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MATRICULA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ATIVIDADE"  */
    if exists (select 1
               from   ATIVIDADE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ATIVIDADE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DISPONIBFUNC"  */
    if exists (select 1
               from   DISPONIBFUNC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DISPONIBFUNC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOTE"  */
    if exists (select 1
               from   LOTE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOTE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CNABPAG"  */
    if exists (select 1
               from   CNABPAG t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CNABPAG". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CNABREC"  */
    if exists (select 1
               from   CNABREC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CNABREC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LANCTOESTOQ"  */
    if exists (select 1
               from   LANCTOESTOQ t2, deleted t1
               where  t2.CDFILIMOVI = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCTOESTOQ". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ROMANEIO"  */
    if exists (select 1
               from   ROMANEIO t2, deleted t1
               where  t2.CDFILIEMITRO = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROMANEIO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CLIENTE"  */
    if exists (select 1
               from   CLIENTE t2, deleted t1
               where  t2.CDFILORIGCLI = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENTE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMETROQPR"  */
    if exists (select 1
               from   PARAMETROQPR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMETROQPR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOGMATRICULA"  */
    if exists (select 1
               from   LOGMATRICULA t2, deleted t1
               where  t2.CDFILORIGLTR = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGMATRICULA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDFILIEMIT = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDFILIRECE = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVESTMATDID"  */
    if exists (select 1
               from   MOVESTMATDID t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVESTMATDID". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ALIQIMPFIS"  */
    if exists (select 1
               from   ALIQIMPFIS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ALIQIMPFIS". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ROTFABLOCAL"  */
    if exists (select 1
               from   ROTFABLOCAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROTFABLOCAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SUBGRPRODFIL"  */
    if exists (select 1
               from   SUBGRPRODFIL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SUBGRPRODFIL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ATIVAPRHR"  */
    if exists (select 1
               from   ATIVAPRHR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ATIVAPRHR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SETORFIL"  */
    if exists (select 1
               from   SETORFIL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SETORFIL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "HORAALTA"  */
    if exists (select 1
               from   HORAALTA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HORAALTA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "EXEMPLAR"  */
    if exists (select 1
               from   EXEMPLAR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXEMPLAR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PCHAVEPROD"  */
    if exists (select 1
               from   PCHAVEPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PCHAVEPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOGEQUIPPRO"  */
    if exists (select 1
               from   LOGEQUIPPRO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGEQUIPPRO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MAPABASEV"  */
    if exists (select 1
               from   MAPABASEV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPABASEV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MAPABASEV"  */
    if exists (select 1
               from   MAPABASEV t2, deleted t1
               where  t2.CDFILIALCLIE = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPABASEV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ATENHOSP"  */
    if exists (select 1
               from   ATENHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ATENHOSP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ANDAR"  */
    if exists (select 1
               from   ANDAR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ANDAR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ALA"  */
    if exists (select 1
               from   ALA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ALA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "POSTO"  */
    if exists (select 1
               from   POSTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "POSTO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DESTFILI"  */
    if exists (select 1
               from   DESTFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESTFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DISPONIBEQUI"  */
    if exists (select 1
               from   DISPONIBEQUI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DISPONIBEQUI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "AGENEQUI"  */
    if exists (select 1
               from   AGENEQUI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENEQUI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREVABASEV"  */
    if exists (select 1
               from   PREVABASEV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVABASEV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "AGENFUNC"  */
    if exists (select 1
               from   AGENFUNC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENFUNC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TABEPREC"  */
    if exists (select 1
               from   TABEPREC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TABEPREC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SEQNUMAVIENT"  */
    if exists (select 1
               from   SEQNUMAVIENT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SEQNUMAVIENT". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DESCROTLOCAL"  */
    if exists (select 1
               from   DESCROTLOCAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESCROTLOCAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "EQUIPAPROD"  */
    if exists (select 1
               from   EQUIPAPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQUIPAPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "HORVISITADEP"  */
    if exists (select 1
               from   HORVISITADEP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HORVISITADEP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TABEPROCFILI"  */
    if exists (select 1
               from   TABEPROCFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TABEPROCFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "AUDITORIA"  */
    if exists (select 1
               from   AUDITORIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUDITORIA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SIMULACAO"  */
    if exists (select 1
               from   SIMULACAO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SIMULACAO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "INFOBRCONV"  */
    if exists (select 1
               from   INFOBRCONV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INFOBRCONV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FTFUNCMEDI"  */
    if exists (select 1
               from   FTFUNCMEDI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FTFUNCMEDI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREAUTPROD"  */
    if exists (select 1
               from   PREAUTPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREAUTPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DESCPROGPR"  */
    if exists (select 1
               from   DESCPROGPR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESCPROGPR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PROAGRCONV"  */
    if exists (select 1
               from   PROAGRCONV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROAGRCONV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRNAUTCONV"  */
    if exists (select 1
               from   PRNAUTCONV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRNAUTCONV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TOLTMPCONV"  */
    if exists (select 1
               from   TOLTMPCONV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TOLTMPCONV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FTAUTOCONV"  */
    if exists (select 1
               from   FTAUTOCONV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FTAUTOCONV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOEDUTCONV"  */
    if exists (select 1
               from   MOEDUTCONV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOEDUTCONV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOJA"  */
    if exists (select 1
               from   LOJA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOJA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "EQUIPAPROD"  */
    if exists (select 1
               from   EQUIPAPROD t2, deleted t1
               where  t2.CDFILIPROP = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQUIPAPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARFILIALEMP"  */
    if exists (select 1
               from   PARFILIALEMP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALEMP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PROCEXAMDOAC"  */
    if exists (select 1
               from   PROCEXAMDOAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROCEXAMDOAC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "VISLIBPROD"  */
    if exists (select 1
               from   VISLIBPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VISLIBPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "HEMOPRDOAC"  */
    if exists (select 1
               from   HEMOPRDOAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HEMOPRDOAC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMAN"  */
    if exists (select 1
               from   PARAMAN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMAN". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "POOLHEMOPR"  */
    if exists (select 1
               from   POOLHEMOPR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "POOLHEMOPR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOGEQUIPPRO"  */
    if exists (select 1
               from   LOGEQUIPPRO t2, deleted t1
               where  t2.CDFILIPROP = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGEQUIPPRO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PROCPREPDOAC"  */
    if exists (select 1
               from   PROCPREPDOAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROCPREPDOAC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PROTESE"  */
    if exists (select 1
               from   PROTESE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROTESE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FORMPRPROT"  */
    if exists (select 1
               from   FORMPRPROT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORMPRPROT". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARFILIHOSP"  */
    if exists (select 1
               from   PARFILIHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIHOSP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVPROTESE"  */
    if exists (select 1
               from   MOVPROTESE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVPROTESE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "REQUHOSP"  */
    if exists (select 1
               from   REQUHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUHOSP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RETIHOSP"  */
    if exists (select 1
               from   RETIHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RETIHOSP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DESCVALRPR"  */
    if exists (select 1
               from   DESCVALRPR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESCVALRPR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREVENDA"  */
    if exists (select 1
               from   PREVENDA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVENDA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARFILIALVMN"  */
    if exists (select 1
               from   PARFILIALVMN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALVMN". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ESPEPROD"  */
    if exists (select 1
               from   ESPEPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESPEPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVIVALOR"  */
    if exists (select 1
               from   MOVIVALOR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVIVALOR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PROCFABRICA"  */
    if exists (select 1
               from   PROCFABRICA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROCFABRICA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "AGENTARE"  */
    if exists (select 1
               from   AGENTARE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENTARE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOGUTEQUIP"  */
    if exists (select 1
               from   LOGUTEQUIP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGUTEQUIP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREVCOMP"  */
    if exists (select 1
               from   PREVCOMP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCOMP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TURNOFILI"  */
    if exists (select 1
               from   TURNOFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TURNOFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "POSESTCONS"  */
    if exists (select 1
               from   POSESTCONS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "POSESTCONS". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CUSTPROD"  */
    if exists (select 1
               from   CUSTPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOGCATRACA"  */
    if exists (select 1
               from   LOGCATRACA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGCATRACA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ITCOMISSAO"  */
    if exists (select 1
               from   ITCOMISSAO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITCOMISSAO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDFILIPADRAO = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PROPOSTA"  */
    if exists (select 1
               from   PROPOSTA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPOSTA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRECCONCORR"  */
    if exists (select 1
               from   PRECCONCORR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRECCONCORR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DIAUTILFIL"  */
    if exists (select 1
               from   DIAUTILFIL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIAUTILFIL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARFILIALLOJ"  */
    if exists (select 1
               from   PARFILIALLOJ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALLOJ". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FORMPRECPROD"  */
    if exists (select 1
               from   FORMPRECPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORMPRECPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVOCORRPROD"  */
    if exists (select 1
               from   MOVOCORRPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOCORRPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CURVAABC"  */
    if exists (select 1
               from   CURVAABC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CURVAABC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "VISITANTE"  */
    if exists (select 1
               from   VISITANTE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VISITANTE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ORCAFINA"  */
    if exists (select 1
               from   ORCAFINA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAFINA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CALCCUST"  */
    if exists (select 1
               from   CALCCUST t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CALCCUST". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMPRODSUP"  */
    if exists (select 1
               from   PARAMPRODSUP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPRODSUP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMPRODSUP"  */
    if exists (select 1
               from   PARAMPRODSUP t2, deleted t1
               where  t2.CDFILICTRL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPRODSUP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FORMPRECOCIA"  */
    if exists (select 1
               from   FORMPRECOCIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORMPRECOCIA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PERCPRODFATU"  */
    if exists (select 1
               from   PERCPRODFATU t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERCPRODFATU". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CTRLMOVIESTQ"  */
    if exists (select 1
               from   CTRLMOVIESTQ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLMOVIESTQ". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TRABNUTRIREC"  */
    if exists (select 1
               from   TRABNUTRIREC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TRABNUTRIREC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREVIVENDA"  */
    if exists (select 1
               from   PREVIVENDA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVIVENDA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMPRODSUP"  */
    if exists (select 1
               from   PARAMPRODSUP t2, deleted t1
               where  t2.CDFILIOPELOG = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPRODSUP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PROJETO"  */
    if exists (select 1
               from   PROJETO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROJETO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TAREFA"  */
    if exists (select 1
               from   TAREFA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAREFA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "REQUESTO"  */
    if exists (select 1
               from   REQUESTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUESTO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CODIGOBARRA"  */
    if exists (select 1
               from   CODIGOBARRA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODIGOBARRA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SOLICTROCO"  */
    if exists (select 1
               from   SOLICTROCO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICTROCO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TRAJETO"  */
    if exists (select 1
               from   TRAJETO t2, deleted t1
               where  t2.CDFILIALORI = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TRAJETO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TRAJETO"  */
    if exists (select 1
               from   TRAJETO t2, deleted t1
               where  t2.CDFILIALDES = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TRAJETO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "VENDAVMN"  */
    if exists (select 1
               from   VENDAVMN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDAVMN". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "VENDAVMN"  */
    if exists (select 1
               from   VENDAVMN t2, deleted t1
               where  t2.CDFILIALCLIE = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDAVMN". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARFILIALGEP"  */
    if exists (select 1
               from   PARFILIALGEP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALGEP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREVENDAEQU"  */
    if exists (select 1
               from   PREVENDAEQU t2, deleted t1
               where  t2.CDFILIALCLIE = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVENDAEQU". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREVENDAEQU"  */
    if exists (select 1
               from   PREVENDAEQU t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVENDAEQU". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREVENDAVMN"  */
    if exists (select 1
               from   PREVENDAVMN t2, deleted t1
               where  t2.CDFILIALCLIE = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVENDAVMN". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREVENDAVMN"  */
    if exists (select 1
               from   PREVENDAVMN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVENDAVMN". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "EXPEDICAO"  */
    if exists (select 1
               from   EXPEDICAO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXPEDICAO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREVABASEV"  */
    if exists (select 1
               from   PREVABASEV t2, deleted t1
               where  t2.CDFILIALCLIE = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVABASEV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "EXPEDICAO"  */
    if exists (select 1
               from   EXPEDICAO t2, deleted t1
               where  t2.CDFILIALPED = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXPEDICAO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "IMPITVENDAVM"  */
    if exists (select 1
               from   IMPITVENDAVM t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPITVENDAVM". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARFILIALSAC"  */
    if exists (select 1
               from   PARFILIALSAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALSAC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ABASTEROTA"  */
    if exists (select 1
               from   ABASTEROTA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ABASTEROTA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "JUSTIFICAVMN"  */
    if exists (select 1
               from   JUSTIFICAVMN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "JUSTIFICAVMN". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVPESQUISA"  */
    if exists (select 1
               from   MOVPESQUISA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVPESQUISA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PROGPESQUISA"  */
    if exists (select 1
               from   PROGPESQUISA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROGPESQUISA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TARIFADOR"  */
    if exists (select 1
               from   TARIFADOR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TARIFADOR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "INVEROTALOTE"  */
    if exists (select 1
               from   INVEROTALOTE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVEROTALOTE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TAXAADMFATUR"  */
    if exists (select 1
               from   TAXAADMFATUR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAXAADMFATUR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARFILIALFAT"  */
    if exists (select 1
               from   PARFILIALFAT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALFAT". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DESPPREVFILI"  */
    if exists (select 1
               from   DESPPREVFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESPPREVFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRODFILIFATU"  */
    if exists (select 1
               from   PRODFILIFATU t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFILIFATU". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CTRLTPRECEB"  */
    if exists (select 1
               from   CTRLTPRECEB t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLTPRECEB". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "EMITAFFORN"  */
    if exists (select 1
               from   EMITAFFORN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMITAFFORN". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RESERPRODPED"  */
    if exists (select 1
               from   RESERPRODPED t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESERPRODPED". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MAPARESUMECF"  */
    if exists (select 1
               from   MAPARESUMECF t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPARESUMECF". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREVISAOPDV"  */
    if exists (select 1
               from   PREVISAOPDV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVISAOPDV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RELAT_MANDATO"  */
    if exists (select 1
               from   RELAT_MANDATO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELAT_MANDATO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMCMPPROD"  */
    if exists (select 1
               from   PARAMCMPPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMCMPPROD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CUSTOSUP"  */
    if exists (select 1
               from   CUSTOSUP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOSUP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FERIFUNC"  */
    if exists (select 1
               from   FERIFUNC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FERIFUNC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMPEDIDO"  */
    if exists (select 1
               from   PARAMPEDIDO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPEDIDO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PAREXPEDICAO"  */
    if exists (select 1
               from   PAREXPEDICAO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PAREXPEDICAO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TROCA"  */
    if exists (select 1
               from   TROCA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TROCA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "VENDAREST"  */
    if exists (select 1
               from   VENDAREST t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDAREST". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LIVROPRECO"  */
    if exists (select 1
               from   LIVROPRECO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LIVROPRECO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CONFTELA"  */
    if exists (select 1
               from   CONFTELA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONFTELA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "AGRUPAF"  */
    if exists (select 1
               from   AGRUPAF t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGRUPAF". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ARQUIVIMPPOO"  */
    if exists (select 1
               from   ARQUIVIMPPOO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ARQUIVIMPPOO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "REAJSERVFAT"  */
    if exists (select 1
               from   REAJSERVFAT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REAJSERVFAT". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "BREAKDOWN"  */
    if exists (select 1
               from   BREAKDOWN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BREAKDOWN". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TEMPOPREPVOO"  */
    if exists (select 1
               from   TEMPOPREPVOO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TEMPOPREPVOO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TEMPOPREPSER"  */
    if exists (select 1
               from   TEMPOPREPSER t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TEMPOPREPSER". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TEMPOPREPSUB"  */
    if exists (select 1
               from   TEMPOPREPSUB t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TEMPOPREPSUB". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TEMPOPREPREC"  */
    if exists (select 1
               from   TEMPOPREPREC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TEMPOPREPREC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "APURPRECO"  */
    if exists (select 1
               from   APURPRECO t2, deleted t1
               where  t2.CDFILCENTCMP = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURPRECO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ESTOSEGU"  */
    if exists (select 1
               from   ESTOSEGU t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESTOSEGU". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "APURPRECO"  */
    if exists (select 1
               from   APURPRECO t2, deleted t1
               where  t2.CDFILLANC = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURPRECO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CALEPRODUCAO"  */
    if exists (select 1
               from   CALEPRODUCAO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CALEPRODUCAO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMPRODMRP"  */
    if exists (select 1
               from   PARAMPRODMRP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPRODMRP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PROGPRODU"  */
    if exists (select 1
               from   PROGPRODU t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROGPRODU". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PREMATPPROGP"  */
    if exists (select 1
               from   PREMATPPROGP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREMATPPROGP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CALEPRODMRP"  */
    if exists (select 1
               from   CALEPRODMRP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CALEPRODMRP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CTRLACESPER"  */
    if exists (select 1
               from   CTRLACESPER t2, deleted t1
               where  t2.CDFILIPER = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLACESPER". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ENTREGAMERC"  */
    if exists (select 1
               from   ENTREGAMERC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ENTREGAMERC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FILIAL"  */
    if exists (select 1
               from   FILIAL t2, deleted t1
               where  t2.CDFILCENTPME = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DEMAEXTRAMRP"  */
    if exists (select 1
               from   DEMAEXTRAMRP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DEMAEXTRAMRP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "IMPOCONTRETI"  */
    if exists (select 1
               from   IMPOCONTRETI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOCONTRETI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LANCFATUDIVE"  */
    if exists (select 1
               from   LANCFATUDIVE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCFATUDIVE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "INCONSMRP"  */
    if exists (select 1
               from   INCONSMRP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INCONSMRP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LISTAINFLAC"  */
    if exists (select 1
               from   LISTAINFLAC t2, deleted t1
               where  t2.CDFILCENTCMP = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LISTAINFLAC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "GSCRESULTADO"  */
    if exists (select 1
               from   GSCRESULTADO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GSCRESULTADO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ARVTIPOCARD"  */
    if exists (select 1
               from   ARVTIPOCARD t2, deleted t1
               where  t2.CDFILCENTPME = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ARVTIPOCARD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CARDAPIOPME"  */
    if exists (select 1
               from   CARDAPIOPME t2, deleted t1
               where  t2.CDFILCENTPME = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDAPIOPME". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CARDIAPME"  */
    if exists (select 1
               from   CARDIAPME t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDIAPME". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "GATILHO"  */
    if exists (select 1
               from   GATILHO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GATILHO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DIAFECMES"  */
    if exists (select 1
               from   DIAFECMES t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIAFECMES". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARCTEMISBOL"  */
    if exists (select 1
               from   PARCTEMISBOL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARCTEMISBOL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CTRLEMISBOL"  */
    if exists (select 1
               from   CTRLEMISBOL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLEMISBOL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TIPOCARD"  */
    if exists (select 1
               from   TIPOCARD t2, deleted t1
               where  t2.CDFILCENTPME = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOCARD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FILITIPOCARD"  */
    if exists (select 1
               from   FILITIPOCARD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILITIPOCARD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CMPCENTENDIR"  */
    if exists (select 1
               from   CMPCENTENDIR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
