       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CMPCENTENDIR". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ITEMVENDMERE"  */
    if exists (select 1
               from   ITEMVENDMERE t2, deleted t1
               where  t2.CDFILCENTPME = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMVENDMERE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRODNECECOMP"  */
    if exists (select 1
               from   PRODNECECOMP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODNECECOMP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PAR_FILIAL_GEP"  */
    if exists (select 1
               from   PAR_FILIAL_GEP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PAR_FILIAL_GEP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "INVOICE"  */
    if exists (select 1
               from   INVOICE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVOICE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FAMSALDOFILI"  */
    if exists (select 1
               from   FAMSALDOFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FAMSALDOFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PROPOSTAFAS"  */
    if exists (select 1
               from   PROPOSTAFAS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPOSTAFAS". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "APURFATUFAS"  */
    if exists (select 1
               from   APURFATUFAS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURFATUFAS". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CONSSERVPME"  */
    if exists (select 1
               from   CONSSERVPME t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSSERVPME". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DIASEMCONSPME"  */
    if exists (select 1
               from   DIASEMCONSPME t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIASEMCONSPME". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PEDIDOEXOL"  */
    if exists (select 1
               from   PEDIDOEXOL t2, deleted t1
               where  t2.CDFILLANC = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDOEXOL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PEDIDOEXOL"  */
    if exists (select 1
               from   PEDIDOEXOL t2, deleted t1
               where  t2.CDFILDEST = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDOEXOL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ORCAEVENTO"  */
    if exists (select 1
               from   ORCAEVENTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAEVENTO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "RALIMENT"  */
    if exists (select 1
               from   RALIMENT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RALIMENT". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "UNIDMEDIFILI"  */
    if exists (select 1
               from   UNIDMEDIFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "UNIDMEDIFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "FATORESUFAT"  */
    if exists (select 1
               from   FATORESUFAT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FATORESUFAT". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CTRLNUMEDELI"  */
    if exists (select 1
               from   CTRLNUMEDELI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLNUMEDELI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CUSTOLOGIST"  */
    if exists (select 1
               from   CUSTOLOGIST t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOLOGIST". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DEPARTAMENTO"  */
    if exists (select 1
               from   DEPARTAMENTO t2, deleted t1
               where  t2.CDFILIPEDORC = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DEPARTAMENTO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ORCAEVENTO"  */
    if exists (select 1
               from   ORCAEVENTO t2, deleted t1
               where  t2.CDFILDESTENC = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAEVENTO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVOPAF"  */
    if exists (select 1
               from   MOVOPAF t2, deleted t1
               where  t2.CDFILDESMOV = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOPAF". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MOVOPAF"  */
    if exists (select 1
               from   MOVOPAF t2, deleted t1
               where  t2.CDFILLANMOV = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOPAF". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ITEMSOLI"  */
    if exists (select 1
               from   ITEMSOLI t2, deleted t1
               where  t2.CDFILIOPELOG = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMSOLI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CALENCARD"  */
    if exists (select 1
               from   CALENCARD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CALENCARD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PEDIDOEXOL"  */
    if exists (select 1
               from   PEDIDOEXOL t2, deleted t1
               where  t2.CDFILIOPELOG = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDOEXOL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CONSSOLOL"  */
    if exists (select 1
               from   CONSSOLOL t2, deleted t1
               where  t2.CDFILIOPELOG = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSSOLOL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRODCONSSOLOL"  */
    if exists (select 1
               from   PRODCONSSOLOL t2, deleted t1
               where  t2.CDFILLAN = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODCONSSOLOL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRODCONSSOLOL"  */
    if exists (select 1
               from   PRODCONSSOLOL t2, deleted t1
               where  t2.CDFILSOLI = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODCONSSOLOL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "OBSAPROVCARDFILI"  */
    if exists (select 1
               from   OBSAPROVCARDFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSAPROVCARDFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DATAEMBARQUEOL"  */
    if exists (select 1
               from   DATAEMBARQUEOL t2, deleted t1
               where  t2.CDFILIOPELOG = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DATAEMBARQUEOL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "DATAEMBARQUEOL"  */
    if exists (select 1
               from   DATAEMBARQUEOL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DATAEMBARQUEOL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARFILIALROP"  */
    if exists (select 1
               from   PARFILIALROP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALROP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "IMPOSTOROP"  */
    if exists (select 1
               from   IMPOSTOROP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSTOROP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TIPOOPERACROP"  */
    if exists (select 1
               from   TIPOOPERACROP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOOPERACROP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PRODUTOROP"  */
    if exists (select 1
               from   PRODUTOROP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTOROP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ROPFILIAL"  */
    if exists (select 1
               from   ROPFILIAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROPFILIAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LEITO"  */
    if exists (select 1
               from   LEITO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LEITO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ETL_RESUGER"  */
    if exists (select 1
               from   ETL_RESUGER t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUGER". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ETL_FORSALE"  */
    if exists (select 1
               from   ETL_FORSALE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_FORSALE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "CAIXAPERS"  */
    if exists (select 1
               from   CAIXAPERS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CAIXAPERS". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "IMPFISPERS"  */
    if exists (select 1
               from   IMPFISPERS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPFISPERS". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "TIPOCONSFILI"  */
    if exists (select 1
               from   TIPOCONSFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOCONSFILI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARPRODOPCONS"  */
    if exists (select 1
               from   PARPRODOPCONS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARPRODOPCONS". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ETL_RESUFOOD"  */
    if exists (select 1
               from   ETL_RESUFOOD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUFOOD". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ETL_RESUESTO"  */
    if exists (select 1
               from   ETL_RESUESTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUESTO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOGESTATDIET"  */
    if exists (select 1
               from   LOGESTATDIET t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGESTATDIET". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ETL_RESUSERV"  */
    if exists (select 1
               from   ETL_RESUSERV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUSERV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "APURACAOROP"  */
    if exists (select 1
               from   APURACAOROP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURACAOROP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PERMUTAORE"  */
    if exists (select 1
               from   PERMUTAORE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERMUTAORE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LANCROPFILIAL"  */
    if exists (select 1
               from   LANCROPFILIAL t2, deleted t1
               where  t2.CDFILCENTROP = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCROPFILIAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ETLFATUINVOICE"  */
    if exists (select 1
               from   ETLFATUINVOICE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLFATUINVOICE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LANCATESTADO"  */
    if exists (select 1
               from   LANCATESTADO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCATESTADO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARAMSERIE"  */
    if exists (select 1
               from   PARAMSERIE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMSERIE". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "OBSERVREDZ"  */
    if exists (select 1
               from   OBSERVREDZ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSERVREDZ". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ETLDRO"  */
    if exists (select 1
               from   ETLDRO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLDRO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ITCOMMARKUP"  */
    if exists (select 1
               from   ITCOMMARKUP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITCOMMARKUP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PESSOA"  */
    if exists (select 1
               from   PESSOA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PESSOA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "GRTIPOPRRISL"  */
    if exists (select 1
               from   GRTIPOPRRISL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GRTIPOPRRISL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "HISTFILIAL"  */
    if exists (select 1
               from   HISTFILIAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HISTFILIAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PEDCOLGERAL"  */
    if exists (select 1
               from   PEDCOLGERAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDCOLGERAL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ETLCOMPRASGER"  */
    if exists (select 1
               from   ETLCOMPRASGER t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCOMPRASGER". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ETLCONTAPAG"  */
    if exists (select 1
               from   ETLCONTAPAG t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCONTAPAG". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ETLCONTAREC"  */
    if exists (select 1
               from   ETLCONTAREC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCONTAREC". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "MAPDISTALMOX"  */
    if exists (select 1
               from   MAPDISTALMOX t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPDISTALMOX". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "BATIDAPONTO"  */
    if exists (select 1
               from   BATIDAPONTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BATIDAPONTO". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "GRUPOFISCIMPFIL"  */
    if exists (select 1
               from   GRUPOFISCIMPFIL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GRUPOFISCIMPFIL". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LAYOUTIMP"  */
    if exists (select 1
               from   LAYOUTIMP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LAYOUTIMP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "NFRELACCDRS"  */
    if exists (select 1
               from   NFRELACCDRS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFRELACCDRS". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "PARGERALSUV"  */
    if exists (select 1
               from   PARGERALSUV t2, deleted t1
               where  t2.CDFILSOLISUP = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARGERALSUV". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SOLICITASUPRI"  */
    if exists (select 1
               from   SOLICITASUPRI t2, deleted t1
               where  t2.CDFILISOLI = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICITASUPRI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "SOLICITASUPRI"  */
    if exists (select 1
               from   SOLICITASUPRI t2, deleted t1
               where  t2.CDFILAPROVSOLI = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICITASUPRI". 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.CDFILIALOCFUNC = 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 "FILIALROTA"  */
    if exists (select 1
               from   FILIALROTA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIALROTA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "LOCK_LISTA"  */
    if exists (select 1
               from   LOCK_LISTA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOCK_LISTA". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "ITCOMANDAPEND"  */
    if exists (select 1
               from   ITCOMANDAPEND t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITCOMANDAPEND". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "INVENTARIORETI"  */
    if exists (select 1
               from   INVENTARIORETI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVENTARIORETI". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Cannot delete parent "FILIAL" if children still exist in "AUTGRUPOP"  */
    if exists (select 1
               from   AUTGRUPOP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUTGRUPOP". Cannot delete parent "FILIAL".'
          goto error
       end
    
    /*  Delete all children in "ENDEFILI"  */
    delete ENDEFILI
    from   ENDEFILI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "PARCMPSUP"  */
    delete PARCMPSUP
    from   PARCMPSUP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "PARASUPRI"  */
    delete PARASUPRI
    from   PARASUPRI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "FILINECE"  */
    delete FILINECE
    from   FILINECE t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "FILINECE"  */
    delete FILINECE
    from   FILINECE t2, deleted t1
    where  t2.CDFILINECCMP = t1.CDFILIAL
    
    /*  Delete all children in "COLETAPR"  */
    delete COLETAPR
    from   COLETAPR t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "PARTFILI"  */
    delete PARTFILI
    from   PARTFILI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "LIMISEGPROD"  */
    delete LIMISEGPROD
    from   LIMISEGPROD t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "REFORFIL"  */
    delete REFORFIL
    from   REFORFIL t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "CALECOMP"  */
    delete CALECOMP
    from   CALECOMP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "EMPRCNTRFIL"  */
    delete EMPRCNTRFIL
    from   EMPRCNTRFIL t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "EXPAUTFILIAL"  */
    delete EXPAUTFILIAL
    from   EXPAUTFILIAL t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "LOGTRMOVCONS"  */
    delete LOGTRMOVCONS
    from   LOGTRMOVCONS t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "FILGRUPFILI"  */
    delete FILGRUPFILI
    from   FILGRUPFILI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "INSPRECRPADR"  */
    delete INSPRECRPADR
    from   INSPRECRPADR t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "INSPRECRDEPT"  */
    delete INSPRECRDEPT
    from   INSPRECRDEPT t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "INSPRECRCARG"  */
    delete INSPRECRCARG
    from   INSPRECRCARG t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "EXPFILCLIE"  */
    delete EXPFILCLIE
    from   EXPFILCLIE t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "REGRAREVRECE"  */
    delete REGRAREVRECE
    from   REGRAREVRECE t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
    
    /*  Delete all children in "PERCACRPROD"  */
    delete PERCACRPROD
    from   PERCACRPROD t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL

    return

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

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

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

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

    return

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

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

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

    
    /*  Delete all children in "FUNC_FOLHA"  */
    delete FUNC_FOLHA
    from   FUNC_FOLHA t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.AAMECMPFOLHA = t1.AAMECMPFOLHA
     and   t2.NRVERSFOLHA = t1.NRVERSFOLHA

    return

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

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

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

    
    /*  Cannot delete parent "FORMPGTO" if children still exist in "CLIENTE"  */
    if exists (select 1
               from   CLIENTE t2, deleted t1
               where  t2.CDFORMPGTO = t1.CDFORMPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENTE". Cannot delete parent "FORMPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "FORMPGTO" if children still exist in "FORNECEDOR"  */
    if exists (select 1
               from   FORNECEDOR t2, deleted t1
               where  t2.CDFORMPGTO = t1.CDFORMPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORNECEDOR". Cannot delete parent "FORMPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "FORMPGTO" if children still exist in "AUTOFORNE"  */
    if exists (select 1
               from   AUTOFORNE t2, deleted t1
               where  t2.CDFORMPGTO = t1.CDFORMPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUTOFORNE". Cannot delete parent "FORMPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "FORMPGTO" if children still exist in "ITEMCOLE"  */
    if exists (select 1
               from   ITEMCOLE t2, deleted t1
               where  t2.CDFORMPGTO = t1.CDFORMPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMCOLE". Cannot delete parent "FORMPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "FORMPGTO" if children still exist in "PRODFORN"  */
    if exists (select 1
               from   PRODFORN t2, deleted t1
               where  t2.CDFORMPGTO = t1.CDFORMPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFORN". Cannot delete parent "FORMPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "FORMPGTO" if children still exist in "PEDIDO"  */
    if exists (select 1
               from   PEDIDO t2, deleted t1
               where  t2.CDFORMPGTO = t1.CDFORMPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDO". Cannot delete parent "FORMPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "FORMPGTO" if children still exist in "RELFORMPGTO"  */
    if exists (select 1
               from   RELFORMPGTO t2, deleted t1
               where  t2.CDFORMPGTO = t1.CDFORMPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELFORMPGTO". Cannot delete parent "FORMPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "FORMPGTO" if children still exist in "MODPGTO"  */
    if exists (select 1
               from   MODPGTO t2, deleted t1
               where  t2.CDFORMPGTO = t1.CDFORMPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MODPGTO". Cannot delete parent "FORMPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "FORMPGTO" if children still exist in "SERVCONTRAT"  */
    if exists (select 1
               from   SERVCONTRAT t2, deleted t1
               where  t2.CDFORMPGTO = t1.CDFORMPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCONTRAT". Cannot delete parent "FORMPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "FORMPGTO" if children still exist in "FORMPRPROT"  */
    if exists (select 1
               from   FORMPRPROT t2, deleted t1
               where  t2.CDFORMPGTO = t1.CDFORMPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORMPRPROT". Cannot delete parent "FORMPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "FORMPGTO" if children still exist in "PROPOSTA"  */
    if exists (select 1
               from   PROPOSTA t2, deleted t1
               where  t2.CDFORMPGTO = t1.CDFORMPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPOSTA". Cannot delete parent "FORMPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "FORMPGTO" if children still exist in "ITMPREVLIST"  */
    if exists (select 1
               from   ITMPREVLIST t2, deleted t1
               where  t2.CDFORMPGTO = t1.CDFORMPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITMPREVLIST". Cannot delete parent "FORMPGTO".'
          goto error
       end
    
    /*  Delete all children in "DIAPGTO"  */
    delete DIAPGTO
    from   DIAPGTO t2, deleted t1
    where  t2.CDFORMPGTO = t1.CDFORMPGTO

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "IMPOSTO"  */
    if exists (select 1
               from   IMPOSTO t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSTO". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "BOLETPAGAR"  */
    if exists (select 1
               from   BOLETPAGAR t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BOLETPAGAR". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "ITEMAUTF"  */
    if exists (select 1
               from   ITEMAUTF t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMAUTF". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "TESTEPROD"  */
    if exists (select 1
               from   TESTEPROD t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TESTEPROD". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "ITEMCOLE"  */
    if exists (select 1
               from   ITEMCOLE t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMCOLE". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "AUTOFORNE"  */
    if exists (select 1
               from   AUTOFORNE t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUTOFORNE". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "MOVOCORR"  */
    if exists (select 1
               from   MOVOCORR t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOCORR". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "PARTFILI"  */
    if exists (select 1
               from   PARTFILI t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARTFILI". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "BEM"  */
    if exists (select 1
               from   BEM t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BEM". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "CONTATO"  */
    if exists (select 1
               from   CONTATO t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTATO". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "CCORFORN"  */
    if exists (select 1
               from   CCORFORN t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CCORFORN". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "PEDCOLETA"  */
    if exists (select 1
               from   PEDCOLETA t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDCOLETA". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "QFOPRODFORN"  */
    if exists (select 1
               from   QFOPRODFORN t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "QFOPRODFORN". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "ACAO"  */
    if exists (select 1
               from   ACAO t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACAO". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "VISTEQFORN"  */
    if exists (select 1
               from   VISTEQFORN t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VISTEQFORN". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "LOGAPURQFO"  */
    if exists (select 1
               from   LOGAPURQFO t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGAPURQFO". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "PREVSAIDA"  */
    if exists (select 1
               from   PREVSAIDA t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVSAIDA". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "ATENHOSP"  */
    if exists (select 1
               from   ATENHOSP t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ATENHOSP". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "HONORATHOSP"  */
    if exists (select 1
               from   HONORATHOSP t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HONORATHOSP". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "ITREQUPROC"  */
    if exists (select 1
               from   ITREQUPROC t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITREQUPROC". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "AUDITORIA"  */
    if exists (select 1
               from   AUDITORIA t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUDITORIA". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "EQUIPAPROD"  */
    if exists (select 1
               from   EQUIPAPROD t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQUIPAPROD". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "RECEBDOAC"  */
    if exists (select 1
               from   RECEBDOAC t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEBDOAC". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "HEMOPRDOAC"  */
    if exists (select 1
               from   HEMOPRDOAC t2, deleted t1
               where  t2.CDFORORIHEMO = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HEMOPRDOAC". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "HEMOPRDOAC"  */
    if exists (select 1
               from   HEMOPRDOAC t2, deleted t1
               where  t2.CDFORDESHEMO = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HEMOPRDOAC". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "PROTESE"  */
    if exists (select 1
               from   PROTESE t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROTESE". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "FORMPRPROT"  */
    if exists (select 1
               from   FORMPRPROT t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORMPRPROT". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "VENDEDOR"  */
    if exists (select 1
               from   VENDEDOR t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDEDOR". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "PARAMPRODSUP"  */
    if exists (select 1
               from   PARAMPRODSUP t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPRODSUP". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "DESPHOSP"  */
    if exists (select 1
               from   DESPHOSP t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESPHOSP". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "TAREFA"  */
    if exists (select 1
               from   TAREFA t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAREFA". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "MOVOCORR"  */
    if exists (select 1
               from   MOVOCORR t2, deleted t1
               where  t2.CDFORNTRANSP = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOCORR". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "CODIGOBARRA"  */
    if exists (select 1
               from   CODIGOBARRA t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODIGOBARRA". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "EMITAFFORN"  */
    if exists (select 1
               from   EMITAFFORN t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMITAFFORN". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "BEM"  */
    if exists (select 1
               from   BEM t2, deleted t1
               where  t2.CDFORNSEGURA = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BEM". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "ATIVCLIEFORN"  */
    if exists (select 1
               from   ATIVCLIEFORN t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ATIVCLIEFORN". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "TRANSPORT"  */
    if exists (select 1
               from   TRANSPORT t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TRANSPORT". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "MINFORNREG"  */
    if exists (select 1
               from   MINFORNREG t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MINFORNREG". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "FORPRCOMPRA"  */
    if exists (select 1
               from   FORPRCOMPRA t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORPRCOMPRA". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "FILIAL"  */
    if exists (select 1
               from   FILIAL t2, deleted t1
               where  t2.CDFORNOPELOG = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIAL". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "CUSTOLOGIST"  */
    if exists (select 1
               from   CUSTOLOGIST t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOLOGIST". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "MOBRAORCFORN"  */
    if exists (select 1
               from   MOBRAORCFORN t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOBRAORCFORN". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "ITEMORCAEVEN"  */
    if exists (select 1
               from   ITEMORCAEVEN t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMORCAEVEN". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "ACERTOHREXTR"  */
    if exists (select 1
               from   ACERTOHREXTR t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACERTOHREXTR". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "PARAMORCAEVE"  */
    if exists (select 1
               from   PARAMORCAEVE t2, deleted t1
               where  t2.CDFORNPADORC = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMORCAEVE". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "PERMUTAORE"  */
    if exists (select 1
               from   PERMUTAORE t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERMUTAORE". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "FORNECEDOR"  */
    if exists (select 1
               from   FORNECEDOR t2, deleted t1
               where  t2.CDFORNERELAC = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORNECEDOR". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "MOVCREDBANC"  */
    if exists (select 1
               from   MOVCREDBANC t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVCREDBANC". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "MOVDEBBANC"  */
    if exists (select 1
               from   MOVDEBBANC t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVDEBBANC". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "FORNEPEDCOL"  */
    if exists (select 1
               from   FORNEPEDCOL t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORNEPEDCOL". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "ETLCOMPRASGER"  */
    if exists (select 1
               from   ETLCOMPRASGER t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCOMPRASGER". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "ETLCONTAPAG"  */
    if exists (select 1
               from   ETLCONTAPAG t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCONTAPAG". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "FORNECEDOR" if children still exist in "SOLICITASUPRI"  */
    if exists (select 1
               from   SOLICITASUPRI t2, deleted t1
               where  t2.CDFORNBENEF = t1.CDFORNECED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICITASUPRI". Cannot delete parent "FORNECEDOR".'
          goto error
       end
    
    /*  Delete all children in "IMPOSFORN"  */
    delete IMPOSFORN
    from   IMPOSFORN t2, deleted t1
    where  t2.CDFORNECED = t1.CDFORNECED
    
    /*  Delete all children in "ENDEFORN"  */
    delete ENDEFORN
    from   ENDEFORN t2, deleted t1
    where  t2.CDFORNECED = t1.CDFORNECED
    
    /*  Delete all children in "MARCCOTA"  */
    delete MARCCOTA
    from   MARCCOTA t2, deleted t1
    where  t2.CDFORNECED = t1.CDFORNECED
    
    /*  Delete all children in "INTEFORN"  */
    delete INTEFORN
    from   INTEFORN t2, deleted t1
    where  t2.CDFORNECED = t1.CDFORNECED
    
    /*  Delete all children in "REFORFIL"  */
    delete REFORFIL
    from   REFORFIL t2, deleted t1
    where  t2.CDFORNECED = t1.CDFORNECED
    
    /*  Delete all children in "REFOREG"  */
    delete REFOREG
    from   REFOREG t2, deleted t1
    where  t2.CDFORNECED = t1.CDFORNECED
    
    /*  Delete all children in "PRODFORN"  */
    delete PRODFORN
    from   PRODFORN t2, deleted t1
    where  t2.CDFORNECED = t1.CDFORNECED
    
    /*  Delete all children in "OBSERVFORN"  */
    delete OBSERVFORN
    from   OBSERVFORN t2, deleted t1
    where  t2.CDFORNECED = t1.CDFORNECED
    
    /*  Delete all children in "PRAZOPGTFORN"  */
    delete PRAZOPGTFORN
    from   PRAZOPGTFORN t2, deleted t1
    where  t2.CDFORNECED = t1.CDFORNECED
    
    /*  Delete all children in "HISTQUALFORN"  */
    delete HISTQUALFORN
    from   HISTQUALFORN t2, deleted t1
    where  t2.CDFORNECED = t1.CDFORNECED

    return

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

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

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

    
    /*  Delete all children in "REENVIOMAILPCOL"  */
    delete REENVIOMAILPCOL
    from   REENVIOMAILPCOL t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRPEDIDOCOL = t1.NRPEDIDOCOL
     and   t2.CDFORNECED = t1.CDFORNECED

    return

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

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

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

    
    /*  Delete all children in "LANCAM_FOLHA"  */
    delete LANCAM_FOLHA
    from   LANCAM_FOLHA t2, deleted t1
    where  t2.AAMECMPFOLHA = t1.AAMECMPFOLHA
     and   t2.NRVERSFOLHA = t1.NRVERSFOLHA
     and   t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDFUNC = t1.CDFUNC

    return

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

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

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

    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "ESTRUATEN"  */
    if exists (select 1
               from   ESTRUATEN t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESTRUATEN". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "ESPEPROFFUNC"  */
    if exists (select 1
               from   ESPEPROFFUNC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESPEPROFFUNC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "HORAPADR"  */
    if exists (select 1
               from   HORAPADR t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HORAPADR". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "AUSEFUNC"  */
    if exists (select 1
               from   AUSEFUNC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUSEFUNC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "CONVACEITO"  */
    if exists (select 1
               from   CONVACEITO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONVACEITO". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "ETAPATEN"  */
    if exists (select 1
               from   ETAPATEN t2, deleted t1
               where  t2.CDEMPRETAP = t1.CDEMPRESA
                and   t2.CDFUNCETAP = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETAPATEN". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "HOSPMEDI"  */
    if exists (select 1
               from   HOSPMEDI t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HOSPMEDI". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "RETOPROG"  */
    if exists (select 1
               from   RETOPROG t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RETOPROG". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "LISTAESPR"  */
    if exists (select 1
               from   LISTAESPR t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LISTAESPR". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" 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)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXTRMARC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "MARCACAO"  */
    if exists (select 1
               from   MARCACAO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MARCACAO". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "APONTAMENTO"  */
    if exists (select 1
               from   APONTAMENTO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APONTAMENTO". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "REMU_RH_FUNC"  */
    if exists (select 1
               from   REMU_RH_FUNC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REMU_RH_FUNC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "ESC_TRABALHO"  */
    if exists (select 1
               from   ESC_TRABALHO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESC_TRABALHO". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "MOVI_PESSOAL"  */
    if exists (select 1
               from   MOVI_PESSOAL t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVI_PESSOAL". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "DESLIGAMENTO"  */
    if exists (select 1
               from   DESLIGAMENTO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESLIGAMENTO". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "FOTO_FUNC"  */
    if exists (select 1
               from   FOTO_FUNC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FOTO_FUNC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "DEPENDENTE"  */
    if exists (select 1
               from   DEPENDENTE t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DEPENDENTE". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "TESTEFUNC"  */
    if exists (select 1
               from   TESTEFUNC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TESTEFUNC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "CPREV_DEMIT"  */
    if exists (select 1
               from   CPREV_DEMIT t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CPREV_DEMIT". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "RESUMOMENSAL"  */
    if exists (select 1
               from   RESUMOMENSAL t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESUMOMENSAL". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "CPREV_TEMP"  */
    if exists (select 1
               from   CPREV_TEMP t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CPREV_TEMP". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "ESCALANORMAL"  */
    if exists (select 1
               from   ESCALANORMAL t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESCALANORMAL". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "HAB_BENEFICIO"  */
    if exists (select 1
               from   HAB_BENEFICIO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HAB_BENEFICIO". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "MARCSUSP"  */
    if exists (select 1
               from   MARCSUSP t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MARCSUSP". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "AGENTEMP"  */
    if exists (select 1
               from   AGENTEMP t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENTEMP". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "CONSMEDI"  */
    if exists (select 1
               from   CONSMEDI t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSMEDI". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "PONTO"  */
    if exists (select 1
               from   PONTO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PONTO". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "SERVPREST"  */
    if exists (select 1
               from   SERVPREST t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVPREST". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "FERIAS"  */
    if exists (select 1
               from   FERIAS t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FERIAS". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "EVENTO_FUNC"  */
    if exists (select 1
               from   EVENTO_FUNC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EVENTO_FUNC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "FUNC_FOLHA"  */
    if exists (select 1
               from   FUNC_FOLHA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FUNC_FOLHA". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "VALE_TRANSP"  */
    if exists (select 1
               from   VALE_TRANSP t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VALE_TRANSP". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "ALTERAFUNC"  */
    if exists (select 1
               from   ALTERAFUNC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ALTERAFUNC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "COMISSAOFUNC"  */
    if exists (select 1
               from   COMISSAOFUNC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMISSAOFUNC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "FGTS_FUNC"  */
    if exists (select 1
               from   FGTS_FUNC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FGTS_FUNC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "MOVFUNCRH"  */
    if exists (select 1
               from   MOVFUNCRH t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVFUNCRH". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "FALTA"  */
    if exists (select 1
               from   FALTA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FALTA". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "MANUPONTO"  */
    if exists (select 1
               from   MANUPONTO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MANUPONTO". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "AUTOEXTRA"  */
    if exists (select 1
               from   AUTOEXTRA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUTOEXTRA". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "AGENDEMO"  */
    if exists (select 1
               from   AGENDEMO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENDEMO". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "TREINAMENT"  */
    if exists (select 1
               from   TREINAMENT t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TREINAMENT". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "APRHRTRAB"  */
    if exists (select 1
               from   APRHRTRAB t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APRHRTRAB". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "BATIDAPONTO"  */
    if exists (select 1
               from   BATIDAPONTO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BATIDAPONTO". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "NOTAAVAL"  */
    if exists (select 1
               from   NOTAAVAL t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAAVAL". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "TURMAPAD"  */
    if exists (select 1
               from   TURMAPAD t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TURMAPAD". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "TURMAPERIODO"  */
    if exists (select 1
               from   TURMAPERIODO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TURMAPERIODO". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "DISPONIBFUNC"  */
    if exists (select 1
               from   DISPONIBFUNC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DISPONIBFUNC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "RESERVATURMA"  */
    if exists (select 1
               from   RESERVATURMA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESERVATURMA". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "DISCIPLIESTA"  */
    if exists (select 1
               from   DISCIPLIESTA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DISCIPLIESTA". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "AULATURMA"  */
    if exists (select 1
               from   AULATURMA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AULATURMA". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "PROSPECT"  */
    if exists (select 1
               from   PROSPECT t2, deleted t1
               where  t2.CDEMPFCAPPRO = t1.CDEMPRESA
                and   t2.CDFUNCCAPPRO = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROSPECT". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "AGEATIVISALA"  */
    if exists (select 1
               from   AGEATIVISALA t2, deleted t1
               where  t2.CDEMPFSOLAGS = t1.CDEMPRESA
                and   t2.CDFUNCSOLAGS = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGEATIVISALA". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "EMPRESTPROD"  */
    if exists (select 1
               from   EMPRESTPROD t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMPRESTPROD". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "RESERVAPROD"  */
    if exists (select 1
               from   RESERVAPROD t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESERVAPROD". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "PARTATIVSALA"  */
    if exists (select 1
               from   PARTATIVSALA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARTATIVSALA". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "MATRICULA"  */
    if exists (select 1
               from   MATRICULA t2, deleted t1
               where  t2.CDEMPFRESMAT = t1.CDEMPRESA
                and   t2.CDFUNCRESMAT = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MATRICULA". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "MATRICULA"  */
    if exists (select 1
               from   MATRICULA t2, deleted t1
               where  t2.CDEMPFCAPMAT = t1.CDEMPRESA
                and   t2.CDFUNCCAPMAT = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MATRICULA". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "CLIENTE"  */
    if exists (select 1
               from   CLIENTE t2, deleted t1
               where  t2.CDEMPFCAPCLI = t1.CDEMPRESA
                and   t2.CDFUNCCAPCLI = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENTE". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "CLIENTE"  */
    if exists (select 1
               from   CLIENTE t2, deleted t1
               where  t2.CDEMPFAPLTES = t1.CDEMPRESA
                and   t2.CDFUNCAPLTES = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENTE". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "FUNCESTA"  */
    if exists (select 1
               from   FUNCESTA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FUNCESTA". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "PROGRAMAULA"  */
    if exists (select 1
               from   PROGRAMAULA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROGRAMAULA". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "MOVESTMATDID"  */
    if exists (select 1
               from   MOVESTMATDID t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVESTMATDID". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "ATIVAPRHR"  */
    if exists (select 1
               from   ATIVAPRHR t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ATIVAPRHR". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "FUNCATHOSP"  */
    if exists (select 1
               from   FUNCATHOSP t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FUNCATHOSP". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "HONORATHOSP"  */
    if exists (select 1
               from   HONORATHOSP t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HONORATHOSP". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "REQUPROC"  */
    if exists (select 1
               from   REQUPROC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUPROC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "ITREQUPROC"  */
    if exists (select 1
               from   ITREQUPROC t2, deleted t1
               where  t2.CDEMPFUNCTEC = t1.CDEMPRESA
                and   t2.CDFUNRESPTEC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITREQUPROC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "ITREQUPROC"  */
    if exists (select 1
               from   ITREQUPROC t2, deleted t1
               where  t2.CDEMPFUNCLAU = t1.CDEMPRESA
                and   t2.CDFUNRESPLAU = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITREQUPROC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "AGENFUNC"  */
    if exists (select 1
               from   AGENFUNC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENFUNC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "EXAMDOACAO"  */
    if exists (select 1
               from   EXAMDOACAO t2, deleted t1
               where  t2.CDEMPFUNCEXD = t1.CDEMPRESA
                and   t2.CDFUNRESPEXD = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXAMDOACAO". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "AFERESE"  */
    if exists (select 1
               from   AFERESE t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AFERESE". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "FMEDFUNC"  */
    if exists (select 1
               from   FMEDFUNC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FMEDFUNC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "OPERADOR"  */
    if exists (select 1
               from   OPERADOR t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERADOR". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "DESPHOSP"  */
    if exists (select 1
               from   DESPHOSP t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESPHOSP". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "LIMCONVACE"  */
    if exists (select 1
               from   LIMCONVACE t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LIMCONVACE". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "REQUHOSP"  */
    if exists (select 1
               from   REQUHOSP t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUHOSP". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "LOGPONTO"  */
    if exists (select 1
               from   LOGPONTO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGPONTO". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "FERIFUNC"  */
    if exists (select 1
               from   FERIFUNC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FERIFUNC". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "FUNCRELEQUIP"  */
    if exists (select 1
               from   FUNCRELEQUIP t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FUNCRELEQUIP". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "AGENDASERV"  */
    if exists (select 1
               from   AGENDASERV t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENDASERV". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "RATECOMILINP"  */
    if exists (select 1
               from   RATECOMILINP t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATECOMILINP". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "CHT"  */
    if exists (select 1
               from   CHT t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CHT". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "LANCATESTADO"  */
    if exists (select 1
               from   LANCATESTADO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCATESTADO". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Cannot delete parent "FUNCIONARIO" if children still exist in "PESSOA"  */
    if exists (select 1
               from   PESSOA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PESSOA". Cannot delete parent "FUNCIONARIO".'
          goto error
       end
    
    /*  Delete all children in "OBSERVFUNC"  */
    delete OBSERVFUNC
    from   OBSERVFUNC t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDFUNC = t1.CDFUNC
    
    /*  Delete all children in "HORBASFU"  */
    delete HORBASFU
    from   HORBASFU t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDFUNC = t1.CDFUNC
    
    /*  Delete all children in "AFASTAMENTO"  */
    delete AFASTAMENTO
    from   AFASTAMENTO t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDFUNC = t1.CDFUNC

    return

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

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

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

    
    /*  Delete all children in "ROMANEIONF"  */
    delete ROMANEIONF
    from   ROMANEIONF t2, deleted t1
    where  t2.CDFILIALNFRO = t1.CDFILIAL
     and   t2.NRGERANFROMA = t1.NRGERANFROMA

    return

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

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

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

    
    /*  Cannot delete parent "GERENTE" if children still exist in "LIMGERPROD"  */
    if exists (select 1
               from   LIMGERPROD t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LIMGERPROD". Cannot delete parent "GERENTE".'
          goto error
       end
    
    /*  Cannot delete parent "GERENTE" if children still exist in "AUTOFORNE"  */
    if exists (select 1
               from   AUTOFORNE t2, deleted t1
               where  t2.CDGERENTE = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUTOFORNE". Cannot delete parent "GERENTE".'
          goto error
       end
    
    /*  Cannot delete parent "GERENTE" if children still exist in "AUTOFORNE"  */
    if exists (select 1
               from   AUTOFORNE t2, deleted t1
               where  t2.CDGEREORCFIN = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUTOFORNE". Cannot delete parent "GERENTE".'
          goto error
       end
    
    /*  Delete all children in "GERECOMP"  */
    delete GERECOMP
    from   GERECOMP t2, deleted t1
    where  t2.CDOPERGERE = t1.CDOPERADOR

    return

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

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

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

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

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "OPERGRUPOP"  */
    if exists (select 1
               from   OPERGRUPOP t2, deleted t1
               where  t2.CDGRUPOPER = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERGRUPOP". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMESTOQUE"  */
    if exists (select 1
               from   PARAMESTOQUE t2, deleted t1
               where  t2.CDGROPERRETI = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMESTOQUE". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDGROPERRESE = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDGROPERREEX = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMESTOQUE"  */
    if exists (select 1
               from   PARAMESTOQUE t2, deleted t1
               where  t2.CDGROPERFMES = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMESTOQUE". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDGROPERREPA = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARASUPRI"  */
    if exists (select 1
               from   PARASUPRI t2, deleted t1
               where  t2.CDGRUPOPER = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARASUPRI". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMPEDIDO"  */
    if exists (select 1
               from   PARAMPEDIDO t2, deleted t1
               where  t2.CDGROPERAPRO = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPEDIDO". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMESTOQUE"  */
    if exists (select 1
               from   PARAMESTOQUE t2, deleted t1
               where  t2.CDGROPERSOLI = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMESTOQUE". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "GROPERVISTO"  */
    if exists (select 1
               from   GROPERVISTO t2, deleted t1
               where  t2.CDGRUPOPER = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GROPERVISTO". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDGROPERPDV = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMPEDIDO"  */
    if exists (select 1
               from   PARAMPEDIDO t2, deleted t1
               where  t2.CDGROPERDESC = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPEDIDO". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDGROPERPDV1 = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDGROPERPROP = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDGROPERPROR = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "GERAL_PAG"  */
    if exists (select 1
               from   GERAL_PAG t2, deleted t1
               where  t2.CDGROPERCNTP = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERAL_PAG". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "GERAL_REC"  */
    if exists (select 1
               from   GERAL_REC t2, deleted t1
               where  t2.CDGROPERCNTR = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERAL_REC". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDGROPERPROB = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "AUTGRUPOP"  */
    if exists (select 1
               from   AUTGRUPOP t2, deleted t1
               where  t2.CDGRUPOPER = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUTGRUPOP". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARGERALVMN"  */
    if exists (select 1
               from   PARGERALVMN t2, deleted t1
               where  t2.CDGROPERABAS = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARGERALVMN". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "FASETRAM"  */
    if exists (select 1
               from   FASETRAM t2, deleted t1
               where  t2.CDGROPERRESP = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FASETRAM". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PAREXPEDICAO"  */
    if exists (select 1
               from   PAREXPEDICAO t2, deleted t1
               where  t2.CDGROPERTROC = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PAREXPEDICAO". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMPEDIDO"  */
    if exists (select 1
               from   PARAMPEDIDO t2, deleted t1
               where  t2.CDGROPLIBINC = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPEDIDO". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARASUPRI"  */
    if exists (select 1
               from   PARASUPRI t2, deleted t1
               where  t2.CDGRUPAPRSOL = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARASUPRI". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMCOMIS"  */
    if exists (select 1
               from   PARAMCOMIS t2, deleted t1
               where  t2.CDGROPCANDEL = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMCOMIS". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "GRUPOPROJETO"  */
    if exists (select 1
               from   GRUPOPROJETO t2, deleted t1
               where  t2.CDGRUPOPER = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GRUPOPROJETO". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "CONSULTADW"  */
    if exists (select 1
               from   CONSULTADW t2, deleted t1
               where  t2.CDGRUPOPER = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSULTADW". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMESTOQUE"  */
    if exists (select 1
               from   PARAMESTOQUE t2, deleted t1
               where  t2.CDGROPERREQ = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMESTOQUE". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "PARAMORCAEVE"  */
    if exists (select 1
               from   PARAMORCAEVE t2, deleted t1
               where  t2.CDGROPEPROPRAZO = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMORCAEVE". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "ITEMDWGRUPOP"  */
    if exists (select 1
               from   ITEMDWGRUPOP t2, deleted t1
               where  t2.CDGRUPOPER = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMDWGRUPOP". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "DIMEDWGRUPOP"  */
    if exists (select 1
               from   DIMEDWGRUPOP t2, deleted t1
               where  t2.CDGRUPOPER = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIMEDWGRUPOP". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPER" if children still exist in "GROPMAPARESULT"  */
    if exists (select 1
               from   GROPMAPARESULT t2, deleted t1
               where  t2.CDGRUPOPER = t1.CDGRUPOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GROPMAPARESULT". Cannot delete parent "GRUPOPER".'
          goto error
       end
    
    /*  Delete all children in "PROGGRUPOP"  */
    delete PROGGRUPOP
    from   PROGGRUPOP t2, deleted t1
    where  t2.CDGRUPOPER = t1.CDGRUPOPER
    
    /*  Delete all children in "GROPERALMOX"  */
    delete GROPERALMOX
    from   GROPERALMOX t2, deleted t1
    where  t2.CDGRUPOPER = t1.CDGRUPOPER
    
    /*  Delete all children in "GROPERLESTOQ"  */
    delete GROPERLESTOQ
    from   GROPERLESTOQ t2, deleted t1
    where  t2.CDGRUPOPER = t1.CDGRUPOPER
    
    /*  Delete all children in "MENUGRUPOP"  */
    delete MENUGRUPOP
    from   MENUGRUPOP t2, deleted t1
    where  t2.CDGRUPOPER = t1.CDGRUPOPER
    
    /*  Delete all children in "CONSDWGRUPOP"  */
    delete CONSDWGRUPOP
    from   CONSDWGRUPOP t2, deleted t1
    where  t2.CDGRUPOPER = t1.CDGRUPOPER

    return

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

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

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

    
    /*  Cannot delete parent "GRUPOPROJETO" if children still exist in "PROJETO"  */
    if exists (select 1
               from   PROJETO t2, deleted t1
               where  t2.CDGRUPOPROJ = t1.CDGRUPOPROJ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROJETO". Cannot delete parent "GRUPOPROJETO".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPROJETO" if children still exist in "BASELINEGRPROJ"  */
    if exists (select 1
               from   BASELINEGRPROJ t2, deleted t1
               where  t2.CDGRUPOPROJ = t1.CDGRUPOPROJ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BASELINEGRPROJ". Cannot delete parent "GRUPOPROJETO".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPROJETO" if children still exist in "OPERGRPROJETO"  */
    if exists (select 1
               from   OPERGRPROJETO t2, deleted t1
               where  t2.CDGRUPOPROJ = t1.CDGRUPOPROJ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERGRPROJETO". Cannot delete parent "GRUPOPROJETO".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPROJETO" if children still exist in "ACORDOGRPROJETO"  */
    if exists (select 1
               from   ACORDOGRPROJETO t2, deleted t1
               where  t2.CDGRUPOPROJ = t1.CDGRUPOPROJ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACORDOGRPROJETO". Cannot delete parent "GRUPOPROJETO".'
          goto error
       end
    
    /*  Cannot delete parent "GRUPOPROJETO" if children still exist in "SOLGRACORDOPROJ"  */
    if exists (select 1
               from   SOLGRACORDOPROJ t2, deleted t1
               where  t2.CDGRUPOPROJ = t1.CDGRUPOPROJ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLGRACORDOPROJ". Cannot delete parent "GRUPOPROJETO".'
          goto error
       end
    
    /*  Delete all children in "DATAGRPROJETO"  */
    delete DATAGRPROJETO
    from   DATAGRPROJETO t2, deleted t1
    where  t2.CDGRUPOPROJ = t1.CDGRUPOPROJ

    return

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

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

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

    
    /*  Delete all children in "PARAMGPROC"  */
    delete PARAMGPROC
    from   PARAMGPROC t2, deleted t1
    where  t2.CDGRUPROCESSO = t1.CDGRUPROCESSO
    
    /*  Delete all children in "PROCESGPROC"  */
    delete PROCESGPROC
    from   PROCESGPROC t2, deleted t1
    where  t2.CDGRUPROCESSO = t1.CDGRUPROCESSO

    return

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

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

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

    
    /*  Cannot delete parent "GRUPSERVFAT" 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
                and   t2.CDGRSERVFAT = t1.CDGRSERVFAT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCLIE". Cannot delete parent "GRUPSERVFAT".'
          goto error
       end
    
    /*  Delete all children in "CORRPRECGRUP"  */
    delete CORRPRECGRUP
    from   CORRPRECGRUP t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDGRSERVFAT = t1.CDGRSERVFAT

    return

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

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

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

    
    /*  Cannot delete parent "HABILIDADE" if children still exist in "RECURETAPA"  */
    if exists (select 1
               from   RECURETAPA t2, deleted t1
               where  t2.CDHABILIDADE = t1.CDHABILIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECURETAPA". Cannot delete parent "HABILIDADE".'
          goto error
       end
    
    /*  Cannot delete parent "HABILIDADE" if children still exist in "RECURETAPAOP"  */
    if exists (select 1
               from   RECURETAPAOP t2, deleted t1
               where  t2.CDHABILIDADE = t1.CDHABILIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECURETAPAOP". Cannot delete parent "HABILIDADE".'
          goto error
       end
    
    /*  Cannot delete parent "HABILIDADE" if children still exist in "HABIPROCESSO"  */
    if exists (select 1
               from   HABIPROCESSO t2, deleted t1
               where  t2.CDHABILIDADE = t1.CDHABILIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HABIPROCESSO". Cannot delete parent "HABILIDADE".'
          goto error
       end
    
    /*  Cannot delete parent "HABILIDADE" if children still exist in "COMPOSINSPEC"  */
    if exists (select 1
               from   COMPOSINSPEC t2, deleted t1
               where  t2.CDHABILIDADE = t1.CDHABILIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPOSINSPEC". Cannot delete parent "HABILIDADE".'
          goto error
       end
    
    /*  Cannot delete parent "HABILIDADE" if children still exist in "HABIGRUPCARG"  */
    if exists (select 1
               from   HABIGRUPCARG t2, deleted t1
               where  t2.CDHABILIDADE = t1.CDHABILIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HABIGRUPCARG". Cannot delete parent "HABILIDADE".'
          goto error
       end
    
    /*  Cannot delete parent "HABILIDADE" if children still exist in "HABILPROJETO"  */
    if exists (select 1
               from   HABILPROJETO t2, deleted t1
               where  t2.CDHABILIDADE = t1.CDHABILIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HABILPROJETO". Cannot delete parent "HABILIDADE".'
          goto error
       end
    
    /*  Cannot delete parent "HABILIDADE" if children still exist in "HABILOPERPRJ"  */
    if exists (select 1
               from   HABILOPERPRJ t2, deleted t1
               where  t2.CDHABILIDADE = t1.CDHABILIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HABILOPERPRJ". Cannot delete parent "HABILIDADE".'
          goto error
       end
    
    /*  Delete all children in "HABILOPER"  */
    delete HABILOPER
    from   HABILOPER t2, deleted t1
    where  t2.CDHABILIDADE = t1.CDHABILIDADE

    return

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

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

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

    
    /*  Delete all children in "OBSJUSTORCA"  */
    delete OBSJUSTORCA
    from   OBSJUSTORCA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
     and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
     and   t2.CDOPERADOR = t1.CDOPERADOR
     and   t2.DTHRHISTORCA = t1.DTHRHISTORCA

    return

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

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

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

    
    /*  Cannot delete parent "HISTPADR" if children still exist in "LANCLOTE"  */
    if exists (select 1
               from   LANCLOTE t2, deleted t1
               where  t2.CDHISTPADR = t1.CDHISTPADR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCLOTE". Cannot delete parent "HISTPADR".'
          goto error
       end
    
    /*  Cannot delete parent "HISTPADR" if children still exist in "LANCCONT"  */
    if exists (select 1
               from   LANCCONT t2, deleted t1
               where  t2.CDHISTPADR = t1.CDHISTPADR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCCONT". Cannot delete parent "HISTPADR".'
          goto error
       end
    
    /*  Cannot delete parent "HISTPADR" if children still exist in "TIPOAJRECEBER"  */
    if exists (select 1
               from   TIPOAJRECEBER t2, deleted t1
               where  t2.CDHISTPADR = t1.CDHISTPADR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOAJRECEBER". Cannot delete parent "HISTPADR".'
          goto error
       end
    
    /*  Cannot delete parent "HISTPADR" if children still exist in "TPCREDBANC"  */
    if exists (select 1
               from   TPCREDBANC t2, deleted t1
               where  t2.CDHISTPADR = t1.CDHISTPADR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TPCREDBANC". Cannot delete parent "HISTPADR".'
          goto error
       end
    
    /*  Cannot delete parent "HISTPADR" if children still exist in "TPDEBIBANC"  */
    if exists (select 1
               from   TPDEBIBANC t2, deleted t1
               where  t2.CDHISTPADR = t1.CDHISTPADR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TPDEBIBANC". Cannot delete parent "HISTPADR".'
          goto error
       end
    
    /*  Cannot delete parent "HISTPADR" if children still exist in "TIPOAJPAGAR"  */
    if exists (select 1
               from   TIPOAJPAGAR t2, deleted t1
               where  t2.CDHISTPADR = t1.CDHISTPADR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOAJPAGAR". Cannot delete parent "HISTPADR".'
          goto error
       end
    
    /*  Cannot delete parent "HISTPADR" if children still exist in "HISTINTEGRA"  */
    if exists (select 1
               from   HISTINTEGRA t2, deleted t1
               where  t2.CDHISTPADR = t1.CDHISTPADR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HISTINTEGRA". Cannot delete parent "HISTPADR".'
          goto error
       end
    
    /*  Cannot delete parent "HISTPADR" if children still exist in "TIPOOPERAC"  */
    if exists (select 1
               from   TIPOOPERAC t2, deleted t1
               where  t2.CDHISTPADR = t1.CDHISTPADR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOOPERAC". Cannot delete parent "HISTPADR".'
          goto error
       end
    
    /*  Cannot delete parent "HISTPADR" if children still exist in "TIPORETI"  */
    if exists (select 1
               from   TIPORETI t2, deleted t1
               where  t2.CDHISTPADR = t1.CDHISTPADR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPORETI". Cannot delete parent "HISTPADR".'
          goto error
       end
    
    /*  Cannot delete parent "HISTPADR" if children still exist in "CUSTOINDI"  */
    if exists (select 1
               from   CUSTOINDI t2, deleted t1
               where  t2.CDHISTPADR = t1.CDHISTPADR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOINDI". Cannot delete parent "HISTPADR".'
          goto error
       end
    
    /*  Cannot delete parent "HISTPADR" if children still exist in "TIPOCTPAGAR"  */
    if exists (select 1
               from   TIPOCTPAGAR t2, deleted t1
               where  t2.CDHISTPADR = t1.CDHISTPADR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOCTPAGAR". Cannot delete parent "HISTPADR".'
          goto error
       end
    
    /*  Cannot delete parent "HISTPADR" if children still exist in "TIPOCTRECEBER"  */
    if exists (select 1
               from   TIPOCTRECEBER t2, deleted t1
               where  t2.CDHISTPADR = t1.CDHISTPADR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOCTRECEBER". Cannot delete parent "HISTPADR".'
          goto error
       end
    
    /*  Cannot delete parent "HISTPADR" if children still exist in "TIPOBAIXAPAG"  */
    if exists (select 1
               from   TIPOBAIXAPAG t2, deleted t1
               where  t2.CDHISTPADR = t1.CDHISTPADR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOBAIXAPAG". Cannot delete parent "HISTPADR".'
          goto error
       end
    
    /*  Cannot delete parent "HISTPADR" if children still exist in "TIPOBAIXAREC"  */
    if exists (select 1
               from   TIPOBAIXAREC t2, deleted t1
               where  t2.CDHISTPADR = t1.CDHISTPADR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOBAIXAREC". Cannot delete parent "HISTPADR".'
          goto error
       end
    
    /*  Delete all children in "HISTTIPOLANC"  */
    delete HISTTIPOLANC
    from   HISTTIPOLANC t2, deleted t1
    where  t2.CDHISTPADR = t1.CDHISTPADR

    return

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

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

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

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

    return

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

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

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

    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.CDIMPOSTOT = t1.CDIMPOSTO
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF

    return

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

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

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

    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "ITEMPROIMPOS"  */
    if exists (select 1
               from   ITEMPROIMPOS t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPROIMPOS". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "IMPOSTONF"  */
    if exists (select 1
               from   IMPOSTONF t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSTONF". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "INCIDENCIA"  */
    if exists (select 1
               from   INCIDENCIA t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INCIDENCIA". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "ALIQUOTA"  */
    if exists (select 1
               from   ALIQUOTA t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ALIQUOTA". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "IMPOSFORN"  */
    if exists (select 1
               from   IMPOSFORN t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSFORN". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "BASECALC"  */
    if exists (select 1
               from   BASECALC t2, deleted t1
               where  t2.CDIMPOBCALC = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BASECALC". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "IMPOSNFTOT"  */
    if exists (select 1
               from   IMPOSNFTOT t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSNFTOT". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "TERMABERENC"  */
    if exists (select 1
               from   TERMABERENC t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TERMABERENC". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "MAPAAPURAC"  */
    if exists (select 1
               from   MAPAAPURAC t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPAAPURAC". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "RELATIMPOS"  */
    if exists (select 1
               from   RELATIMPOS t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELATIMPOS". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "IMPOSERIE"  */
    if exists (select 1
               from   IMPOSERIE t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSERIE". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "IMPITMAF"  */
    if exists (select 1
               from   IMPITMAF t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPITMAF". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "IMPCOLET"  */
    if exists (select 1
               from   IMPCOLET t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPCOLET". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "IMPOSTO"  */
    if exists (select 1
               from   IMPOSTO t2, deleted t1
               where  t2.CDIMPOSAPUR = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSTO". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "RELACIMPOSTO"  */
    if exists (select 1
               from   RELACIMPOSTO t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELACIMPOSTO". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "IMPOSMRDP"  */
    if exists (select 1
               from   IMPOSMRDP t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSMRDP". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "IMPOSINFUSU"  */
    if exists (select 1
               from   IMPOSINFUSU t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSINFUSU". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "PAREXPEDICAO"  */
    if exists (select 1
               from   PAREXPEDICAO t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PAREXPEDICAO". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "ALIQIMPFIS"  */
    if exists (select 1
               from   ALIQIMPFIS t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ALIQIMPFIS". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "ITVENDAIMPOS"  */
    if exists (select 1
               from   ITVENDAIMPOS t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITVENDAIMPOS". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "IMPOSTOREDZ"  */
    if exists (select 1
               from   IMPOSTOREDZ t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSTOREDZ". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "IMPITVENDAVM"  */
    if exists (select 1
               from   IMPITVENDAVM t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPITVENDAVM". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "IMPOSTO"  */
    if exists (select 1
               from   IMPOSTO t2, deleted t1
               where  t2.CDIMPOSTRIB = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSTO". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "PARAMFISCAL"  */
    if exists (select 1
               from   PARAMFISCAL t2, deleted t1
               where  t2.CDIMPATIVPER = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMFISCAL". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "CTBIMPTPOPER"  */
    if exists (select 1
               from   CTBIMPTPOPER t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTBIMPTPOPER". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "IMPOSTOROP"  */
    if exists (select 1
               from   IMPOSTOROP t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSTOROP". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "IMPPEDCOLETA"  */
    if exists (select 1
               from   IMPPEDCOLETA t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPPEDCOLETA". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "TPIMPOSLIVRO"  */
    if exists (select 1
               from   TPIMPOSLIVRO t2, deleted t1
               where  t2.CDIMPOSTOLIVRO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TPIMPOSLIVRO". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "GRUPOFISCIMPFIL"  */
    if exists (select 1
               from   GRUPOFISCIMPFIL t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GRUPOFISCIMPFIL". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Cannot delete parent "IMPOSTO" if children still exist in "CODSITUTRIBNFE"  */
    if exists (select 1
               from   CODSITUTRIBNFE t2, deleted t1
               where  t2.CDIMPOSTO = t1.CDIMPOSTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODSITUTRIBNFE". Cannot delete parent "IMPOSTO".'
          goto error
       end
    
    /*  Delete all children in "BASECALC"  */
    delete BASECALC
    from   BASECALC t2, deleted t1
    where  t2.CDIMPOSTO = t1.CDIMPOSTO
    
    /*  Delete all children in "CCTBIMPFORN"  */
    delete CCTBIMPFORN
    from   CCTBIMPFORN t2, deleted t1
    where  t2.CDIMPOSTO = t1.CDIMPOSTO
    
    /*  Delete all children in "DIFEALIQ"  */
    delete DIFEALIQ
    from   DIFEALIQ t2, deleted t1
    where  t2.CDIMPOSTO = t1.CDIMPOSTO
    
    /*  Delete all children in "IMPOSEMPRE"  */
    delete IMPOSEMPRE
    from   IMPOSEMPRE t2, deleted t1
    where  t2.CDIMPOSTO = t1.CDIMPOSTO

    return

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

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

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

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

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "INDICE" if children still exist in "CIAFILI"  */
    if exists (select 1
               from   CIAFILI t2, deleted t1
               where  t2.CDTPINDICE = t1.CDTPINDICE
                and   t2.IDPERVALOR = t1.IDPERVALOR
                and   t2.CDINDICE = t1.CDINDICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CIAFILI". Cannot delete parent "INDICE".'
          goto error
       end
    
    /*  Cannot delete parent "INDICE" if children still exist in "CLIENTE"  */
    if exists (select 1
               from   CLIENTE t2, deleted t1
               where  t2.CDTPINDICE = t1.CDTPINDICE
                and   t2.IDPERVALOR = t1.IDPERVALOR
                and   t2.CDINDICE = t1.CDINDICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENTE". Cannot delete parent "INDICE".'
          goto error
       end
    
    /*  Cannot delete parent "INDICE" if children still exist in "ORCAMENT"  */
    if exists (select 1
               from   ORCAMENT t2, deleted t1
               where  t2.CDTPINDICE = t1.CDTPINDICE
                and   t2.IDPERVALOR = t1.IDPERVALOR
                and   t2.CDINDICE = t1.CDINDICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAMENT". Cannot delete parent "INDICE".'
          goto error
       end
    
    /*  Cannot delete parent "INDICE" if children still exist in "PARASUPRI"  */
    if exists (select 1
               from   PARASUPRI t2, deleted t1
               where  t2.CDTPINDICE = t1.CDTPINDICE
                and   t2.IDPERVALOR = t1.IDPERVALOR
                and   t2.CDINDICE = t1.CDINDICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARASUPRI". Cannot delete parent "INDICE".'
          goto error
       end
    
    /*  Cannot delete parent "INDICE" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDTPINDICE = t1.CDTPINDICE
                and   t2.IDPERVALOR = t1.IDPERVALOR
                and   t2.CDINDICEMF = t1.CDINDICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "INDICE".'
          goto error
       end
    
    /*  Cannot delete parent "INDICE" if children still exist in "PARAMINTEGRA"  */
    if exists (select 1
               from   PARAMINTEGRA t2, deleted t1
               where  t2.CDTPINDICE = t1.CDTPINDICE
                and   t2.IDPERVALOR = t1.IDPERVALOR
                and   t2.CDINDICE = t1.CDINDICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMINTEGRA". Cannot delete parent "INDICE".'
          goto error
       end
    
    /*  Cannot delete parent "INDICE" if children still exist in "TIPORECE"  */
    if exists (select 1
               from   TIPORECE t2, deleted t1
               where  t2.CDTPINDICE = t1.CDTPINDICE
                and   t2.IDPERVALOR = t1.IDPERVALOR
                and   t2.CDINDICE = t1.CDINDICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPORECE". Cannot delete parent "INDICE".'
          goto error
       end
    
    /*  Cannot delete parent "INDICE" if children still exist in "MOEDUTCONV"  */
    if exists (select 1
               from   MOEDUTCONV t2, deleted t1
               where  t2.CDTPINDICE = t1.CDTPINDICE
                and   t2.IDPERVALOR = t1.IDPERVALOR
                and   t2.CDINDICE = t1.CDINDICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOEDUTCONV". Cannot delete parent "INDICE".'
          goto error
       end
    
    /*  Cannot delete parent "INDICE" if children still exist in "PARFILIALLOJ"  */
    if exists (select 1
               from   PARFILIALLOJ t2, deleted t1
               where  t2.CDTPINDICE = t1.CDTPINDICE
                and   t2.IDPERVALOR = t1.IDPERVALOR
                and   t2.CDINDICE = t1.CDINDICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALLOJ". Cannot delete parent "INDICE".'
          goto error
       end
    
    /*  Cannot delete parent "INDICE" if children still exist in "PARAMGERAL"  */
    if exists (select 1
               from   PARAMGERAL t2, deleted t1
               where  t2.CDTPINDICE2 = t1.CDTPINDICE
                and   t2.IDPERVALOR2 = t1.IDPERVALOR
                and   t2.CDINDICEMF2 = t1.CDINDICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMGERAL". Cannot delete parent "INDICE".'
          goto error
       end
    
    /*  Cannot delete parent "INDICE" if children still exist in "PRODUTO"  */
    if exists (select 1
               from   PRODUTO t2, deleted t1
               where  t2.CDTPINDICE = t1.CDTPINDICE
                and   t2.IDPERVALOR = t1.IDPERVALOR
                and   t2.CDINDICE = t1.CDINDICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTO". Cannot delete parent "INDICE".'
          goto error
       end
    
    /*  Cannot delete parent "INDICE" if children still exist in "INVOICE"  */
    if exists (select 1
               from   INVOICE t2, deleted t1
               where  t2.CDTPINDICE = t1.CDTPINDICE
                and   t2.IDPERVALOR = t1.IDPERVALOR
                and   t2.CDINDICE = t1.CDINDICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVOICE". Cannot delete parent "INDICE".'
          goto error
       end
    
    /*  Cannot delete parent "INDICE" if children still exist in "PROPOSTAFAS"  */
    if exists (select 1
               from   PROPOSTAFAS t2, deleted t1
               where  t2.CDTPINDICE1 = t1.CDTPINDICE
                and   t2.IDPERVALOR1 = t1.IDPERVALOR
                and   t2.CDINDICE1 = t1.CDINDICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPOSTAFAS". Cannot delete parent "INDICE".'
          goto error
       end
    
    /*  Cannot delete parent "INDICE" if children still exist in "PROPOSTAFAS"  */
    if exists (select 1
               from   PROPOSTAFAS t2, deleted t1
               where  t2.CDTPINDICE2 = t1.CDTPINDICE
                and   t2.IDPERVALOR2 = t1.IDPERVALOR
                and   t2.CDINDICE2 = t1.CDINDICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPOSTAFAS". Cannot delete parent "INDICE".'
          goto error
       end
    
    /*  Delete all children in "MOVINDICE"  */
    delete MOVINDICE
    from   MOVINDICE t2, deleted t1
    where  t2.CDTPINDICE = t1.CDTPINDICE
     and   t2.IDPERVALOR = t1.IDPERVALOR
     and   t2.CDINDICE = t1.CDINDICE

    return

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

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

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

    
    /*  Delete all children in "ITINQUERPACI"  */
    delete ITINQUERPACI
    from   ITINQUERPACI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTINIINQU = t1.DTINIINQU
     and   t2.DTFININQU = t1.DTFININQU

    return

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

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

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

    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "FILIAL"  */
    if exists (select 1
               from   FILIAL t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIAL". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "INCIDENCIA"  */
    if exists (select 1
               from   INCIDENCIA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INCIDENCIA". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "ALIQUOTA"  */
    if exists (select 1
               from   ALIQUOTA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ALIQUOTA". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "SERIENF"  */
    if exists (select 1
               from   SERIENF t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERIENF". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "CODFISCOPER"  */
    if exists (select 1
               from   CODFISCOPER t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODFISCOPER". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "CODSITUTRIB"  */
    if exists (select 1
               from   CODSITUTRIB t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODSITUTRIB". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "MAPAAPURAC"  */
    if exists (select 1
               from   MAPAAPURAC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPAAPURAC". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "INVENTFISC"  */
    if exists (select 1
               from   INVENTFISC t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVENTFISC". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "EMPRCNTRFIL"  */
    if exists (select 1
               from   EMPRCNTRFIL t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMPRCNTRFIL". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "EMPRESA"  */
    if exists (select 1
               from   EMPRESA t2, deleted t1
               where  t2.CDEMPRIEST = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMPRESA". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "SERVCONTRAT"  */
    if exists (select 1
               from   SERVCONTRAT t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCONTRAT". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "CODTRIBSAP"  */
    if exists (select 1
               from   CODTRIBSAP t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODTRIBSAP". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "VRPRODBCIMPO"  */
    if exists (select 1
               from   VRPRODBCIMPO t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VRPRODBCIMPO". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Cannot delete parent "INSCRESTAD" if children still exist in "PARAMSERIE"  */
    if exists (select 1
               from   PARAMSERIE t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMSERIE". Cannot delete parent "INSCRESTAD".'
          goto error
       end
    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDINSCESTA = t1.CDINSCESTA
    
    /*  Delete all children in "PARAMDIEFCE"  */
    delete PARAMDIEFCE
    from   PARAMDIEFCE t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDINSCESTA = t1.CDINSCESTA

    return

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

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

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

    
    /*  Cannot delete parent "INSPECAO" if children still exist in "INSPECPROD"  */
    if exists (select 1
               from   INSPECPROD t2, deleted t1
               where  t2.CDINSPECAO = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPECPROD". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "INSPTIPAUDIT"  */
    if exists (select 1
               from   INSPTIPAUDIT t2, deleted t1
               where  t2.CDINSPECAO = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPTIPAUDIT". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "INSPECAUDIT"  */
    if exists (select 1
               from   INSPECAUDIT t2, deleted t1
               where  t2.CDINSPECAO = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPECAUDIT". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "ITEMINSAUDIT"  */
    if exists (select 1
               from   ITEMINSAUDIT t2, deleted t1
               where  t2.CDINSPECAO = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMINSAUDIT". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "CRITAVAPARCI"  */
    if exists (select 1
               from   CRITAVAPARCI t2, deleted t1
               where  t2.CDINSPECAO = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CRITAVAPARCI". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "INSPAVAFINAL"  */
    if exists (select 1
               from   INSPAVAFINAL t2, deleted t1
               where  t2.CDINSPECAO = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPAVAFINAL". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "INSPPROC"  */
    if exists (select 1
               from   INSPPROC t2, deleted t1
               where  t2.CDINSPECAO = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPPROC". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "INSPETAPA"  */
    if exists (select 1
               from   INSPETAPA t2, deleted t1
               where  t2.CDINSPECAO = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPETAPA". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "INSPAGENTARE"  */
    if exists (select 1
               from   INSPAGENTARE t2, deleted t1
               where  t2.CDINSPECAO = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPAGENTARE". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "INSPRECRPADR"  */
    if exists (select 1
               from   INSPRECRPADR t2, deleted t1
               where  t2.CDINSPECAO = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPRECRPADR". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "INSPRECRDEPT"  */
    if exists (select 1
               from   INSPRECRDEPT t2, deleted t1
               where  t2.CDINSPECAO = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPRECRDEPT". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "INSPRECRCARG"  */
    if exists (select 1
               from   INSPRECRCARG t2, deleted t1
               where  t2.CDINSPECAO = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPRECRCARG". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "RECURETAPAOP"  */
    if exists (select 1
               from   RECURETAPAOP t2, deleted t1
               where  t2.CDINSPECAO = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECURETAPAOP". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "COMPOSINSPEC"  */
    if exists (select 1
               from   COMPOSINSPEC t2, deleted t1
               where  t2.CDINSPECAO = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPOSINSPEC". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "INSPPROCESSO"  */
    if exists (select 1
               from   INSPPROCESSO t2, deleted t1
               where  t2.CDINSPGRUP = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPPROCESSO". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Cannot delete parent "INSPECAO" if children still exist in "INSPLANATIV"  */
    if exists (select 1
               from   INSPLANATIV t2, deleted t1
               where  t2.CDINSPPROD = t1.CDINSPECAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPLANATIV". Cannot delete parent "INSPECAO".'
          goto error
       end
    
    /*  Delete all children in "RESPOSINSPEC"  */
    delete RESPOSINSPEC
    from   RESPOSINSPEC t2, deleted t1
    where  t2.CDINSPECAO = t1.CDINSPECAO
    
    /*  Delete all children in "DESCINSPECAO"  */
    delete DESCINSPECAO
    from   DESCINSPECAO t2, deleted t1
    where  t2.CDINSPECAO = t1.CDINSPECAO
    
    /*  Delete all children in "ACAOINSPECAO"  */
    delete ACAOINSPECAO
    from   ACAOINSPECAO t2, deleted t1
    where  t2.CDINSPECAO = t1.CDINSPECAO
    
    /*  Delete all children in "VISTOINSPEC"  */
    delete VISTOINSPEC
    from   VISTOINSPEC t2, deleted t1
    where  t2.CDINSPECAO = t1.CDINSPECAO

    return

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

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

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

    
    /*  Delete all children in "ITEMINSAUDIT"  */
    delete ITEMINSAUDIT
    from   ITEMINSAUDIT t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRAUDITORIA = t1.NRAUDITORIA
     and   t2.NRINSPECAUDI = t1.NRINSPECAUDI
    
    /*  Delete all children in "VISTINSAUDIT"  */
    delete VISTINSAUDIT
    from   VISTINSAUDIT t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRAUDITORIA = t1.NRAUDITORIA
     and   t2.NRINSPECAUDI = t1.NRINSPECAUDI
    
    /*  Delete all children in "PESSAUDIT"  */
    delete PESSAUDIT
    from   PESSAUDIT t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRAUDITORIA = t1.NRAUDITORIA
     and   t2.NRINSPECAUDI = t1.NRINSPECAUDI
    
    /*  Delete all children in "MOVOCORRPROD"  */
    delete MOVOCORRPROD
    from   MOVOCORRPROD t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRAUDITORIA = t1.NRAUDITORIA
     and   t2.NRINSPECAUDI = t1.NRINSPECAUDI
    
    /*  Delete all children in "RECURETAPAOP"  */
    delete RECURETAPAOP
    from   RECURETAPAOP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRAUDITORIA = t1.NRAUDITORIA
     and   t2.NRINSPECAUDI = t1.NRINSPECAUDI
    
    /*  Delete all children in "DESCINSAUDIT"  */
    delete DESCINSAUDIT
    from   DESCINSAUDIT t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRAUDITORIA = t1.NRAUDITORIA
     and   t2.NRINSPECAUDI = t1.NRINSPECAUDI

    return

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

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

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

    
    /*  Delete all children in "DESCINSETAPA"  */
    delete DESCINSETAPA
    from   DESCINSETAPA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDPROCFABRIC = t1.CDPROCFABRIC
     and   t2.CDETAPAPROD = t1.CDETAPAPROD
     and   t2.CDINSPECAO = t1.CDINSPECAO
     and   t2.NRINSPETAPA = t1.NRINSPETAPA
    
    /*  Delete all children in "RECURETAPA"  */
    delete RECURETAPA
    from   RECURETAPA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDPROCFABRIC = t1.CDPROCFABRIC
     and   t2.CDETAPAPROD = t1.CDETAPAPROD
     and   t2.CDINSPECAO = t1.CDINSPECAO
     and   t2.NRINSPETAPA = t1.NRINSPETAPA

    return

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

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

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

    
    /*  Cannot delete parent "INTECLIE" if children still exist in "SERVPREST"  */
    if exists (select 1
               from   SERVPREST t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDINTECLIE = t1.CDINTECLIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVPREST". Cannot delete parent "INTECLIE".'
          goto error
       end
    
    /*  Cannot delete parent "INTECLIE" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDINTECLIE = t1.CDINTECLIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "INTECLIE".'
          goto error
       end
    
    /*  Cannot delete parent "INTECLIE" if children still exist in "EVOLCONT"  */
    if exists (select 1
               from   EVOLCONT t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDINTECLIE = t1.CDINTECLIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EVOLCONT". Cannot delete parent "INTECLIE".'
          goto error
       end
    
    /*  Cannot delete parent "INTECLIE" if children still exist in "MALADIRETA"  */
    if exists (select 1
               from   MALADIRETA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDINTECLIE = t1.CDINTECLIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MALADIRETA". Cannot delete parent "INTECLIE".'
          goto error
       end
    
    /*  Cannot delete parent "INTECLIE" if children still exist in "PLANATIVTARE"  */
    if exists (select 1
               from   PLANATIVTARE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDINTECLIE = t1.CDINTECLIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PLANATIVTARE". Cannot delete parent "INTECLIE".'
          goto error
       end
    
    /*  Delete all children in "CORINTECLI"  */
    delete CORINTECLI
    from   CORINTECLI t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDINTECLIE = t1.CDINTECLIE
    
    /*  Delete all children in "PROCINTECLI"  */
    delete PROCINTECLI
    from   PROCINTECLI t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDINTECLIE = t1.CDINTECLIE

    return

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

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

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

    
    /*  Cannot delete parent "INTEFORN" if children still exist in "EVOLCONT"  */
    if exists (select 1
               from   EVOLCONT t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED
                and   t2.CDINTERLOC = t1.CDINTERLOC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EVOLCONT". Cannot delete parent "INTEFORN".'
          goto error
       end
    
    /*  Cannot delete parent "INTEFORN" if children still exist in "MALADIRETA"  */
    if exists (select 1
               from   MALADIRETA t2, deleted t1
               where  t2.CDFORNECED = t1.CDFORNECED
                and   t2.CDINTERLOC = t1.CDINTERLOC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MALADIRETA". Cannot delete parent "INTEFORN".'
          goto error
       end
    
    /*  Delete all children in "CORINTEFOR"  */
    delete CORINTEFOR
    from   CORINTEFOR t2, deleted t1
    where  t2.CDFORNECED = t1.CDFORNECED
     and   t2.CDINTERLOC = t1.CDINTERLOC
    
    /*  Delete all children in "PROCINTEFOR"  */
    delete PROCINTEFOR
    from   PROCINTEFOR t2, deleted t1
    where  t2.CDFORNECED = t1.CDFORNECED
     and   t2.CDINTERLOC = t1.CDINTERLOC

    return

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

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

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

    
    /*  Cannot delete parent "INTEPROSP" if children still exist in "EVOLCONT"  */
    if exists (select 1
               from   EVOLCONT t2, deleted t1
               where  t2.CDPROSPECT = t1.CDPROSPECT
                and   t2.CDINTEPROS = t1.CDINTEPROS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EVOLCONT". Cannot delete parent "INTEPROSP".'
          goto error
       end
    
    /*  Cannot delete parent "INTEPROSP" if children still exist in "MALADIRETA"  */
    if exists (select 1
               from   MALADIRETA t2, deleted t1
               where  t2.CDPROSPECT = t1.CDPROSPECT
                and   t2.CDINTEPROS = t1.CDINTEPROS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MALADIRETA". Cannot delete parent "INTEPROSP".'
          goto error
       end
    
    /*  Delete all children in "CORINTEPRO"  */
    delete CORINTEPRO
    from   CORINTEPRO t2, deleted t1
    where  t2.CDPROSPECT = t1.CDPROSPECT
     and   t2.CDINTEPROS = t1.CDINTEPROS
    
    /*  Delete all children in "PROCINTEPRO"  */
    delete PROCINTEPRO
    from   PROCINTEPRO t2, deleted t1
    where  t2.CDPROSPECT = t1.CDPROSPECT
     and   t2.CDINTEPROS = t1.CDINTEPROS

    return

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

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

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

    
    /*  Cannot delete parent "INTERNAC" if children still exist in "SERVINTE"  */
    if exists (select 1
               from   SERVINTE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE
                and   t2.DTINTERNA = t1.DTINTERNA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVINTE". Cannot delete parent "INTERNAC".'
          goto error
       end
    
    /*  Cannot delete parent "INTERNAC" if children still exist in "INTERLEITO"  */
    if exists (select 1
               from   INTERLEITO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE
                and   t2.DTINTERNA = t1.DTINTERNA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INTERLEITO". Cannot delete parent "INTERNAC".'
          goto error
       end
    
    /*  Delete all children in "CONSPACI"  */
    delete CONSPACI
    from   CONSPACI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTINTERNA = t1.DTINTERNA
    
    /*  Delete all children in "DIAGINTER"  */
    delete DIAGINTER
    from   DIAGINTER t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTINTERNA = t1.DTINTERNA

    return

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

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

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

    
    /*  Cannot delete parent "INVOICE" if children still exist in "APURFATUFAS"  */
    if exists (select 1
               from   APURFATUFAS t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRINVOICE = t1.NRINVOICE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURFATUFAS". Cannot delete parent "INVOICE".'
          goto error
       end
    
    /*  Delete all children in "ITEMINVOICE"  */
    delete ITEMINVOICE
    from   ITEMINVOICE t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRINVOICE = t1.NRINVOICE
    
    /*  Delete all children in "DADOSADICINV"  */
    delete DADOSADICINV
    from   DADOSADICINV t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRINVOICE = t1.NRINVOICE

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "ITEMAUTF" if children still exist in "ITEMPRODUTO"  */
    if exists (select 1
               from   ITEMPRODUTO t2, deleted t1
               where  t2.CDFILDEST = t1.CDFILDEST
                and   t2.CDFILIAL = t1.CDFILLANC
                and   t2.NRAUTOFORN = t1.NRAUTOFORN
                and   t2.CDMARCA = t1.CDMARCA
                and   t2.CDPRODUTO = t1.CDPRODUTO
                and   t2.DTPROGENTAF = t1.DTPROGENTAF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPRODUTO". Cannot delete parent "ITEMAUTF".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMAUTF" if children still exist in "NFRELACAF"  */
    if exists (select 1
               from   NFRELACAF t2, deleted t1
               where  t2.CDFILDEST = t1.CDFILDEST
                and   t2.CDFILLANC = t1.CDFILLANC
                and   t2.NRAUTOFORN = t1.NRAUTOFORN
                and   t2.CDMARCA = t1.CDMARCA
                and   t2.CDPRODUTO = t1.CDPRODUTO
                and   t2.DTPROGENTAF = t1.DTPROGENTAF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFRELACAF". Cannot delete parent "ITEMAUTF".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMAUTF" if children still exist in "NFRELACAFSAI"  */
    if exists (select 1
               from   NFRELACAFSAI t2, deleted t1
               where  t2.CDFILDEST = t1.CDFILDEST
                and   t2.CDFILLANC = t1.CDFILLANC
                and   t2.NRAUTOFORN = t1.NRAUTOFORN
                and   t2.CDMARCA = t1.CDMARCA
                and   t2.CDPRODUTO = t1.CDPRODUTO
                and   t2.DTPROGENTAF = t1.DTPROGENTAF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFRELACAFSAI". Cannot delete parent "ITEMAUTF".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMAUTF" if children still exist in "ITEMSAIDOL"  */
    if exists (select 1
               from   ITEMSAIDOL t2, deleted t1
               where  t2.CDFILDEST = t1.CDFILDEST
                and   t2.CDFILLANC = t1.CDFILLANC
                and   t2.NRAUTOFORN = t1.NRAUTOFORN
                and   t2.CDMARCA = t1.CDMARCA
                and   t2.CDPRODUTO = t1.CDPRODUTO
                and   t2.DTPROGENTAF = t1.DTPROGENTAF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMSAIDOL". Cannot delete parent "ITEMAUTF".'
          goto error
       end
    
    /*  Delete all children in "IMPITMAF"  */
    delete IMPITMAF
    from   IMPITMAF t2, deleted t1
    where  t2.CDFILDEST = t1.CDFILDEST
     and   t2.CDFILLANC = t1.CDFILLANC
     and   t2.NRAUTOFORN = t1.NRAUTOFORN
     and   t2.CDMARCA = t1.CDMARCA
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.DTPROGENTAF = t1.DTPROGENTAF

    return

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

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

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

    
    /*  Delete all children in "PZOPGTITMCOL"  */
    delete PZOPGTITMCOL
    from   PZOPGTITMCOL t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRCOLEPRECO = t1.NRCOLEPRECO
     and   t2.CDMARCA = t1.CDMARCA
     and   t2.CDREGIAO = t1.CDREGIAO
     and   t2.CDFORNECED = t1.CDFORNECED
     and   t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "ITMPREVLIST"  */
    delete ITMPREVLIST
    from   ITMPREVLIST t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRCOLEPRECO = t1.NRCOLEPRECO
     and   t2.CDMARCA = t1.CDMARCA
     and   t2.CDREGIAO = t1.CDREGIAO
     and   t2.CDFORNECED = t1.CDFORNECED
     and   t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "IMPCOLET"  */
    delete IMPCOLET
    from   IMPCOLET t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRCOLEPRECO = t1.NRCOLEPRECO
     and   t2.CDMARCA = t1.CDMARCA
     and   t2.CDREGIAO = t1.CDREGIAO
     and   t2.CDFORNECED = t1.CDFORNECED
     and   t2.CDPRODUTO = t1.CDPRODUTO

    return

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

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

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

    
    /*  Delete all children in "RELACONSOLOL"  */
    delete RELACONSOLOL
    from   RELACONSOLOL t2, deleted t1
    where  t2.NRSEQUCONSOL = t1.NRSEQUCONSOL
     and   t2.CDFILSOLCONS = t1.CDFILSOLI
     and   t2.CDFILLANCONS = t1.CDFILLAN
     and   t2.CDPRODUTOCONS = t1.CDPRODUTO
     and   t2.DTENTRCONSOL = t1.DTENTRCONSOL

    return

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

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

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

    
    /*  Delete all children in "ITEMAPUCTB"  */
    delete ITEMAPUCTB
    from   ITEMAPUCTB t2, deleted t1
    where  t2.CDTIPODEMO = t1.CDTIPODEMO
     and   t2.CDITEMSTRU = t1.CDITEMSTRU

    return

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

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

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

    
    /*  Delete all children in "ITEMDWOPER"  */
    delete ITEMDWOPER
    from   ITEMDWOPER t2, deleted t1
    where  t2.CDTABELADW = t1.CDTABELADW
     and   t2.CDITEMDW = t1.CDITEMDW
    
    /*  Delete all children in "ITEMDWGRUPOP"  */
    delete ITEMDWGRUPOP
    from   ITEMDWGRUPOP t2, deleted t1
    where  t2.CDTABELADW = t1.CDTABELADW
     and   t2.CDITEMDW = t1.CDITEMDW

    return

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

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

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

    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
     and   t2.CDENCARGO = t1.CDENCARGO

    return

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

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

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

    
    /*  Delete all children in "RESULTAUDIT"  */
    delete RESULTAUDIT
    from   RESULTAUDIT t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRAUDITORIA = t1.NRAUDITORIA
     and   t2.NRINSPECAUDI = t1.NRINSPECAUDI
     and   t2.CDINSPECAO = t1.CDINSPECAO

    return

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

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

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

    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
     and   t2.NRSEQUITNOTA = t1.NRSEQUITNOTA

    return

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

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

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

    
    /*  Cannot delete parent "ITEMORCAEVEN" if children still exist in "HISTORCAEVEN"  */
    if exists (select 1
               from   HISTORCAEVEN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
                and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
                and   t2.NRITEMORCEVE = t1.NRITEMORCEVE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HISTORCAEVEN". Cannot delete parent "ITEMORCAEVEN".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMORCAEVEN" if children still exist in "ITEMPEDORE"  */
    if exists (select 1
               from   ITEMPEDORE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
                and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
                and   t2.NRITEMORCEVE = t1.NRITEMORCEVE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPEDORE". Cannot delete parent "ITEMORCAEVEN".'
          goto error
       end
    
    /*  Delete all children in "AGENSALITORC"  */
    delete AGENSALITORC
    from   AGENSALITORC t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
     and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
     and   t2.NRITEMORCEVE = t1.NRITEMORCEVE

    return

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

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

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

    
    /*  Delete all children in "IMPPEDCOLETA"  */
    delete IMPPEDCOLETA
    from   IMPPEDCOLETA t2, deleted t1
    where  t2.CDREGIAO = t1.CDREGIAO
     and   t2.CDFORNECED = t1.CDFORNECED
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRPEDCOLETA = t1.NRPEDCOLETA
     and   t2.CDMARCA = t1.CDMARCA
     and   t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "MOVOPITEMPEDCOL"  */
    delete MOVOPITEMPEDCOL
    from   MOVOPITEMPEDCOL t2, deleted t1
    where  t2.CDREGIAO = t1.CDREGIAO
     and   t2.CDFORNECED = t1.CDFORNECED
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRPEDCOLETA = t1.NRPEDCOLETA
     and   t2.CDMARCA = t1.CDMARCA
     and   t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "LOGNGERAPEDCOL"  */
    delete LOGNGERAPEDCOL
    from   LOGNGERAPEDCOL t2, deleted t1
    where  t2.CDREGIAO = t1.CDREGIAO
     and   t2.CDFORNECED = t1.CDFORNECED
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRPEDCOLETA = t1.NRPEDCOLETA
     and   t2.CDMARCA = t1.CDMARCA
     and   t2.CDPRODUTO = t1.CDPRODUTO

    return

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

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

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

    
    /*  Cannot delete parent "ITEMPEDIDO" if children still exist in "ITEMPRODUTO"  */
    if exists (select 1
               from   ITEMPRODUTO t2, deleted t1
               where  t2.CDFILIALPED = t1.CDFILIAL
                and   t2.NRPEDIDO = t1.NRPEDIDO
                and   t2.NRITEMPED = t1.NRITEMPED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPRODUTO". Cannot delete parent "ITEMPEDIDO".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMPEDIDO" if children still exist in "PROITPROP"  */
    if exists (select 1
               from   PROITPROP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRPEDIDO = t1.NRPEDIDO
                and   t2.NRITEMPED = t1.NRITEMPED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROITPROP". Cannot delete parent "ITEMPEDIDO".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMPEDIDO" if children still exist in "ITLANCTOEST"  */
    if exists (select 1
               from   ITLANCTOEST t2, deleted t1
               where  t2.CDFILIALPED = t1.CDFILIAL
                and   t2.NRPEDIDO = t1.NRPEDIDO
                and   t2.NRITEMPED = t1.NRITEMPED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITLANCTOEST". Cannot delete parent "ITEMPEDIDO".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMPEDIDO" if children still exist in "INCONSPEDIDO"  */
    if exists (select 1
               from   INCONSPEDIDO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRPEDIDO = t1.NRPEDIDO
                and   t2.NRITEMPED = t1.NRITEMPED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INCONSPEDIDO". Cannot delete parent "ITEMPEDIDO".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMPEDIDO" if children still exist in "PEDIDOP"  */
    if exists (select 1
               from   PEDIDOP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRPEDIDO = t1.NRPEDIDO
                and   t2.NRITEMPED = t1.NRITEMPED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDOP". Cannot delete parent "ITEMPEDIDO".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMPEDIDO" if children still exist in "INSUPEDIDOP"  */
    if exists (select 1
               from   INSUPEDIDOP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRPEDIDO = t1.NRPEDIDO
                and   t2.NRITEMPED = t1.NRITEMPED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSUPEDIDOP". Cannot delete parent "ITEMPEDIDO".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMPEDIDO" if children still exist in "ITEMPEDORE"  */
    if exists (select 1
               from   ITEMPEDORE t2, deleted t1
               where  t2.CDFILIPEDORE = t1.CDFILIAL
                and   t2.NRPEDIDO = t1.NRPEDIDO
                and   t2.NRITEMPED = t1.NRITEMPED)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPEDORE". Cannot delete parent "ITEMPEDIDO".'
          goto error
       end
    
    /*  Delete all children in "HISITEMPED"  */
    delete HISITEMPED
    from   HISITEMPED t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRPEDIDO = t1.NRPEDIDO
     and   t2.NRITEMPED = t1.NRITEMPED
    
    /*  Delete all children in "VENDITEMPED"  */
    delete VENDITEMPED
    from   VENDITEMPED t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRPEDIDO = t1.NRPEDIDO
     and   t2.NRITEMPED = t1.NRITEMPED
    
    /*  Delete all children in "RESERPRODPED"  */
    delete RESERPRODPED
    from   RESERPRODPED t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRPEDIDO = t1.NRPEDIDO
     and   t2.NRITEMPED = t1.NRITEMPED
    
    /*  Delete all children in "COMPITEMPED"  */
    delete COMPITEMPED
    from   COMPITEMPED t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRPEDIDO = t1.NRPEDIDO
     and   t2.NRITEMPED = t1.NRITEMPED

    return

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

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

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

    
    /*  Cannot delete parent "ITEMPRECO" if children still exist in "ITEMPRECODIA"  */
    if exists (select 1
               from   ITEMPRECODIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDTABEPREC = t1.CDTABEPREC
                and   t2.DTINIVGPREC = t1.DTINIVGPREC
                and   t2.CDPRODUTO = t1.CDPRODUTO
                and   t2.CDPRPAITABPR = t1.CDPRPAITABPR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPRECODIA". Cannot delete parent "ITEMPRECO".'
          goto error
       end
    
    /*  Delete all children in "ITEMPRECOCLI"  */
    delete ITEMPRECOCLI
    from   ITEMPRECOCLI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDTABEPREC = t1.CDTABEPREC
     and   t2.DTINIVGPREC = t1.DTINIVGPREC
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDPRPAITABPR = t1.CDPRPAITABPR

    return

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

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

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

    
    /*  Delete all children in "ITEMPRECOCON"  */
    delete ITEMPRECOCON
    from   ITEMPRECOCON t2, deleted t1
    where  t2.CDTABEPREC = t1.CDTABEPREC
     and   t2.DTINIVGPREC = t1.DTINIVGPREC
     and   t2.CDPRPAITABPR = t1.CDPRPAITABPR
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDFILIAL = t1.CDFILIAL

    return

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

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

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

    
    /*  Cannot delete parent "ITEMPRODUTO" if children still exist in "SERVFATU"  */
    if exists (select 1
               from   SERVFATU t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF
                and   t2.NRSEQITPROD = t1.NRSEQITPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVFATU". Cannot delete parent "ITEMPRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMPRODUTO" if children still exist in "CTRAPUITPROD"  */
    if exists (select 1
               from   CTRAPUITPROD t2, deleted t1
               where  t2.CDFILIALNF = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF
                and   t2.NRSEQITPROD = t1.NRSEQITPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRAPUITPROD". Cannot delete parent "ITEMPRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMPRODUTO" if children still exist in "ITEMVENDA"  */
    if exists (select 1
               from   ITEMVENDA t2, deleted t1
               where  t2.CDFILIALNF = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF
                and   t2.NRSEQITPROD = t1.NRSEQITPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMVENDA". Cannot delete parent "ITEMPRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMPRODUTO" if children still exist in "AUDITORIA"  */
    if exists (select 1
               from   AUDITORIA t2, deleted t1
               where  t2.CDFILIALNF = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF
                and   t2.NRSEQITPROD = t1.NRSEQITPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUDITORIA". Cannot delete parent "ITEMPRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMPRODUTO" if children still exist in "ITVENDAVMN"  */
    if exists (select 1
               from   ITVENDAVMN t2, deleted t1
               where  t2.CDFILIALNFV = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF
                and   t2.NRSEQITPROD = t1.NRSEQITPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITVENDAVMN". Cannot delete parent "ITEMPRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMPRODUTO" if children still exist in "ITVENDAVMN"  */
    if exists (select 1
               from   ITVENDAVMN t2, deleted t1
               where  t2.CDFILIALNFV2 = t1.CDFILIAL
                and   t2.NRLANCTONF2 = t1.NRLANCTONF
                and   t2.NRSEQITPROD2 = t1.NRSEQITPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITVENDAVMN". Cannot delete parent "ITEMPRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMPRODUTO" if children still exist in "ITVENDAVMN"  */
    if exists (select 1
               from   ITVENDAVMN t2, deleted t1
               where  t2.CDFILIALNFV3 = t1.CDFILIAL
                and   t2.NRLANCTONF3 = t1.NRLANCTONF
                and   t2.NRSEQITPROD3 = t1.NRSEQITPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITVENDAVMN". Cannot delete parent "ITEMPRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMPRODUTO" if children still exist in "PARCLANCATIV"  */
    if exists (select 1
               from   PARCLANCATIV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF
                and   t2.NRSEQITPROD = t1.NRSEQITPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARCLANCATIV". Cannot delete parent "ITEMPRODUTO".'
          goto error
       end
    
    /*  Delete all children in "ITEMPROIMPOS"  */
    delete ITEMPROIMPOS
    from   ITEMPROIMPOS t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
     and   t2.NRSEQITPROD = t1.NRSEQITPROD
    
    /*  Delete all children in "RATEIONF"  */
    delete RATEIONF
    from   RATEIONF t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
     and   t2.NRSEQITPROD = t1.NRSEQITPROD
    
    /*  Delete all children in "IMPOSINFUSU"  */
    delete IMPOSINFUSU
    from   IMPOSINFUSU t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
     and   t2.NRSEQITPROD = t1.NRSEQITPROD
    
    /*  Delete all children in "ITPRODORIEXP"  */
    delete ITPRODORIEXP
    from   ITPRODORIEXP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
     and   t2.NRSEQITPROD = t1.NRSEQITPROD
    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
     and   t2.NRSEQITPROD = t1.NRSEQITPROD

    return

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

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

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

    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
     and   t2.NRSEQITPROD = t1.NRSEQITPROD
     and   t2.CDIMPOSTO = t1.CDIMPOSTO
     and   t2.NRITPROIMPOS = t1.NRITPROIMPOS

    return

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

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

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

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

    return

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

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

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

    
    /*  Delete all children in "ITEMRETIPRAT"  */
    delete ITEMRETIPRAT
    from   ITEMRETIPRAT t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRCARDAPIO = t1.NRCARDAPIO
     and   t2.NRRETIRADA = t1.NRRETIRADA
     and   t2.NRSEQUITRETI = t1.NRSEQUITRETI

    return

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

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

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

    
    /*  Delete all children in "LOTITRETIEST"  */
    delete LOTITRETIEST
    from   LOTITRETIEST t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRRETEST = t1.NRRETEST
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.NRSEQRETPROD = t1.NRSEQRETPROD

    return

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

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

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

    
    /*  Cannot delete parent "ITEMSOLI" if children still exist in "PRODCOMPRA"  */
    if exists (select 1
               from   PRODCOMPRA t2, deleted t1
               where  t2.CDFILSOLI = t1.CDFILSOLI
                and   t2.CDFILLAN = t1.CDFILLAN
                and   t2.NRSOLICMP = t1.NRSOLICMP
                and   t2.CDPRODUTO = t1.CDPRODUTO
                and   t2.DTPROGENTR = t1.DTPROGENTR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODCOMPRA". Cannot delete parent "ITEMSOLI".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMSOLI" if children still exist in "ITEMSOLIDEL"  */
    if exists (select 1
               from   ITEMSOLIDEL t2, deleted t1
               where  t2.CDFILSOLI = t1.CDFILSOLI
                and   t2.CDFILLAN = t1.CDFILLAN
                and   t2.NRSOLICMP = t1.NRSOLICMP
                and   t2.CDPRODUTO = t1.CDPRODUTO
                and   t2.DTPROGENTR = t1.DTPROGENTR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMSOLIDEL". Cannot delete parent "ITEMSOLI".'
          goto error
       end
    
    /*  Delete all children in "NFRELACSOLI"  */
    delete NFRELACSOLI
    from   NFRELACSOLI t2, deleted t1
    where  t2.CDFILSOLI = t1.CDFILSOLI
     and   t2.CDFILLAN = t1.CDFILLAN
     and   t2.NRSOLICMP = t1.NRSOLICMP
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.DTPROGENTR = t1.DTPROGENTR
    
    /*  Delete all children in "RELACONSOLOL"  */
    delete RELACONSOLOL
    from   RELACONSOLOL t2, deleted t1
    where  t2.CDFILSOLI = t1.CDFILSOLI
     and   t2.CDFILLAN = t1.CDFILLAN
     and   t2.NRSOLICMP = t1.NRSOLICMP
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.DTPROGENTR = t1.DTPROGENTR
    
    /*  Set parent code of "ITEMSOLI" to NULL in child "ITEMSOLI"  */
    update ITEMSOLI
     set   CDFILSOLCONS = NULL,
           CDFILLANCONS = NULL,
           NRSOLCMPCONS = NULL,
           CDPRODUTCONS = NULL,
           DTPROGENCONS = NULL
    from   ITEMSOLI t2, deleted t1
    where  t2.CDFILSOLCONS = t1.CDFILSOLI
     and   t2.CDFILLANCONS = t1.CDFILLAN
     and   t2.NRSOLCMPCONS = t1.NRSOLICMP
     and   t2.CDPRODUTCONS = t1.CDPRODUTO
     and   t2.DTPROGENCONS = t1.DTPROGENTR

    return

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

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

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

    
    /*  Cannot delete parent "ITEMTAREFA" if children still exist in "PLATVRELITTR"  */
    if exists (select 1
               from   PLATVRELITTR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA
                and   t2.NRSEQUITTARE = t1.NRSEQUITTARE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PLATVRELITTR". Cannot delete parent "ITEMTAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMTAREFA" if children still exist in "COMPALTITTAR"  */
    if exists (select 1
               from   COMPALTITTAR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA
                and   t2.NRSEQUITTARE = t1.NRSEQUITTARE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPALTITTAR". Cannot delete parent "ITEMTAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMTAREFA" if children still exist in "VALIDATAREFA"  */
    if exists (select 1
               from   VALIDATAREFA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA
                and   t2.NRSEQUITTARE = t1.NRSEQUITTARE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VALIDATAREFA". Cannot delete parent "ITEMTAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMTAREFA" if children still exist in "ITEMVALIDATARE"  */
    if exists (select 1
               from   ITEMVALIDATARE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA
                and   t2.NRSEQUITTARE = t1.NRSEQUITTARE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMVALIDATARE". Cannot delete parent "ITEMTAREFA".'
          goto error
       end
    
    /*  Delete all children in "DESCITEMTARE"  */
    delete DESCITEMTARE
    from   DESCITEMTARE t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRTAREFA = t1.NRTAREFA
     and   t2.NRSEQUITTARE = t1.NRSEQUITTARE

    return

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

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

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

    
    /*  Cannot delete parent "ITEMTRAB" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDITEMTRAB = t1.CDITEMTRAB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "ITEMTRAB".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMTRAB" if children still exist in "PLANATIVTARE"  */
    if exists (select 1
               from   PLANATIVTARE t2, deleted t1
               where  t2.CDITEMTRAB = t1.CDITEMTRAB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PLANATIVTARE". Cannot delete parent "ITEMTRAB".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMTRAB" if children still exist in "ITEMTRABPROJ"  */
    if exists (select 1
               from   ITEMTRABPROJ t2, deleted t1
               where  t2.CDITEMTRAB = t1.CDITEMTRAB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMTRABPROJ". Cannot delete parent "ITEMTRAB".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMTRAB" if children still exist in "ITEMTRABPROD"  */
    if exists (select 1
               from   ITEMTRABPROD t2, deleted t1
               where  t2.CDITEMTRAB = t1.CDITEMTRAB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMTRABPROD". Cannot delete parent "ITEMTRAB".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMTRAB" if children still exist in "SERVCONTRAT"  */
    if exists (select 1
               from   SERVCONTRAT t2, deleted t1
               where  t2.CDITEMTRAB = t1.CDITEMTRAB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCONTRAT". Cannot delete parent "ITEMTRAB".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMTRAB" if children still exist in "ITEMTAREFA"  */
    if exists (select 1
               from   ITEMTAREFA t2, deleted t1
               where  t2.CDITEMTRAB = t1.CDITEMTRAB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMTAREFA". Cannot delete parent "ITEMTRAB".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMTRAB" if children still exist in "COMPITTRAB"  */
    if exists (select 1
               from   COMPITTRAB t2, deleted t1
               where  t2.CDITEMTRAB = t1.CDITEMTRAB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPITTRAB". Cannot delete parent "ITEMTRAB".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMTRAB" if children still exist in "RASTREABTARE"  */
    if exists (select 1
               from   RASTREABTARE t2, deleted t1
               where  t2.CDITEMTRAB = t1.CDITEMTRAB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RASTREABTARE". Cannot delete parent "ITEMTRAB".'
          goto error
       end
    
    /*  Cannot delete parent "ITEMTRAB" if children still exist in "PARFILIALGEP"  */
    if exists (select 1
               from   PARFILIALGEP t2, deleted t1
               where  t2.CDITEMTRAB = t1.CDITEMTRAB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALGEP". Cannot delete parent "ITEMTRAB".'
          goto error
       end
    
    /*  Delete all children in "DESCITEMTRAB"  */
    delete DESCITEMTRAB
    from   DESCITEMTRAB t2, deleted t1
    where  t2.CDITEMTRAB = t1.CDITEMTRAB

    return

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

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

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

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

    return

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

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

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

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

    return

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

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

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

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

    return

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

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

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

    
    /*  Delete all children in "ITRELACITROP"  */
    delete ITRELACITROP
    from   ITRELACITROP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRROPFILIAL = t1.NRROPFILIAL
     and   t2.NRLANCROP = t1.NRLANCROP
     and   t2.NRITLANCROP = t1.NRITLANCROP

    return

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

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

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

    
    /*  Cannot delete parent "ITMAOOBRAORCA" if children still exist in "TURMAITMOBRA"  */
    if exists (select 1
               from   TURMAITMOBRA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
                and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
                and   t2.NRTURNO = t1.NRTURNO
                and   t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TURMAITMOBRA". Cannot delete parent "ITMAOOBRAORCA".'
          goto error
       end
    
    /*  Delete all children in "MOBRAORCFORN"  */
    delete MOBRAORCFORN
    from   MOBRAORCFORN t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
     and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
     and   t2.NRTURNO = t1.NRTURNO
     and   t2.CDPRODUTO = t1.CDPRODUTO

    return

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

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

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

    
    /*  Delete all children in "PROITPROP"  */
    delete PROITPROP
    from   PROITPROP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRPROPOSTA = t1.NRPROPOSTA
     and   t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "PRECCONCPROP"  */
    delete PRECCONCPROP
    from   PRECCONCPROP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRPROPOSTA = t1.NRPROPOSTA
     and   t2.CDPRODUTO = t1.CDPRODUTO

    return

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

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

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

    
    /*  Delete all children in "MENSREQUESTO"  */
    delete MENSREQUESTO
    from   MENSREQUESTO t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRREQUESTO = t1.NRREQUESTO
     and   t2.CDPRODUTO = t1.CDPRODUTO

    return

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

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

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

    
    /*  Cannot delete parent "LANCCONT" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.NRLANCCONT = t1.NRLANCCONT
                and   t2.NRSEQLANC = t1.NRSEQLANC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "LANCCONT".'
          goto error
       end
    
    /*  Cannot delete parent "LANCCONT" if children still exist in "AJCTPAGAR"  */
    if exists (select 1
               from   AJCTPAGAR t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.NRLANCCONT = t1.NRLANCCONT
                and   t2.NRSEQLANC = t1.NRSEQLANC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJCTPAGAR". Cannot delete parent "LANCCONT".'
          goto error
       end
    
    /*  Delete all children in "LANCCCUSTO"  */
    delete LANCCCUSTO
    from   LANCCCUSTO t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.NRLANCCONT = t1.NRLANCCONT
     and   t2.NRSEQLANC = t1.NRSEQLANC

    return

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

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

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

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

    return

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

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

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

    
    /*  Delete all children in "ITLANCROPFILIAL"  */
    delete ITLANCROPFILIAL
    from   ITLANCROPFILIAL t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRROPFILIAL = t1.NRROPFILIAL
     and   t2.NRLANCROP = t1.NRLANCROP

    return

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

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

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

    
    /*  Cannot delete parent "LANCTOESTOQ" if children still exist in "RETIRADA"  */
    if exists (select 1
               from   RETIRADA t2, deleted t1
               where  t2.CDFILANCESTQ = t1.CDFILIAL
                and   t2.NRLANCESTQ = t1.NRLANCESTQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RETIRADA". Cannot delete parent "LANCTOESTOQ".'
          goto error
       end
    
    /*  Cannot delete parent "LANCTOESTOQ" if children still exist in "RETIRADAPCP"  */
    if exists (select 1
               from   RETIRADAPCP t2, deleted t1
               where  t2.CDFILIALLPCP = t1.CDFILIAL
                and   t2.NRLANCESTQ = t1.NRLANCESTQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RETIRADAPCP". Cannot delete parent "LANCTOESTOQ".'
          goto error
       end
    
    /*  Cannot delete parent "LANCTOESTOQ" if children still exist in "RETIRADAESTO"  */
    if exists (select 1
               from   RETIRADAESTO t2, deleted t1
               where  t2.CDFILIALLAN = t1.CDFILIAL
                and   t2.NRLANCESTQ = t1.NRLANCESTQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RETIRADAESTO". Cannot delete parent "LANCTOESTOQ".'
          goto error
       end
    
    /*  Cannot delete parent "LANCTOESTOQ" if children still exist in "AJURELACRET"  */
    if exists (select 1
               from   AJURELACRET t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCESTQ = t1.NRLANCESTQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJURELACRET". Cannot delete parent "LANCTOESTOQ".'
          goto error
       end
    
    /*  Cannot delete parent "LANCTOESTOQ" if children still exist in "AJURELACRET"  */
    if exists (select 1
               from   AJURELACRET t2, deleted t1
               where  t2.CDFILIALRET = t1.CDFILIAL
                and   t2.NRLANCESTRET = t1.NRLANCESTQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJURELACRET". Cannot delete parent "LANCTOESTOQ".'
          goto error
       end
    
    /*  Cannot delete parent "LANCTOESTOQ" if children still exist in "CTRLMOVIESTQ"  */
    if exists (select 1
               from   CTRLMOVIESTQ t2, deleted t1
               where  t2.CDFILIALENTR = t1.CDFILIAL
                and   t2.NRLANESTENTR = t1.NRLANCESTQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLMOVIESTQ". Cannot delete parent "LANCTOESTOQ".'
          goto error
       end
    
    /*  Cannot delete parent "LANCTOESTOQ" if children still exist in "CTRLMOVIESTQ"  */
    if exists (select 1
               from   CTRLMOVIESTQ t2, deleted t1
               where  t2.CDFILIALSAID = t1.CDFILIAL
                and   t2.NRLANESTSAID = t1.NRLANCESTQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLMOVIESTQ". Cannot delete parent "LANCTOESTOQ".'
          goto error
       end
    
    /*  Cannot delete parent "LANCTOESTOQ" if children still exist in "NFABASTEROTA"  */
    if exists (select 1
               from   NFABASTEROTA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCESTQ = t1.NRLANCESTQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFABASTEROTA". Cannot delete parent "LANCTOESTOQ".'
          goto error
       end
    
    /*  Cannot delete parent "LANCTOESTOQ" if children still exist in "PREVPDVLESTO"  */
    if exists (select 1
               from   PREVPDVLESTO t2, deleted t1
               where  t2.CDFILANCESTQ = t1.CDFILIAL
                and   t2.NRLANCESTQ = t1.NRLANCESTQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVPDVLESTO". Cannot delete parent "LANCTOESTOQ".'
          goto error
       end
    
    /*  Cannot delete parent "LANCTOESTOQ" if children still exist in "COMPITVENDA"  */
    if exists (select 1
               from   COMPITVENDA t2, deleted t1
               where  t2.CDFILIALEST = t1.CDFILIAL
                and   t2.NRLANCESTQ = t1.NRLANCESTQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPITVENDA". Cannot delete parent "LANCTOESTOQ".'
          goto error
       end
    
    /*  Cannot delete parent "LANCTOESTOQ" if children still exist in "CUSTOCMPPROD"  */
    if exists (select 1
               from   CUSTOCMPPROD t2, deleted t1
               where  t2.CDFILLANCEST = t1.CDFILIAL
                and   t2.NRLANCESTQ = t1.NRLANCESTQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOCMPPROD". Cannot delete parent "LANCTOESTOQ".'
          goto error
       end
    
    /*  Cannot delete parent "LANCTOESTOQ" if children still exist in "ITEMVENDA"  */
    if exists (select 1
               from   ITEMVENDA t2, deleted t1
               where  t2.CDFILIALEST = t1.CDFILIAL
                and   t2.NRLANCESTQ = t1.NRLANCESTQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMVENDA". Cannot delete parent "LANCTOESTOQ".'
          goto error
       end
    
    /*  Cannot delete parent "LANCTOESTOQ" if children still exist in "REQUESTO"  */
    if exists (select 1
               from   REQUESTO t2, deleted t1
               where  t2.CDFILLANCREQC = t1.CDFILIAL
                and   t2.NRLANCREQC = t1.NRLANCESTQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUESTO". Cannot delete parent "LANCTOESTOQ".'
          goto error
       end
    
    /*  Delete all children in "ITLANCTOEST"  */
    delete ITLANCTOEST
    from   ITLANCTOEST t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCESTQ = t1.NRLANCESTQ
    
    /*  Delete all children in "PRODIMPOINVE"  */
    delete PRODIMPOINVE
    from   PRODIMPOINVE t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCESTQ = t1.NRLANCESTQ
    
    /*  Delete all children in "NFRELACRETIR"  */
    delete NFRELACRETIR
    from   NFRELACRETIR t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCESTQ = t1.NRLANCESTQ
    
    /*  Delete all children in "NFRELACRETIR"  */
    delete NFRELACRETIR
    from   NFRELACRETIR t2, deleted t1
    where  t2.CDFILIALRET = t1.CDFILIAL
     and   t2.NRLANCESTRET = t1.NRLANCESTQ

    return

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

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

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

    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "SERVPREST"  */
    if exists (select 1
               from   SERVPREST t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVPREST". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "CONTATO"  */
    if exists (select 1
               from   CONTATO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTATO". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "SERVCONTRAT"  */
    if exists (select 1
               from   SERVCONTRAT t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCONTRAT". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "SERVFATU"  */
    if exists (select 1
               from   SERVFATU t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVFATU". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "SISTLUSO"  */
    if exists (select 1
               from   SISTLUSO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SISTLUSO". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "BOLETRECEBER"  */
    if exists (select 1
               from   BOLETRECEBER t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BOLETRECEBER". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "MALADIRETA"  */
    if exists (select 1
               from   MALADIRETA t2, deleted t1
               where  t2.CDCLIELICE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MALADIRETA". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "CORRLINUSO"  */
    if exists (select 1
               from   CORRLINUSO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CORRLINUSO". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "TAREFA"  */
    if exists (select 1
               from   TAREFA t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAREFA". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "LINPLUSO"  */
    if exists (select 1
               from   LINPLUSO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LINPLUSO". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "CONTRATOLINP"  */
    if exists (select 1
               from   CONTRATOLINP t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLUSOFATLU = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTRATOLINP". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "CONTRATOLINP"  */
    if exists (select 1
               from   CONTRATOLINP t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLUSOFATMAN = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTRATOLINP". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "CAIXALUSO"  */
    if exists (select 1
               from   CAIXALUSO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CAIXALUSO". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "IMPRLUSO"  */
    if exists (select 1
               from   IMPRLUSO t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPRLUSO". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "APURFATUFAS"  */
    if exists (select 1
               from   APURFATUFAS t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURFATUFAS". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Cannot delete parent "LICENCAUSO" if children still exist in "MENSLINPCLIE"  */
    if exists (select 1
               from   MENSLINPCLIE t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MENSLINPCLIE". Cannot delete parent "LICENCAUSO".'
          goto error
       end
    
    /*  Delete all children in "LOGLIBEPERS"  */
    delete LOGLIBEPERS
    from   LOGLIBEPERS t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.NRLICENCAUSO = t1.NRLICENCAUSO

    return

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

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

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

    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "POSESTDIA"  */
    if exists (select 1
               from   POSESTDIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "POSESTDIA". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "ITLANCTOEST"  */
    if exists (select 1
               from   ITLANCTOEST t2, deleted t1
               where  t2.CDFILIMOVI = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITLANCTOEST". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "PARAMFILIAL"  */
    if exists (select 1
               from   PARAMFILIAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMFILIAL". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "MOVILOTE"  */
    if exists (select 1
               from   MOVILOTE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVILOTE". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "PRODLCESTOQ"  */
    if exists (select 1
               from   PRODLCESTOQ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODLCESTOQ". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "ITEMPRODUTO"  */
    if exists (select 1
               from   ITEMPRODUTO t2, deleted t1
               where  t2.CDFILIMOVI = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPRODUTO". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "PRODFILI"  */
    if exists (select 1
               from   PRODFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFILI". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "ITREPLICDOC"  */
    if exists (select 1
               from   ITREPLICDOC t2, deleted t1
               where  t2.CDFILIORIGRD = t1.CDFILIAL
                and   t2.CDALMOORIGRD = t1.CDALMOXARIFE
                and   t2.CDLESTORIGRD = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITREPLICDOC". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "ITREPLICDOC"  */
    if exists (select 1
               from   ITREPLICDOC t2, deleted t1
               where  t2.CDFILIDESTRD = t1.CDFILIAL
                and   t2.CDALMODESTRD = t1.CDALMOXARIFE
                and   t2.CDLESTDESTRD = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITREPLICDOC". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CAIXA". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROTATRANS". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROTESE". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLESTOQSAID = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUHOSP". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLESTOQENTR = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUHOSP". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOJA". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQUIPAPROD". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGEQUIPPRO". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "PRODFILI"  */
    if exists (select 1
               from   PRODFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXINSPR = t1.CDALMOXARIFE
                and   t2.CDLOCALINSPR = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFILI". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "PRODFILI"  */
    if exists (select 1
               from   PRODFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXPRACA = t1.CDALMOXARIFE
                and   t2.CDLOCALPRACA = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFILI". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "POSESTATU"  */
    if exists (select 1
               from   POSESTATU t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "POSESTATU". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "PRODFILI"  */
    if exists (select 1
               from   PRODFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXPRCLI = t1.CDALMOXARIFE
                and   t2.CDLOCALPRCLI = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFILI". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESERPRODPED". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVEROTA". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CURVAABC". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "LOTITRETIEST"  */
    if exists (select 1
               from   LOTITRETIEST t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOTITRETIEST". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLCESTENTR = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLMOVIESTQ". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLCESTSAID = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLMOVIESTQ". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLCESTSAID = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUESTO". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLCESTENTR = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUESTO". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALPERDA = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALVMN". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODIGOBARRA". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODACABADO". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVEROTALOTE". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" 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
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCTOESTOQ". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "PARAMCOMIS"  */
    if exists (select 1
               from   PARAMCOMIS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXPISTA = t1.CDALMOXARIFE
                and   t2.CDLOCESTPIST = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMCOMIS". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "ITEMENTREGA"  */
    if exists (select 1
               from   ITEMENTREGA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMENTREGA". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "CUSTOCMPPROD"  */
    if exists (select 1
               from   CUSTOCMPPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXARIFE = t1.CDALMOXARIFE
                and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOCMPPROD". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Cannot delete parent "LOCALESTOQUE" if children still exist in "PRODFILI"  */
    if exists (select 1
               from   PRODFILI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDALMOXQUAR = t1.CDALMOXARIFE
                and   t2.CDLOCALQUAR = t1.CDLOCALESTOQ)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFILI". Cannot delete parent "LOCALESTOQUE".'
          goto error
       end
    
    /*  Delete all children in "OPERLESTOQ"  */
    delete OPERLESTOQ
    from   OPERLESTOQ t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDALMOXARIFE = t1.CDALMOXARIFE
     and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ
    
    /*  Delete all children in "GROPERLESTOQ"  */
    delete GROPERLESTOQ
    from   GROPERLESTOQ t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDALMOXARIFE = t1.CDALMOXARIFE
     and   t2.CDLOCALESTOQ = t1.CDLOCALESTOQ

    return

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

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

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

    
    /*  Delete all children in "OCOAPURQFO"  */
    delete OCOAPURQFO
    from   OCOAPURQFO t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLOGAPURQFO = t1.NRLOGAPURQFO

    return

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

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

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

    
    /*  Cannot delete parent "MAPAAPURAC" if children still exist in "IMPOSTONF"  */
    if exists (select 1
               from   IMPOSTONF t2, deleted t1
               where  t2.NRSEQUMAPAPU = t1.NRSEQUMAPAPU)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSTONF". Cannot delete parent "MAPAAPURAC".'
          goto error
       end
    
    /*  Cannot delete parent "MAPAAPURAC" if children still exist in "IMPOSTONF"  */
    if exists (select 1
               from   IMPOSTONF t2, deleted t1
               where  t2.NRSEQMAPOIMP = t1.NRSEQUMAPAPU)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSTONF". Cannot delete parent "MAPAAPURAC".'
          goto error
       end
    
    /*  Cannot delete parent "MAPAAPURAC" if children still exist in "PARCLANCATIV"  */
    if exists (select 1
               from   PARCLANCATIV t2, deleted t1
               where  t2.NRSEQUMAPAPU = t1.NRSEQUMAPAPU)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARCLANCATIV". Cannot delete parent "MAPAAPURAC".'
          goto error
       end
    
    /*  Cannot delete parent "MAPAAPURAC" if children still exist in "PARGERGIASC"  */
    if exists (select 1
               from   PARGERGIASC t2, deleted t1
               where  t2.NRSEQUMAPAPU = t1.NRSEQUMAPAPU)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARGERGIASC". Cannot delete parent "MAPAAPURAC".'
          goto error
       end
    
    /*  Cannot delete parent "MAPAAPURAC" if children still exist in "LANCINFCOMP"  */
    if exists (select 1
               from   LANCINFCOMP t2, deleted t1
               where  t2.NRSEQUMAPAPU = t1.NRSEQUMAPAPU)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCINFCOMP". Cannot delete parent "MAPAAPURAC".'
          goto error
       end
    
    /*  Delete all children in "MAPARECUP"  */
    delete MAPARECUP
    from   MAPARECUP t2, deleted t1
    where  t2.NRSEQUMAPAPU = t1.NRSEQUMAPAPU

    return

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

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

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

    
    /*  Cannot delete parent "MAPABASEV" if children still exist in "TROCOMAPABAS"  */
    if exists (select 1
               from   TROCOMAPABAS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRMAPABASEV = t1.NRMAPABASEV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TROCOMAPABAS". Cannot delete parent "MAPABASEV".'
          goto error
       end
    
    /*  Cannot delete parent "MAPABASEV" if children still exist in "OBSMAPABASEV"  */
    if exists (select 1
               from   OBSMAPABASEV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRMAPABASEV = t1.NRMAPABASEV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSMAPABASEV". Cannot delete parent "MAPABASEV".'
          goto error
       end
    
    /*  Cannot delete parent "MAPABASEV" if children still exist in "RECITEMAPABA"  */
    if exists (select 1
               from   RECITEMAPABA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRMAPABASEV = t1.NRMAPABASEV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECITEMAPABA". Cannot delete parent "MAPABASEV".'
          goto error
       end
    
    /*  Cannot delete parent "MAPABASEV" if children still exist in "SOLICTROCO"  */
    if exists (select 1
               from   SOLICTROCO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRMAPABASEV = t1.NRMAPABASEV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICTROCO". Cannot delete parent "MAPABASEV".'
          goto error
       end
    
    /*  Cannot delete parent "MAPABASEV" if children still exist in "VENDAVMN"  */
    if exists (select 1
               from   VENDAVMN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRMAPABASEV = t1.NRMAPABASEV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDAVMN". Cannot delete parent "MAPABASEV".'
          goto error
       end
    
    /*  Cannot delete parent "MAPABASEV" if children still exist in "MOVIVALOR"  */
    if exists (select 1
               from   MOVIVALOR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRMAPABASEV = t1.NRMAPABASEV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVIVALOR". Cannot delete parent "MAPABASEV".'
          goto error
       end
    
    /*  Cannot delete parent "MAPABASEV" if children still exist in "ITPREVABAMAP"  */
    if exists (select 1
               from   ITPREVABAMAP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRMAPABASEV = t1.NRMAPABASEV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITPREVABAMAP". Cannot delete parent "MAPABASEV".'
          goto error
       end
    
    /*  Cannot delete parent "MAPABASEV" if children still exist in "ITPREVABASAU"  */
    if exists (select 1
               from   ITPREVABASAU t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRMAPABASEV = t1.NRMAPABASEV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITPREVABASAU". Cannot delete parent "MAPABASEV".'
          goto error
       end
    
    /*  Delete all children in "ITEMAPABASEV"  */
    delete ITEMAPABASEV
    from   ITEMAPABASEV t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRMAPABASEV = t1.NRMAPABASEV
    
    /*  Delete all children in "NFRELACMAPAB"  */
    delete NFRELACMAPAB
    from   NFRELACMAPAB t2, deleted t1
    where  t2.CDFILMAPABAS = t1.CDFILIAL
     and   t2.NRMAPABASEV = t1.NRMAPABASEV

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "MARCA" if children still exist in "ITEMAUTF"  */
    if exists (select 1
               from   ITEMAUTF t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMAUTF". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "ITEMAUTF"  */
    if exists (select 1
               from   ITEMAUTF t2, deleted t1
               where  t2.CDSEGMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMAUTF". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "TESTEPROD"  */
    if exists (select 1
               from   TESTEPROD t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TESTEPROD". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "PARTFILI"  */
    if exists (select 1
               from   PARTFILI t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARTFILI". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "QFOMARPROD"  */
    if exists (select 1
               from   QFOMARPROD t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "QFOMARPROD". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "MARCCOTA"  */
    if exists (select 1
               from   MARCCOTA t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MARCCOTA". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "PARASUPRI"  */
    if exists (select 1
               from   PARASUPRI t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARASUPRI". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "ITEMPEDCOL"  */
    if exists (select 1
               from   ITEMPEDCOL t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPEDCOL". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "ITEMCOLE"  */
    if exists (select 1
               from   ITEMCOLE t2, deleted t1
               where  t2.CDSEGMARCCOL = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMCOLE". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "ITEMCOLE"  */
    if exists (select 1
               from   ITEMCOLE t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMCOLE". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "MARCTESTPR"  */
    if exists (select 1
               from   MARCTESTPR t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MARCTESTPR". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "COLETDOAC"  */
    if exists (select 1
               from   COLETDOAC t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COLETDOAC". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "RECEBDOAC"  */
    if exists (select 1
               from   RECEBDOAC t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEBDOAC". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "PROTESE"  */
    if exists (select 1
               from   PROTESE t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROTESE". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "ITEMENTREGA"  */
    if exists (select 1
               from   ITEMENTREGA t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMENTREGA". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "ITEMAUTF"  */
    if exists (select 1
               from   ITEMAUTF t2, deleted t1
               where  t2.CDMARCAOL = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMAUTF". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "RELACPRODOL"  */
    if exists (select 1
               from   RELACPRODOL t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELACPRODOL". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "MARCPRODUTO"  */
    if exists (select 1
               from   MARCPRODUTO t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MARCPRODUTO". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "ITEMPEDEXOL"  */
    if exists (select 1
               from   ITEMPEDEXOL t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPEDEXOL". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "MOVOPAF"  */
    if exists (select 1
               from   MOVOPAF t2, deleted t1
               where  t2.CDMARCMOV = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOPAF". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Cannot delete parent "MARCA" if children still exist in "PRMARCPEDCOL"  */
    if exists (select 1
               from   PRMARCPEDCOL t2, deleted t1
               where  t2.CDMARCA = t1.CDMARCA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRMARCPEDCOL". Cannot delete parent "MARCA".'
          goto error
       end
    
    /*  Delete all children in "MARCPROD"  */
    delete MARCPROD
    from   MARCPROD t2, deleted t1
    where  t2.CDMARCA = t1.CDMARCA

    return

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

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

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

    
    /*  Delete all children in "MARCPRODCOMP"  */
    delete MARCPRODCOMP
    from   MARCPRODCOMP t2, deleted t1
    where  t2.CDMARCA = t1.CDMARCA
     and   t2.CDPRODUTO = t1.CDPRODUTO

    return

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

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

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

    
    /*  Delete all children in "MEDICAPACI"  */
    delete MEDICAPACI
    from   MEDICAPACI t2, deleted t1
    where  t2.CDMEDICAMENT = t1.CDMEDICAMENT
    
    /*  Delete all children in "PRINATIVMEDI"  */
    delete PRINATIVMEDI
    from   PRINATIVMEDI t2, deleted t1
    where  t2.CDMEDICAMENT = t1.CDMEDICAMENT

    return

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

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

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

    
    /*  Cannot delete parent "MEDICASE" if children still exist in "ITINQUERPACI"  */
    if exists (select 1
               from   ITINQUERPACI t2, deleted t1
               where  t2.CDMEDICASE = t1.CDMEDICASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITINQUERPACI". Cannot delete parent "MEDICASE".'
          goto error
       end
    
    /*  Cannot delete parent "MEDICASE" if children still exist in "COMCADIAPACI"  */
    if exists (select 1
               from   COMCADIAPACI t2, deleted t1
               where  t2.CDMEDICASE = t1.CDMEDICASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMCADIAPACI". Cannot delete parent "MEDICASE".'
          goto error
       end
    
    /*  Cannot delete parent "MEDICASE" if children still exist in "PRODMECA"  */
    if exists (select 1
               from   PRODMECA t2, deleted t1
               where  t2.CDMEDICASE = t1.CDMEDICASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODMECA". Cannot delete parent "MEDICASE".'
          goto error
       end
    
    /*  Cannot delete parent "MEDICASE" if children still exist in "CARDCONSNUTR"  */
    if exists (select 1
               from   CARDCONSNUTR t2, deleted t1
               where  t2.CDMEDICASE = t1.CDMEDICASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDCONSNUTR". Cannot delete parent "MEDICASE".'
          goto error
       end
    
    /*  Cannot delete parent "MEDICASE" if children still exist in "ITEQUIVALE"  */
    if exists (select 1
               from   ITEQUIVALE t2, deleted t1
               where  t2.CDMEDICASE = t1.CDMEDICASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEQUIVALE". Cannot delete parent "MEDICASE".'
          goto error
       end
    
    /*  Delete all children in "MEDIPRATO"  */
    delete MEDIPRATO
    from   MEDIPRATO t2, deleted t1
    where  t2.CDMEDICASE = t1.CDMEDICASE

    return

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

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

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

    
    /*  Delete all children in "FGMENUGRUPOP"  */
    delete FGMENUGRUPOP
    from   FGMENUGRUPOP t2, deleted t1
    where  t2.CDGRUPOPER = t1.CDGRUPOPER
     and   t2.CDSISTGRUP = t1.CDSISTGRUP
     and   t2.CDMENUITGRUP = t1.CDMENUITGRUP

    return

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

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

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

    
    /*  Delete all children in "FGMENUOPER"  */
    delete FGMENUOPER
    from   FGMENUOPER t2, deleted t1
    where  t2.CDOPERADOR = t1.CDOPERADOR
     and   t2.CDSISTOPER = t1.CDSISTOPER
     and   t2.CDMENUITOPER = t1.CDMENUITOPER

    return

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

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

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

    
    /*  Delete all children in "INFOAVAL"  */
    delete INFOAVAL
    from   INFOAVAL t2, deleted t1
    where  t2.CDTBMETOAVAL = t1.CDTBMETOAVAL
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTCONSPACI = t1.DTCONSPACI
    
    /*  Delete all children in "RESUAVAL"  */
    delete RESUAVAL
    from   RESUAVAL t2, deleted t1
    where  t2.CDTBMETOAVAL = t1.CDTBMETOAVAL
     and   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_metorefe" for table "METOREFE"  */
create trigger td_metorefe on METOREFE for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

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

    
    /*  Delete all children in "TABEVRDIET"  */
    delete TABEVRDIET
    from   TABEVRDIET t2, deleted t1
    where  t2.CDREFEBIBL = t1.CDREFEBIBL
     and   t2.CDTBMETOAVAL = t1.CDTBMETOAVAL
    
    /*  Delete all children in "TABVRPREGAS"  */
    delete TABVRPREGAS
    from   TABVRPREGAS t2, deleted t1
    where  t2.CDREFEBIBL = t1.CDREFEBIBL
     and   t2.CDTBMETOAVAL = t1.CDTBMETOAVAL

    return

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

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

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

    
    /*  Cannot delete parent "MODPGTO" if children still exist in "PRECOUTI"  */
    if exists (select 1
               from   PRECOUTI t2, deleted t1
               where  t2.CDMODPGTO = t1.CDMODPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRECOUTI". Cannot delete parent "MODPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "MODPGTO" if children still exist in "PEDIDO"  */
    if exists (select 1
               from   PEDIDO t2, deleted t1
               where  t2.CDMODPGTO = t1.CDMODPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDO". Cannot delete parent "MODPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "MODPGTO" if children still exist in "PARAMPEDIDO"  */
    if exists (select 1
               from   PARAMPEDIDO t2, deleted t1
               where  t2.CDMODPGTO = t1.CDMODPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPEDIDO". Cannot delete parent "MODPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "MODPGTO" if children still exist in "PROPOSTA"  */
    if exists (select 1
               from   PROPOSTA t2, deleted t1
               where  t2.CDMODPGTO = t1.CDMODPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPOSTA". Cannot delete parent "MODPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "MODPGTO" if children still exist in "CLIENTE"  */
    if exists (select 1
               from   CLIENTE t2, deleted t1
               where  t2.CDMODPGTO = t1.CDMODPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENTE". Cannot delete parent "MODPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "MODPGTO" if children still exist in "ORCAEVENTO"  */
    if exists (select 1
               from   ORCAEVENTO t2, deleted t1
               where  t2.CDMODPGTO = t1.CDMODPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAEVENTO". Cannot delete parent "MODPGTO".'
          goto error
       end
    
    /*  Cannot delete parent "MODPGTO" if children still exist in "PARAMORCAEVE"  */
    if exists (select 1
               from   PARAMORCAEVE t2, deleted t1
               where  t2.CDMODPGMOBRA = t1.CDMODPGTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMORCAEVE". Cannot delete parent "MODPGTO".'
          goto error
       end
    
    /*  Delete all children in "PRAZOMODPGTO"  */
    delete PRAZOMODPGTO
    from   PRAZOMODPGTO t2, deleted t1
    where  t2.CDMODPGTO = t1.CDMODPGTO

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "MOVFLUXO" if children still exist in "TRANSFBANC"  */
    if exists (select 1
               from   TRANSFBANC t2, deleted t1
               where  t2.CDCLASSFINA = t1.CDCLASSFINA
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFILIAL = t1.CDFILIAL
                and   t2.DTMOVFLUXO = t1.DTMOVFLUXO
                and   t2.IDMOVFLUXO = t1.IDMOVFLUXO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TRANSFBANC". Cannot delete parent "MOVFLUXO".'
          goto error
       end
    
    /*  Cannot delete parent "MOVFLUXO" if children still exist in "MOVCREDBANC"  */
    if exists (select 1
               from   MOVCREDBANC t2, deleted t1
               where  t2.CDCLASSFINA = t1.CDCLASSFINA
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFILIAL = t1.CDFILIAL
                and   t2.DTMOVFLUXO = t1.DTMOVFLUXO
                and   t2.IDMOVFLUXO = t1.IDMOVFLUXO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVCREDBANC". Cannot delete parent "MOVFLUXO".'
          goto error
       end
    
    /*  Cannot delete parent "MOVFLUXO" if children still exist in "MOVDEBBANC"  */
    if exists (select 1
               from   MOVDEBBANC t2, deleted t1
               where  t2.CDCLASSFINA = t1.CDCLASSFINA
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFILIAL = t1.CDFILIAL
                and   t2.DTMOVFLUXO = t1.DTMOVFLUXO
                and   t2.IDMOVFLUXO = t1.IDMOVFLUXO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVDEBBANC". Cannot delete parent "MOVFLUXO".'
          goto error
       end
    
    /*  Cannot delete parent "MOVFLUXO" if children still exist in "CONTAPAGAR"  */
    if exists (select 1
               from   CONTAPAGAR t2, deleted t1
               where  t2.CDCLASSFINA = t1.CDCLASSFINA
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFIMOVFLPAG = t1.CDFILIAL
                and   t2.DTMOVFLUXO = t1.DTMOVFLUXO
                and   t2.IDMOVFLUXO = t1.IDMOVFLUXO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTAPAGAR". Cannot delete parent "MOVFLUXO".'
          goto error
       end
    
    /*  Cannot delete parent "MOVFLUXO" if children still exist in "CONTARECEBER"  */
    if exists (select 1
               from   CONTARECEBER t2, deleted t1
               where  t2.CDCLASSFINA = t1.CDCLASSFINA
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFIMOVFLREC = t1.CDFILIAL
                and   t2.DTMOVFLUXO = t1.DTMOVFLUXO
                and   t2.IDMOVFLUXO = t1.IDMOVFLUXO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTARECEBER". Cannot delete parent "MOVFLUXO".'
          goto error
       end
    
    /*  Cannot delete parent "MOVFLUXO" if children still exist in "AJCTPAGAR"  */
    if exists (select 1
               from   AJCTPAGAR t2, deleted t1
               where  t2.CDCLASSFINA = t1.CDCLASSFINA
                and   t2.CDEMMFAJCNTP = t1.CDEMPRESA
                and   t2.CDFIMFAJCNTP = t1.CDFILIAL
                and   t2.DTMOVFLUXO = t1.DTMOVFLUXO
                and   t2.IDMOVFLUXO = t1.IDMOVFLUXO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJCTPAGAR". Cannot delete parent "MOVFLUXO".'
          goto error
       end
    
    /*  Cannot delete parent "MOVFLUXO" if children still exist in "AJBOLPAG"  */
    if exists (select 1
               from   AJBOLPAG t2, deleted t1
               where  t2.CDCLASSFINA = t1.CDCLASSFINA
                and   t2.CDEMMFAJBOLP = t1.CDEMPRESA
                and   t2.CDFIMFAJBOLP = t1.CDFILIAL
                and   t2.DTMOVFLUXO = t1.DTMOVFLUXO
                and   t2.IDMOVFLUXO = t1.IDMOVFLUXO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJBOLPAG". Cannot delete parent "MOVFLUXO".'
          goto error
       end
    
    /*  Cannot delete parent "MOVFLUXO" if children still exist in "AJBOLREC"  */
    if exists (select 1
               from   AJBOLREC t2, deleted t1
               where  t2.CDCLASSFINA = t1.CDCLASSFINA
                and   t2.CDEMMFAJBOLR = t1.CDEMPRESA
                and   t2.CDFIMFAJBOLR = t1.CDFILIAL
                and   t2.DTMOVFLUXO = t1.DTMOVFLUXO
                and   t2.IDMOVFLUXO = t1.IDMOVFLUXO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJBOLREC". Cannot delete parent "MOVFLUXO".'
          goto error
       end
    
    /*  Cannot delete parent "MOVFLUXO" if children still exist in "AJCTRECEBER"  */
    if exists (select 1
               from   AJCTRECEBER t2, deleted t1
               where  t2.CDCLASSFINA = t1.CDCLASSFINA
                and   t2.CDEMMFAJCNTR = t1.CDEMPRESA
                and   t2.CDFIMFAJCNTR = t1.CDFILIAL
                and   t2.DTMOVFLUXO = t1.DTMOVFLUXO
                and   t2.IDMOVFLUXO = t1.IDMOVFLUXO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJCTRECEBER". Cannot delete parent "MOVFLUXO".'
          goto error
       end
    
    /*  Delete all children in "MOVFXAJBOLP"  */
    delete MOVFXAJBOLP
    from   MOVFXAJBOLP t2, deleted t1
    where  t2.CDCLASSFINA = t1.CDCLASSFINA
     and   t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.DTMOVFLUXO = t1.DTMOVFLUXO
     and   t2.IDMOVFLUXO = t1.IDMOVFLUXO
    
    /*  Delete all children in "MOVFXAJBOLR"  */
    delete MOVFXAJBOLR
    from   MOVFXAJBOLR t2, deleted t1
    where  t2.CDCLASSFINA = t1.CDCLASSFINA
     and   t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.DTMOVFLUXO = t1.DTMOVFLUXO
     and   t2.IDMOVFLUXO = t1.IDMOVFLUXO

    return

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

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

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

    
    /*  Delete all children in "ITMOVICOME"  */
    delete ITMOVICOME
    from   ITMOVICOME t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDCOMENSAL = t1.CDCOMENSAL
     and   t2.DTHRMOVICOME = t1.DTHRMOVICOME

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "MOVOCORR" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDFILILANCMO = t1.CDFILILANCMO
                and   t2.NROCORRMOV = t1.NROCORRMOV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "MOVOCORR".'
          goto error
       end
    
    /*  Delete all children in "PARECER"  */
    delete PARECER
    from   PARECER t2, deleted t1
    where  t2.CDFILILANCMO = t1.CDFILILANCMO
     and   t2.NROCORRMOV = t1.NROCORRMOV
    
    /*  Delete all children in "OCOAPURQFO"  */
    delete OCOAPURQFO
    from   OCOAPURQFO t2, deleted t1
    where  t2.CDFILILANCMO = t1.CDFILILANCMO
     and   t2.NROCORRMOV = t1.NROCORRMOV
    
    /*  Delete all children in "DESCMOVOCORR"  */
    delete DESCMOVOCORR
    from   DESCMOVOCORR t2, deleted t1
    where  t2.CDFILILANCMO = t1.CDFILILANCMO
     and   t2.NROCORRMOV = t1.NROCORRMOV

    return

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

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

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

    
    /*  Delete all children in "ACAOOCORPROD"  */
    delete ACAOOCORPROD
    from   ACAOOCORPROD t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRMOVOCOPROD = t1.NRMOVOCOPROD
    
    /*  Delete all children in "PAREOCORPROD"  */
    delete PAREOCORPROD
    from   PAREOCORPROD t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRMOVOCOPROD = t1.NRMOVOCOPROD
    
    /*  Delete all children in "AVALOCORPROD"  */
    delete AVALOCORPROD
    from   AVALOCORPROD t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRMOVOCOPROD = t1.NRMOVOCOPROD

    return

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

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

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

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

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "SERVPLANO"  */
    if exists (select 1
               from   SERVPLANO t2, deleted t1
               where  t2.CDFILIALNF = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVPLANO". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "ITEMNOTAFISC"  */
    if exists (select 1
               from   ITEMNOTAFISC t2, deleted t1
               where  t2.CDFILIALIT = t1.CDFILIAL
                and   t2.NRLANCTONFIT = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMNOTAFISC". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "CONTAPAGAR"  */
    if exists (select 1
               from   CONTAPAGAR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTAPAGAR". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "ITEMAUTF"  */
    if exists (select 1
               from   ITEMAUTF t2, deleted t1
               where  t2.CDFILLANC = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMAUTF". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "MAPAAPURAC"  */
    if exists (select 1
               from   MAPAAPURAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPAAPURAC". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "ROMANEIONF"  */
    if exists (select 1
               from   ROMANEIONF t2, deleted t1
               where  t2.CDFILIALNF = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROMANEIONF". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "AMOSTRAPROC"  */
    if exists (select 1
               from   AMOSTRAPROC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AMOSTRAPROC". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "MOVOCORR"  */
    if exists (select 1
               from   MOVOCORR t2, deleted t1
               where  t2.CDFILIALNF = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOCORR". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "LANCDESPFATU"  */
    if exists (select 1
               from   LANCDESPFATU t2, deleted t1
               where  t2.CDFILILANC = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCDESPFATU". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "PREVRETI"  */
    if exists (select 1
               from   PREVRETI t2, deleted t1
               where  t2.CDFILIALNF = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVRETI". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "FORMULARIONF"  */
    if exists (select 1
               from   FORMULARIONF t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORMULARIONF". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "NFRELACAF"  */
    if exists (select 1
               from   NFRELACAF t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFRELACAF". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "NFABASTEROTA"  */
    if exists (select 1
               from   NFABASTEROTA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFABASTEROTA". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "MAPARESUMECF"  */
    if exists (select 1
               from   MAPARESUMECF t2, deleted t1
               where  t2.CDFILIALNF = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPARESUMECF". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "EXPNFFRETE"  */
    if exists (select 1
               from   EXPNFFRETE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXPNFFRETE". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "ITREQUESTO"  */
    if exists (select 1
               from   ITREQUESTO t2, deleted t1
               where  t2.CDFILIALRE = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITREQUESTO". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "ITEMROMANEIO"  */
    if exists (select 1
               from   ITEMROMANEIO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMROMANEIO". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "OCORRERFATUR"  */
    if exists (select 1
               from   OCORRERFATUR t2, deleted t1
               where  t2.CDFILIALNF = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OCORRERFATUR". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "NFRELACAFSAI"  */
    if exists (select 1
               from   NFRELACAFSAI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFRELACAFSAI". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "INVOICE"  */
    if exists (select 1
               from   INVOICE t2, deleted t1
               where  t2.CDFILIALNF = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVOICE". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "APURFATUFAS"  */
    if exists (select 1
               from   APURFATUFAS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURFATUFAS". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "NFRELACPEXOL"  */
    if exists (select 1
               from   NFRELACPEXOL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFRELACPEXOL". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "NFRELACORC"  */
    if exists (select 1
               from   NFRELACORC t2, deleted t1
               where  t2.CDFILIALNF = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFRELACORC". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "MOVCAIXA"  */
    if exists (select 1
               from   MOVCAIXA t2, deleted t1
               where  t2.CDFILIALNF = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVCAIXA". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Cannot delete parent "NOTAFISCAL" if children still exist in "NFRELACCDRS"  */
    if exists (select 1
               from   NFRELACCDRS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRLANCTONF = t1.NRLANCTONF)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFRELACCDRS". Cannot delete parent "NOTAFISCAL".'
          goto error
       end
    
    /*  Delete all children in "ITEMENCARGO"  */
    delete ITEMENCARGO
    from   ITEMENCARGO t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
    
    /*  Delete all children in "ITEMNOTAFISC"  */
    delete ITEMNOTAFISC
    from   ITEMNOTAFISC t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
    
    /*  Delete all children in "IMPOSTONF"  */
    delete IMPOSTONF
    from   IMPOSTONF t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
    
    /*  Delete all children in "ITEMPRODUTO"  */
    delete ITEMPRODUTO
    from   ITEMPRODUTO t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
    
    /*  Delete all children in "DADOSADIC"  */
    delete DADOSADIC
    from   DADOSADIC t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
    
    /*  Delete all children in "LANCTOESTOQ"  */
    delete LANCTOESTOQ
    from   LANCTOESTOQ t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
    
    /*  Delete all children in "IMPOSNFTOT"  */
    delete IMPOSNFTOT
    from   IMPOSNFTOT t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
    
    /*  Delete all children in "CONTARECEBER"  */
    delete CONTARECEBER
    from   CONTARECEBER t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
    
    /*  Delete all children in "NFRELACSOLI"  */
    delete NFRELACSOLI
    from   NFRELACSOLI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
    
    /*  Delete all children in "NFRELACMAPAB"  */
    delete NFRELACMAPAB
    from   NFRELACMAPAB t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
    
    /*  Delete all children in "RELAT_CDRS"  */
    delete RELAT_CDRS
    from   RELAT_CDRS t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
    
    /*  Delete all children in "DADOSNF"  */
    delete DADOSNF
    from   DADOSNF t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF
    
    /*  Delete all children in "DADOSORIGNF"  */
    delete DADOSORIGNF
    from   DADOSORIGNF t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRLANCTONF = t1.NRLANCTONF

    return

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

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

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

    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.CDFILIALRE = t1.CDFILIAL
     and   t2.DTNUMERNF = t1.DTNUMERNF
     and   t2.IDENTRSAID = t1.IDENTRSAID
     and   t2.IDTIPOOPERNF = t1.IDTIPOOPERNF
     and   t2.NRNUMERNF = t1.NRNUMERNF

    return

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

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

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

    
    /*  Cannot delete parent "NUTRIENTE" if children still exist in "LIMNUTRI"  */
    if exists (select 1
               from   LIMNUTRI t2, deleted t1
               where  t2.CDNUTRIENTE = t1.CDNUTRIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LIMNUTRI". Cannot delete parent "NUTRIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "NUTRIENTE" if children still exist in "NUTRPROD"  */
    if exists (select 1
               from   NUTRPROD t2, deleted t1
               where  t2.CDNUTRIENTE = t1.CDNUTRIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NUTRPROD". Cannot delete parent "NUTRIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "NUTRIENTE" if children still exist in "DIAGNUTR"  */
    if exists (select 1
               from   DIAGNUTR t2, deleted t1
               where  t2.CDNUTRIENTE = t1.CDNUTRIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIAGNUTR". Cannot delete parent "NUTRIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "NUTRIENTE" if children still exist in "NUTRECOM"  */
    if exists (select 1
               from   NUTRECOM t2, deleted t1
               where  t2.CDNUTRIENTE = t1.CDNUTRIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NUTRECOM". Cannot delete parent "NUTRIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "NUTRIENTE" if children still exist in "NUTRNECE"  */
    if exists (select 1
               from   NUTRNECE t2, deleted t1
               where  t2.CDNUTRIENTE = t1.CDNUTRIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NUTRNECE". Cannot delete parent "NUTRIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "NUTRIENTE" if children still exist in "GREQUIVALE"  */
    if exists (select 1
               from   GREQUIVALE t2, deleted t1
               where  t2.CDNUTRIENTE = t1.CDNUTRIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GREQUIVALE". Cannot delete parent "NUTRIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "NUTRIENTE" if children still exist in "NUTRECOMPACI"  */
    if exists (select 1
               from   NUTRECOMPACI t2, deleted t1
               where  t2.CDNUTRIENTE = t1.CDNUTRIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NUTRECOMPACI". Cannot delete parent "NUTRIENTE".'
          goto error
       end
    
    /*  Delete all children in "PRINATIVNUTR"  */
    delete PRINATIVNUTR
    from   PRINATIVNUTR t2, deleted t1
    where  t2.CDNUTRIENTE = t1.CDNUTRIENTE
    
    /*  Delete all children in "TRABNUTRIREC"  */
    delete TRABNUTRIREC
    from   TRABNUTRIREC t2, deleted t1
    where  t2.CDNUTRIENTE = t1.CDNUTRIENTE

    return

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

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

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

    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LANCTOESTOQ"  */
    if exists (select 1
               from   LANCTOESTOQ t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCTOESTOQ". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CARDADIA"  */
    if exists (select 1
               from   CARDADIA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDADIA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CONSPACI"  */
    if exists (select 1
               from   CONSPACI t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSPACI". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MOVOCORR"  */
    if exists (select 1
               from   MOVOCORR t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOCORR". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MOVOPSOL"  */
    if exists (select 1
               from   MOVOPSOL t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOPSOL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGNECECOMP"  */
    if exists (select 1
               from   LOGNECECOMP t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGNECECOMP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGPREVCONS"  */
    if exists (select 1
               from   LOGPREVCONS t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGPREVCONS". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGPREVRETI"  */
    if exists (select 1
               from   LOGPREVRETI t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGPREVRETI". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MOVSOLEXT"  */
    if exists (select 1
               from   MOVSOLEXT t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVSOLEXT". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "TALAOCHEQUE"  */
    if exists (select 1
               from   TALAOCHEQUE t2, deleted t1
               where  t2.CDRESPRECEB = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TALAOCHEQUE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "FILIAL"  */
    if exists (select 1
               from   FILIAL t2, deleted t1
               where  t2.CDGOPERAC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIAL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "FILIAL"  */
    if exists (select 1
               from   FILIAL t2, deleted t1
               where  t2.CDSUPERVISOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIAL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "FILIAL"  */
    if exists (select 1
               from   FILIAL t2, deleted t1
               where  t2.CDADMINISTRA = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIAL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGSERVTRAN"  */
    if exists (select 1
               from   LOGSERVTRAN t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGSERVTRAN". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PROGOPER"  */
    if exists (select 1
               from   PROGOPER t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROGOPER". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGLIBEPERS"  */
    if exists (select 1
               from   LOGLIBEPERS t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGLIBEPERS". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "TARESERV"  */
    if exists (select 1
               from   TARESERV t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TARESERV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGFASETRAM"  */
    if exists (select 1
               from   LOGFASETRAM t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGFASETRAM". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CNABPAG"  */
    if exists (select 1
               from   CNABPAG t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CNABPAG". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CNABREC"  */
    if exists (select 1
               from   CNABREC t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CNABREC". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "TARESERV"  */
    if exists (select 1
               from   TARESERV t2, deleted t1
               where  t2.CDOPLIBETEST = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TARESERV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "VERSGSIS"  */
    if exists (select 1
               from   VERSGSIS t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VERSGSIS". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "TURCAIXA"  */
    if exists (select 1
               from   TURCAIXA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TURCAIXA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "TESTTARE"  */
    if exists (select 1
               from   TESTTARE t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TESTTARE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CNABBCO"  */
    if exists (select 1
               from   CNABBCO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CNABBCO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGDATACOMP"  */
    if exists (select 1
               from   LOGDATACOMP t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGDATACOMP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDOPANALISE = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PREVRETI"  */
    if exists (select 1
               from   PREVRETI t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVRETI". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PAROPERSERV"  */
    if exists (select 1
               from   PAROPERSERV t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PAROPERSERV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "GERENTE"  */
    if exists (select 1
               from   GERENTE t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERENTE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "COMPRADOR"  */
    if exists (select 1
               from   COMPRADOR t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPRADOR". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MALADIRETA"  */
    if exists (select 1
               from   MALADIRETA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MALADIRETA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ATUALIZABD"  */
    if exists (select 1
               from   ATUALIZABD t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ATUALIZABD". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OBSACAO"  */
    if exists (select 1
               from   OBSACAO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSACAO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OBSACAO"  */
    if exists (select 1
               from   OBSACAO t2, deleted t1
               where  t2.CDOPEVISTOAC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSACAO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGTRAMACAO"  */
    if exists (select 1
               from   LOGTRAMACAO t2, deleted t1
               where  t2.CDOPERECEBAC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGTRAMACAO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGTRAMACAO"  */
    if exists (select 1
               from   LOGTRAMACAO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGTRAMACAO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ETAPACAO"  */
    if exists (select 1
               from   ETAPACAO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETAPACAO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ACAO"  */
    if exists (select 1
               from   ACAO t2, deleted t1
               where  t2.CDOPERABERAC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACAO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ACAO"  */
    if exists (select 1
               from   ACAO t2, deleted t1
               where  t2.CDOPERRESPAC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACAO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGPREVACAO"  */
    if exists (select 1
               from   LOGPREVACAO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGPREVACAO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PARAMFILIAL"  */
    if exists (select 1
               from   PARAMFILIAL t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMFILIAL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGAPURQFO"  */
    if exists (select 1
               from   LOGAPURQFO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGAPURQFO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ACAO"  */
    if exists (select 1
               from   ACAO t2, deleted t1
               where  t2.CDOPERAVAL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACAO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "NUTRICIO"  */
    if exists (select 1
               from   NUTRICIO t2, deleted t1
               where  t2.CDNUTRICIO = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NUTRICIO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGDATAPREV"  */
    if exists (select 1
               from   LOGDATAPREV t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGDATAPREV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "DELIVERY"  */
    if exists (select 1
               from   DELIVERY t2, deleted t1
               where  t2.CDOPCANCDELI = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DELIVERY". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CUSTOPRODUTO"  */
    if exists (select 1
               from   CUSTOPRODUTO t2, deleted t1
               where  t2.CDOPERCPRO = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOPRODUTO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CUSTOPROCIA"  */
    if exists (select 1
               from   CUSTOPROCIA t2, deleted t1
               where  t2.CDOPERPCIA = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOPROCIA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PLANOPGESTA"  */
    if exists (select 1
               from   PLANOPGESTA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PLANOPGESTA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "AGEATIVISALA"  */
    if exists (select 1
               from   AGEATIVISALA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGEATIVISALA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGCOPECTBL"  */
    if exists (select 1
               from   LOGCOPECTBL t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGCOPECTBL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGMATRICULA"  */
    if exists (select 1
               from   LOGMATRICULA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGMATRICULA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "VENDA"  */
    if exists (select 1
               from   VENDA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "TURCAIXA"  */
    if exists (select 1
               from   TURCAIXA t2, deleted t1
               where  t2.CDOPERABER = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TURCAIXA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "TURCAIXA"  */
    if exists (select 1
               from   TURCAIXA t2, deleted t1
               where  t2.CDOPERFECH = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TURCAIXA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGEQUIPPRO"  */
    if exists (select 1
               from   LOGEQUIPPRO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGEQUIPPRO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "AGENEQUI"  */
    if exists (select 1
               from   AGENEQUI t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENEQUI". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SEQNUMAVIENT"  */
    if exists (select 1
               from   SEQNUMAVIENT t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SEQNUMAVIENT". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "AUDITORIA"  */
    if exists (select 1
               from   AUDITORIA t2, deleted t1
               where  t2.CDOPEINCAUDI = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUDITORIA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "INSPECAUDIT"  */
    if exists (select 1
               from   INSPECAUDIT t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPECAUDIT". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITEMINSAUDIT"  */
    if exists (select 1
               from   ITEMINSAUDIT t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMINSAUDIT". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "VISTINSAUDIT"  */
    if exists (select 1
               from   VISTINSAUDIT t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VISTINSAUDIT". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PESSAUDIT"  */
    if exists (select 1
               from   PESSAUDIT t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PESSAUDIT". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SIMULACAO"  */
    if exists (select 1
               from   SIMULACAO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SIMULACAO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "VISHEMOPRDOA"  */
    if exists (select 1
               from   VISHEMOPRDOA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VISHEMOPRDOA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "POOLHEMOPR"  */
    if exists (select 1
               from   POOLHEMOPR t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "POOLHEMOPR". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "RECEBDOAC"  */
    if exists (select 1
               from   RECEBDOAC t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEBDOAC". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "RECEBDOAC"  */
    if exists (select 1
               from   RECEBDOAC t2, deleted t1
               where  t2.CDOPEDEVDOAC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEBDOAC". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "HEMOPRDOAC"  */
    if exists (select 1
               from   HEMOPRDOAC t2, deleted t1
               where  t2.CDOPEINUHEMO = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HEMOPRDOAC". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "HEMOPRDOAC"  */
    if exists (select 1
               from   HEMOPRDOAC t2, deleted t1
               where  t2.CDOPEENVHEMO = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HEMOPRDOAC". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "HEMOPRDOAC"  */
    if exists (select 1
               from   HEMOPRDOAC t2, deleted t1
               where  t2.CDOPERECHEMO = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HEMOPRDOAC". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MOVPROTESE"  */
    if exists (select 1
               from   MOVPROTESE t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVPROTESE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "REQUHOSP"  */
    if exists (select 1
               from   REQUHOSP t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUHOSP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "RETIHOSP"  */
    if exists (select 1
               from   RETIHOSP t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RETIHOSP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITRETIHOSP"  */
    if exists (select 1
               from   ITRETIHOSP t2, deleted t1
               where  t2.CDOPERECRETI = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITRETIHOSP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "VENDEDOR"  */
    if exists (select 1
               from   VENDEDOR t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDEDOR". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "BANCSOLU"  */
    if exists (select 1
               from   BANCSOLU t2, deleted t1
               where  t2.CDOPINCLSOLU = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BANCSOLU". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "BANCSOLU"  */
    if exists (select 1
               from   BANCSOLU t2, deleted t1
               where  t2.CDOPAPROSOLU = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BANCSOLU". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MOVICLIE"  */
    if exists (select 1
               from   MOVICLIE t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVICLIE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MOVICLIE"  */
    if exists (select 1
               from   MOVICLIE t2, deleted t1
               where  t2.CDOPERECSALD = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVICLIE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PREVENDA"  */
    if exists (select 1
               from   PREVENDA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVENDA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ESPEPROD"  */
    if exists (select 1
               from   ESPEPROD t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESPEPROD". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SALDOCAIXA"  */
    if exists (select 1
               from   SALDOCAIXA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SALDOCAIXA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MOVIVALOR"  */
    if exists (select 1
               from   MOVIVALOR t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVIVALOR". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PROCFABRICA"  */
    if exists (select 1
               from   PROCFABRICA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROCFABRICA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGUTEQUIP"  */
    if exists (select 1
               from   LOGUTEQUIP t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGUTEQUIP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "VISTOINSPEC"  */
    if exists (select 1
               from   VISTOINSPEC t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VISTOINSPEC". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PREVCOMP"  */
    if exists (select 1
               from   PREVCOMP t2, deleted t1
               where  t2.CDOPGERPRCOM = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCOMP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PREVCOMP"  */
    if exists (select 1
               from   PREVCOMP t2, deleted t1
               where  t2.CDOPAPRPRCOM = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCOMP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "VISESPEPRO"  */
    if exists (select 1
               from   VISESPEPRO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VISESPEPRO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITAGENFUNC"  */
    if exists (select 1
               from   ITAGENFUNC t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITAGENFUNC". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CONETAPAOP"  */
    if exists (select 1
               from   CONETAPAOP t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONETAPAOP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGCATRACA"  */
    if exists (select 1
               from   LOGCATRACA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGCATRACA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGMOVREAL"  */
    if exists (select 1
               from   LOGMOVREAL t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGMOVREAL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OPERCLIE"  */
    if exists (select 1
               from   OPERCLIE t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERCLIE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CANDIDATO"  */
    if exists (select 1
               from   CANDIDATO t2, deleted t1
               where  t2.CDOPEENTCAND = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CANDIDATO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "RECURETAPAOP"  */
    if exists (select 1
               from   RECURETAPAOP t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECURETAPAOP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGINDICE"  */
    if exists (select 1
               from   LOGINDICE t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGINDICE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDOPERSOLI = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDOPERRESP = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "TARESERV"  */
    if exists (select 1
               from   TARESERV t2, deleted t1
               where  t2.CDOPERRESP = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TARESERV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CONTATO"  */
    if exists (select 1
               from   CONTATO t2, deleted t1
               where  t2.CDOPERCONT = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTATO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "EVOLCONT"  */
    if exists (select 1
               from   EVOLCONT t2, deleted t1
               where  t2.CDOPEREVOL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EVOLCONT". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGFASETRAM"  */
    if exists (select 1
               from   LOGFASETRAM t2, deleted t1
               where  t2.CDOPERRESP = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGFASETRAM". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDOPVALIIMPL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MOVIVALOR"  */
    if exists (select 1
               from   MOVIVALOR t2, deleted t1
               where  t2.CDOPERSOLVLR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVIVALOR". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ROTATRANS"  */
    if exists (select 1
               from   ROTATRANS t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROTATRANS". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "DESPHOSP"  */
    if exists (select 1
               from   DESPHOSP t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESPHOSP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "EXEMPLAR"  */
    if exists (select 1
               from   EXEMPLAR t2, deleted t1
               where  t2.CDOPERRESP = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXEMPLAR". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "EXEMPLAR"  */
    if exists (select 1
               from   EXEMPLAR t2, deleted t1
               where  t2.CDOPERVISTO = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXEMPLAR". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITLANCTOEST"  */
    if exists (select 1
               from   ITLANCTOEST t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITLANCTOEST". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDOPVERIIMPL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CONTATO"  */
    if exists (select 1
               from   CONTATO t2, deleted t1
               where  t2.CDOPCONCCONT = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTATO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "REVIPUBLPROD"  */
    if exists (select 1
               from   REVIPUBLPROD t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REVIPUBLPROD". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOTE"  */
    if exists (select 1
               from   LOTE t2, deleted t1
               where  t2.CDOPELIBLOTE = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOTE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "RELACOPER"  */
    if exists (select 1
               from   RELACOPER t2, deleted t1
               where  t2.CDOPERELAC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELACOPER". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "RELACOPER"  */
    if exists (select 1
               from   RELACOPER t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELACOPER". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OBSERVPROSP"  */
    if exists (select 1
               from   OBSERVPROSP t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSERVPROSP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OBSERVCLIE"  */
    if exists (select 1
               from   OBSERVCLIE t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSERVCLIE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "TAREFA"  */
    if exists (select 1
               from   TAREFA t2, deleted t1
               where  t2.CDOPERRESP = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAREFA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "TAREFA"  */
    if exists (select 1
               from   TAREFA t2, deleted t1
               where  t2.CDOPERABER = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAREFA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "TAREFA"  */
    if exists (select 1
               from   TAREFA t2, deleted t1
               where  t2.CDOPERCONC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAREFA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITEMTAREFA"  */
    if exists (select 1
               from   ITEMTAREFA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMTAREFA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PLANATIVTARE"  */
    if exists (select 1
               from   PLANATIVTARE t2, deleted t1
               where  t2.CDOPERRESP = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PLANATIVTARE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "APROATIVTARE"  */
    if exists (select 1
               from   APROATIVTARE t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APROATIVTARE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ATIVPESSOAL"  */
    if exists (select 1
               from   ATIVPESSOAL t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ATIVPESSOAL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "INSPECAUDIT"  */
    if exists (select 1
               from   INSPECAUDIT t2, deleted t1
               where  t2.CDOPECONCINS = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPECAUDIT". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PLANATIVTARE"  */
    if exists (select 1
               from   PLANATIVTARE t2, deleted t1
               where  t2.CDOPERINCL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PLANATIVTARE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "REQUESTO"  */
    if exists (select 1
               from   REQUESTO t2, deleted t1
               where  t2.CDOPERREQU = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUESTO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CODIGOBARRA"  */
    if exists (select 1
               from   CODIGOBARRA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODIGOBARRA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OPERPROJETO"  */
    if exists (select 1
               from   OPERPROJETO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERPROJETO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ACORDOREQPRO"  */
    if exists (select 1
               from   ACORDOREQPRO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACORDOREQPRO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MAPABASEV"  */
    if exists (select 1
               from   MAPABASEV t2, deleted t1
               where  t2.CDOPERABASEV = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPABASEV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "BAIXARECEBIDA"  */
    if exists (select 1
               from   BAIXARECEBIDA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BAIXARECEBIDA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "BAIXAPAGA"  */
    if exists (select 1
               from   BAIXAPAGA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BAIXAPAGA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "BASELINEPROJ"  */
    if exists (select 1
               from   BASELINEPROJ t2, deleted t1
               where  t2.CDOPERRESP = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BASELINEPROJ". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "BASELINEPROJ"  */
    if exists (select 1
               from   BASELINEPROJ t2, deleted t1
               where  t2.CDOPERINCL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BASELINEPROJ". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "RISCOPROJETO"  */
    if exists (select 1
               from   RISCOPROJETO t2, deleted t1
               where  t2.CDOPERINCL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RISCOPROJETO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "RISCOPROJETO"  */
    if exists (select 1
               from   RISCOPROJETO t2, deleted t1
               where  t2.CDOPEOCORISC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RISCOPROJETO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGREQPROD"  */
    if exists (select 1
               from   LOGREQPROD t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGREQPROD". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OPERENTITELE"  */
    if exists (select 1
               from   OPERENTITELE t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERENTITELE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ABASTEROTA"  */
    if exists (select 1
               from   ABASTEROTA t2, deleted t1
               where  t2.CDOPERABASEV = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ABASTEROTA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "JUSTIFICAVMN"  */
    if exists (select 1
               from   JUSTIFICAVMN t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "JUSTIFICAVMN". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITEMPREBASE"  */
    if exists (select 1
               from   ITEMPREBASE t2, deleted t1
               where  t2.CDOPERATZIPB = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPREBASE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PROJETO"  */
    if exists (select 1
               from   PROJETO t2, deleted t1
               where  t2.CDOPERABER = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROJETO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PROJETO"  */
    if exists (select 1
               from   PROJETO t2, deleted t1
               where  t2.CDOPERCONC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROJETO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SOLICITA"  */
    if exists (select 1
               from   SOLICITA t2, deleted t1
               where  t2.CDOPERLANC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICITA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITEMSOLI"  */
    if exists (select 1
               from   ITEMSOLI t2, deleted t1
               where  t2.CDOPERAPROV = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMSOLI". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MOVPESQUISA"  */
    if exists (select 1
               from   MOVPESQUISA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVPESQUISA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "INCONSPEDIDO"  */
    if exists (select 1
               from   INCONSPEDIDO t2, deleted t1
               where  t2.CDOPERLIBE = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INCONSPEDIDO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGMENSVEND"  */
    if exists (select 1
               from   LOGMENSVEND t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGMENSVEND". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "HISITEMPED"  */
    if exists (select 1
               from   HISITEMPED t2, deleted t1
               where  t2.CDOPEMANHIST = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HISITEMPED". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PREVISAOPDV"  */
    if exists (select 1
               from   PREVISAOPDV t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVISAOPDV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGPONTO"  */
    if exists (select 1
               from   LOGPONTO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGPONTO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGMANUTBD"  */
    if exists (select 1
               from   LOGMANUTBD t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGMANUTBD". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ASSINATAREFA"  */
    if exists (select 1
               from   ASSINATAREFA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ASSINATAREFA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDOPERGERA = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "RESPONSAVBEM"  */
    if exists (select 1
               from   RESPONSAVBEM t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESPONSAVBEM". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGCHEQUEREC"  */
    if exists (select 1
               from   LOGCHEQUEREC t2, deleted t1
               where  t2.CDOPEBORCREC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGCHEQUEREC". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITEMTAREFA"  */
    if exists (select 1
               from   ITEMTAREFA t2, deleted t1
               where  t2.CDOPERRESP = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMTAREFA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MENUOPER"  */
    if exists (select 1
               from   MENUOPER t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MENUOPER". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "VENDAREST"  */
    if exists (select 1
               from   VENDAREST t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDAREST". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "COMANDACXA"  */
    if exists (select 1
               from   COMANDACXA t2, deleted t1
               where  t2.CDOPERCOMCXA = t1.CDOPERADOR)
