       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMANDACXA". 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.CDOPECANCOMC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMANDACXA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "AGRUPAF"  */
    if exists (select 1
               from   AGRUPAF t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGRUPAF". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITEMAUTF"  */
    if exists (select 1
               from   ITEMAUTF t2, deleted t1
               where  t2.CDOPERBAIXA = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMAUTF". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "RESERVAMESA"  */
    if exists (select 1
               from   RESERVAMESA t2, deleted t1
               where  t2.CDOPERINCRES = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESERVAMESA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITEMCOLE"  */
    if exists (select 1
               from   ITEMCOLE t2, deleted t1
               where  t2.CDOPERINCL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMCOLE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "COLETAPR"  */
    if exists (select 1
               from   COLETAPR t2, deleted t1
               where  t2.CDOPERINCL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COLETAPR". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "BREAKDOWN"  */
    if exists (select 1
               from   BREAKDOWN t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BREAKDOWN". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SERVBREAK"  */
    if exists (select 1
               from   SERVBREAK t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVBREAK". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "COMPROPER"  */
    if exists (select 1
               from   COMPROPER t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPROPER". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "TEMPOPREPVOO"  */
    if exists (select 1
               from   TEMPOPREPVOO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TEMPOPREPVOO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "APURPRECO"  */
    if exists (select 1
               from   APURPRECO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURPRECO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "BKGTHREAD"  */
    if exists (select 1
               from   BKGTHREAD t2, deleted t1
               where  t2.CDOPERTHREAD = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BKGTHREAD". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OBSOLIOPER"  */
    if exists (select 1
               from   OBSOLIOPER t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSOLIOPER". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OPERADORURA"  */
    if exists (select 1
               from   OPERADORURA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERADORURA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CTRLACESPER"  */
    if exists (select 1
               from   CTRLACESPER t2, deleted t1
               where  t2.CDOPERPER = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLACESPER". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LISTAINFLAC"  */
    if exists (select 1
               from   LISTAINFLAC t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LISTAINFLAC". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGLISINFLAC"  */
    if exists (select 1
               from   LOGLISINFLAC t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGLISINFLAC". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "GSCRESULTADO"  */
    if exists (select 1
               from   GSCRESULTADO t2, deleted t1
               where  t2.CDGOPEGSCRES = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GSCRESULTADO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "GATILHO"  */
    if exists (select 1
               from   GATILHO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GATILHO". 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.CDOPREVISOLU = 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 "UTILBANCSOLU"  */
    if exists (select 1
               from   UTILBANCSOLU t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "UTILBANCSOLU". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITCOMANDACXA"  */
    if exists (select 1
               from   ITCOMANDACXA t2, deleted t1
               where  t2.CDOPECAITCXA = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITCOMANDACXA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PROCESSO"  */
    if exists (select 1
               from   PROCESSO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROCESSO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PROPOSTAFAS"  */
    if exists (select 1
               from   PROPOSTAFAS t2, deleted t1
               where  t2.CDOPERINCL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPOSTAFAS". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "APURFATUFAS"  */
    if exists (select 1
               from   APURFATUFAS t2, deleted t1
               where  t2.CDOPERAPUR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURFATUFAS". 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.CDOPECANCNF = 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 "LOGACESSO"  */
    if exists (select 1
               from   LOGACESSO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGACESSO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CONTROLSENHA"  */
    if exists (select 1
               from   CONTROLSENHA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTROLSENHA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PARAVEND"  */
    if exists (select 1
               from   PARAVEND t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAVEND". 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.CDOPERESPRJ = 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 "ITEMPEDEXOL"  */
    if exists (select 1
               from   ITEMPEDEXOL t2, deleted t1
               where  t2.CDOPERBAIXA = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPEDEXOL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "HABILOPERPRJ"  */
    if exists (select 1
               from   HABILOPERPRJ t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HABILOPERPRJ". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CRONOPROJETO"  */
    if exists (select 1
               from   CRONOPROJETO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CRONOPROJETO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ORCAEVENTO"  */
    if exists (select 1
               from   ORCAEVENTO t2, deleted t1
               where  t2.CDOPEINCOREV = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAEVENTO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "COMPRA"  */
    if exists (select 1
               from   COMPRA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPRA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITEMPEDIDO"  */
    if exists (select 1
               from   ITEMPEDIDO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPEDIDO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CONSULTADW"  */
    if exists (select 1
               from   CONSULTADW t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSULTADW". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "FATORESUFAT"  */
    if exists (select 1
               from   FATORESUFAT t2, deleted t1
               where  t2.CDGOPERAC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FATORESUFAT". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "FATORESUFAT"  */
    if exists (select 1
               from   FATORESUFAT t2, deleted t1
               where  t2.CDSUPERVISOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FATORESUFAT". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ACERTOORCEVE"  */
    if exists (select 1
               from   ACERTOORCEVE t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACERTOORCEVE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "FERIOPER"  */
    if exists (select 1
               from   FERIOPER t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FERIOPER". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ETLRESULTDES"  */
    if exists (select 1
               from   ETLRESULTDES t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLRESULTDES". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MOVOPAF"  */
    if exists (select 1
               from   MOVOPAF t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOPAF". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "HISTORCAEVEN"  */
    if exists (select 1
               from   HISTORCAEVEN t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HISTORCAEVEN". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ORCAEVENTO"  */
    if exists (select 1
               from   ORCAEVENTO t2, deleted t1
               where  t2.CDOPEJUSTORC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAEVENTO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CTRLAPROVCARD"  */
    if exists (select 1
               from   CTRLAPROVCARD t2, deleted t1
               where  t2.CDOPERLIBECARDMATR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLAPROVCARD". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CTRLAPROVCARD"  */
    if exists (select 1
               from   CTRLAPROVCARD t2, deleted t1
               where  t2.CDOPERAPROCARDMATR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLAPROVCARD". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CTRLAPROVCARD"  */
    if exists (select 1
               from   CTRLAPROVCARD t2, deleted t1
               where  t2.CDOPERAPROCARDFILI = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLAPROVCARD". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGAPROVCARD"  */
    if exists (select 1
               from   LOGAPROVCARD t2, deleted t1
               where  t2.CDOPERLOGAPROVCARD = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGAPROVCARD". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGCALENCARD"  */
    if exists (select 1
               from   LOGCALENCARD t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGCALENCARD". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CONSSOLOL"  */
    if exists (select 1
               from   CONSSOLOL t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSSOLOL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "COMANDAFOS"  */
    if exists (select 1
               from   COMANDAFOS t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMANDAFOS". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "JUSTCONSSOLOL"  */
    if exists (select 1
               from   JUSTCONSSOLOL t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "JUSTCONSSOLOL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OCUPAOPERPROJ"  */
    if exists (select 1
               from   OCUPAOPERPROJ t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OCUPAOPERPROJ". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "EVENTOAEV"  */
    if exists (select 1
               from   EVENTOAEV t2, deleted t1
               where  t2.CDOPERINCL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EVENTOAEV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "AGENDAEVENTO"  */
    if exists (select 1
               from   AGENDAEVENTO t2, deleted t1
               where  t2.CDOPERINCL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENDAEVENTO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "EXECEVENTO"  */
    if exists (select 1
               from   EXECEVENTO t2, deleted t1
               where  t2.CDOPERDEST = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXECEVENTO". 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.CDOPERATEND = 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 "ITEMVALIDATARE"  */
    if exists (select 1
               from   ITEMVALIDATARE t2, deleted t1
               where  t2.CDOPERINCL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMVALIDATARE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "EQUIPETRAB"  */
    if exists (select 1
               from   EQUIPETRAB t2, deleted t1
               where  t2.CDOPERRESP = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQUIPETRAB". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ROPFILIAL"  */
    if exists (select 1
               from   ROPFILIAL t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROPFILIAL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ETL_RESUGER"  */
    if exists (select 1
               from   ETL_RESUGER t2, deleted t1
               where  t2.CDSUPERVISOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUGER". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ETL_RESUGER"  */
    if exists (select 1
               from   ETL_RESUGER t2, deleted t1
               where  t2.CDGOPERAC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUGER". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PROPOSTAFAS"  */
    if exists (select 1
               from   PROPOSTAFAS t2, deleted t1
               where  t2.CDOPERSOLI = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPOSTAFAS". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGPREVCONSPDV"  */
    if exists (select 1
               from   LOGPREVCONSPDV t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGPREVCONSPDV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CONTRATOLINP"  */
    if exists (select 1
               from   CONTRATOLINP t2, deleted t1
               where  t2.CDOPERLINPRESC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTRATOLINP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CONTRATOSERV"  */
    if exists (select 1
               from   CONTRATOSERV t2, deleted t1
               where  t2.CDOPERSERVRESC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTRATOSERV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "TESTCAND"  */
    if exists (select 1
               from   TESTCAND t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TESTCAND". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "APURCOMISOLU"  */
    if exists (select 1
               from   APURCOMISOLU t2, deleted t1
               where  t2.CDOPERAPUR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURCOMISOLU". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITEMCOMISOLU"  */
    if exists (select 1
               from   ITEMCOMISOLU t2, deleted t1
               where  t2.CDOPERRECECOMI = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMCOMISOLU". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "AGENDAREL"  */
    if exists (select 1
               from   AGENDAREL t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENDAREL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ETL_RESUFOOD"  */
    if exists (select 1
               from   ETL_RESUFOOD t2, deleted t1
               where  t2.CDGOPERAC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUFOOD". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ETL_RESUFOOD"  */
    if exists (select 1
               from   ETL_RESUFOOD t2, deleted t1
               where  t2.CDSUPERVISOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUFOOD". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ETL_RESUESTO"  */
    if exists (select 1
               from   ETL_RESUESTO t2, deleted t1
               where  t2.CDGOPERAC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUESTO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ETL_RESUESTO"  */
    if exists (select 1
               from   ETL_RESUESTO t2, deleted t1
               where  t2.CDSUPERVISOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUESTO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOGESTATDIET"  */
    if exists (select 1
               from   LOGESTATDIET t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGESTATDIET". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITEMAUTF"  */
    if exists (select 1
               from   ITEMAUTF t2, deleted t1
               where  t2.CDOPERAPROV = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMAUTF". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ETL_RESUSERV"  */
    if exists (select 1
               from   ETL_RESUSERV t2, deleted t1
               where  t2.CDSUPERVISOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUSERV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ETL_RESUSERV"  */
    if exists (select 1
               from   ETL_RESUSERV t2, deleted t1
               where  t2.CDGOPERAC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUSERV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ORCAEVENTO"  */
    if exists (select 1
               from   ORCAEVENTO t2, deleted t1
               where  t2.CDOPECORORE1 = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAEVENTO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ORCAEVENTO"  */
    if exists (select 1
               from   ORCAEVENTO t2, deleted t1
               where  t2.CDOPECORORE2 = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAEVENTO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "AGENDASALAO"  */
    if exists (select 1
               from   AGENDASALAO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENDASALAO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITEMORCAEVEN"  */
    if exists (select 1
               from   ITEMORCAEVEN t2, deleted t1
               where  t2.CDOPLIBPRORE = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMORCAEVEN". 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.CDOPCFBAIREC = 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 "PERMUTAORE"  */
    if exists (select 1
               from   PERMUTAORE t2, deleted t1
               where  t2.CDOPINCPEORE = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERMUTAORE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "VALORLINP"  */
    if exists (select 1
               from   VALORLINP t2, deleted t1
               where  t2.CDOPERREAJLINP = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VALORLINP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITEMDWOPER"  */
    if exists (select 1
               from   ITEMDWOPER t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMDWOPER". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "DIMEODWOPER"  */
    if exists (select 1
               from   DIMEODWOPER t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIMEODWOPER". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ITEMCOMISOLU"  */
    if exists (select 1
               from   ITEMCOMISOLU t2, deleted t1
               where  t2.CDOPERUTILSOLU = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMCOMISOLU". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "CHT"  */
    if exists (select 1
               from   CHT t2, deleted t1
               where  t2.CDOPERINCL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CHT". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "VALORSERV"  */
    if exists (select 1
               from   VALORSERV t2, deleted t1
               where  t2.CDOPERREAJSERV = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VALORSERV". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ETLDRO"  */
    if exists (select 1
               from   ETLDRO t2, deleted t1
               where  t2.CDSUPERVISOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLDRO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ETLDRO"  */
    if exists (select 1
               from   ETLDRO t2, deleted t1
               where  t2.CDGOPERAC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLDRO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "HISTFILIAL"  */
    if exists (select 1
               from   HISTFILIAL t2, deleted t1
               where  t2.CDGOPERACHIST = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HISTFILIAL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "HISTFILIAL"  */
    if exists (select 1
               from   HISTFILIAL t2, deleted t1
               where  t2.CDSUPERVHIST = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HISTFILIAL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PRODBLOQVND"  */
    if exists (select 1
               from   PRODBLOQVND t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODBLOQVND". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ETLCOMPRASGER"  */
    if exists (select 1
               from   ETLCOMPRASGER t2, deleted t1
               where  t2.CDSUPERVISOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCOMPRASGER". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ETLCOMPRASGER"  */
    if exists (select 1
               from   ETLCOMPRASGER t2, deleted t1
               where  t2.CDGOPERAC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCOMPRASGER". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PROPOSTAFAS"  */
    if exists (select 1
               from   PROPOSTAFAS t2, deleted t1
               where  t2.CDOPERRESP = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPOSTAFAS". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OPERGRPROJETO"  */
    if exists (select 1
               from   OPERGRPROJETO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERGRPROJETO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "ACORDOGRPROJETO"  */
    if exists (select 1
               from   ACORDOGRPROJETO t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACORDOGRPROJETO". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OBSRELACCLIE"  */
    if exists (select 1
               from   OBSRELACCLIE t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSRELACCLIE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PEDCOLGERAL"  */
    if exists (select 1
               from   PEDCOLGERAL t2, deleted t1
               where  t2.CDOPERLANC = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDCOLGERAL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "TRABLIMPEZA"  */
    if exists (select 1
               from   TRABLIMPEZA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TRABLIMPEZA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MOVOPITEMPEDCOL"  */
    if exists (select 1
               from   MOVOPITEMPEDCOL t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOPITEMPEDCOL". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SOLGRACORDOPROJ"  */
    if exists (select 1
               from   SOLGRACORDOPROJ t2, deleted t1
               where  t2.CDOPEREXCLUSAO = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLGRACORDOPROJ". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SOLGRACORDOPROJ"  */
    if exists (select 1
               from   SOLGRACORDOPROJ t2, deleted t1
               where  t2.CDOPERINCLUSAO = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLGRACORDOPROJ". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OPERPRESTCONTA"  */
    if exists (select 1
               from   OPERPRESTCONTA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERPRESTCONTA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OPERTIPOSUPRI"  */
    if exists (select 1
               from   OPERTIPOSUPRI t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERTIPOSUPRI". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OPERAPROVSUPRI"  */
    if exists (select 1
               from   OPERAPROVSUPRI t2, deleted t1
               where  t2.CDGERENTEAPROV = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERAPROVSUPRI". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OPERAPROVSUPRI"  */
    if exists (select 1
               from   OPERAPROVSUPRI t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERAPROVSUPRI". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SOLICITASUPRI"  */
    if exists (select 1
               from   SOLICITASUPRI t2, deleted t1
               where  t2.CDOPERSOLI = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICITASUPRI". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SOLICITASUPRI"  */
    if exists (select 1
               from   SOLICITASUPRI t2, deleted t1
               where  t2.CDOPERBENEF = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICITASUPRI". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SOLICITASUPRI"  */
    if exists (select 1
               from   SOLICITASUPRI t2, deleted t1
               where  t2.CDOPERAPROV = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICITASUPRI". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOTENFE"  */
    if exists (select 1
               from   LOTENFE t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOTENFE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PLANEJAEQUIPE"  */
    if exists (select 1
               from   PLANEJAEQUIPE t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PLANEJAEQUIPE". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PARCLIEFAS"  */
    if exists (select 1
               from   PARCLIEFAS t2, deleted t1
               where  t2.CDGERENTEIMPCLI = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARCLIEFAS". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "OPERPRECOPROP"  */
    if exists (select 1
               from   OPERPRECOPROP t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPERPRECOPROP". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "LOCK_LISTA"  */
    if exists (select 1
               from   LOCK_LISTA t2, deleted t1
               where  t2.CDOPERADOR = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOCK_LISTA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "MAPARESULT"  */
    if exists (select 1
               from   MAPARESULT t2, deleted t1
               where  t2.CDOPERINCL = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPARESULT". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "PRESTACONTA"  */
    if exists (select 1
               from   PRESTACONTA t2, deleted t1
               where  t2.CDOPERCONCPREST = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRESTACONTA". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Cannot delete parent "OPERADOR" if children still exist in "SERVPREST"  */
    if exists (select 1
               from   SERVPREST t2, deleted t1
               where  t2.CDOPERINCLCSA = t1.CDOPERADOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVPREST". Cannot delete parent "OPERADOR".'
          goto error
       end
    
    /*  Delete all children in "FILIOPER"  */
    delete FILIOPER
    from   FILIOPER t2, deleted t1
    where  t2.CDOPERADOR = t1.CDOPERADOR
    
    /*  Delete all children in "OPERGRUPOP"  */
    delete OPERGRUPOP
    from   OPERGRUPOP t2, deleted t1
    where  t2.CDOPERADOR = t1.CDOPERADOR
    
    /*  Delete all children in "PROGOPEROPC"  */
    delete PROGOPEROPC
    from   PROGOPEROPC t2, deleted t1
    where  t2.CDOPERADOR = t1.CDOPERADOR
    
    /*  Delete all children in "LOGTRMOVCONS"  */
    delete LOGTRMOVCONS
    from   LOGTRMOVCONS t2, deleted t1
    where  t2.CDOPERADOR = t1.CDOPERADOR
    
    /*  Delete all children in "HABILOPER"  */
    delete HABILOPER
    from   HABILOPER t2, deleted t1
    where  t2.CDOPERADOR = t1.CDOPERADOR
    
    /*  Delete all children in "NATUCONTOPER"  */
    delete NATUCONTOPER
    from   NATUCONTOPER t2, deleted t1
    where  t2.CDOPERADOR = t1.CDOPERADOR
    
    /*  Delete all children in "NATUASSUOPER"  */
    delete NATUASSUOPER
    from   NATUASSUOPER t2, deleted t1
    where  t2.CDOPERADOR = t1.CDOPERADOR
    
    /*  Delete all children in "OPERALMOX"  */
    delete OPERALMOX
    from   OPERALMOX t2, deleted t1
    where  t2.CDOPERADOR = t1.CDOPERADOR
    
    /*  Delete all children in "OPERLESTOQ"  */
    delete OPERLESTOQ
    from   OPERLESTOQ t2, deleted t1
    where  t2.CDOPERADOR = t1.CDOPERADOR

    return

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

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

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

    
    /*  Cannot delete parent "ORCAEVENTO" if children still exist in "OBSINTORCAEV"  */
    if exists (select 1
               from   OBSINTORCAEV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
                and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSINTORCAEV". Cannot delete parent "ORCAEVENTO".'
          goto error
       end
    
    /*  Cannot delete parent "ORCAEVENTO" if children still exist in "PEDIDO"  */
    if exists (select 1
               from   PEDIDO t2, deleted t1
               where  t2.CDFILIALORC = t1.CDFILIAL
                and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
                and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDO". Cannot delete parent "ORCAEVENTO".'
          goto error
       end
    
    /*  Cannot delete parent "ORCAEVENTO" if children still exist in "NFRELACORC"  */
    if exists (select 1
               from   NFRELACORC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
                and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFRELACORC". Cannot delete parent "ORCAEVENTO".'
          goto error
       end
    
    /*  Cannot delete parent "ORCAEVENTO" if children still exist in "AGENDASALAO"  */
    if exists (select 1
               from   AGENDASALAO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
                and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENDASALAO". Cannot delete parent "ORCAEVENTO".'
          goto error
       end
    
    /*  Cannot delete parent "ORCAEVENTO" if children still exist in "CONTRATOORCEVE"  */
    if exists (select 1
               from   CONTRATOORCEVE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
                and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTRATOORCEVE". Cannot delete parent "ORCAEVENTO".'
          goto error
       end
    
    /*  Cannot delete parent "ORCAEVENTO" if children still exist in "PERMUTAORE"  */
    if exists (select 1
               from   PERMUTAORE t2, deleted t1
               where  t2.CDFILIALOREV = t1.CDFILIAL
                and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
                and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERMUTAORE". Cannot delete parent "ORCAEVENTO".'
          goto error
       end
    
    /*  Cannot delete parent "ORCAEVENTO" if children still exist in "DEVOLORCAEVEN"  */
    if exists (select 1
               from   DEVOLORCAEVEN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
                and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DEVOLORCAEVEN". Cannot delete parent "ORCAEVENTO".'
          goto error
       end
    
    /*  Delete all children in "ITEMORCAEVEN"  */
    delete ITEMORCAEVEN
    from   ITEMORCAEVEN t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
     and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
    
    /*  Delete all children in "ITMAOOBRAORCA"  */
    delete ITMAOOBRAORCA
    from   ITMAOOBRAORCA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
     and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
    
    /*  Delete all children in "DEGUSTACAO"  */
    delete DEGUSTACAO
    from   DEGUSTACAO t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
     and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
    
    /*  Delete all children in "OBSEXTORCAEV"  */
    delete OBSEXTORCAEV
    from   OBSEXTORCAEV t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
     and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
    
    /*  Delete all children in "ACERTOORCEVE"  */
    delete ACERTOORCEVE
    from   ACERTOORCEVE t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
     and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
    
    /*  Delete all children in "ENDCLIORCEVE"  */
    delete ENDCLIORCEVE
    from   ENDCLIORCEVE t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
     and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE
    
    /*  Delete all children in "HISTORCAEVEN"  */
    delete HISTORCAEVEN
    from   HISTORCAEVEN t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORCAEVENTO = t1.NRORCAEVENTO
     and   t2.NRSEQORCAEVE = t1.NRSEQORCAEVE

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "ORDPRODUCAO" if children still exist in "ORDPRODUCAO"  */
    if exists (select 1
               from   ORDPRODUCAO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRORDPRODPAI = t1.NRORDPROD
                and   t2.NRSUBOPPAI = t1.NRSUBORDPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORDPRODUCAO". Cannot delete parent "ORDPRODUCAO".'
          goto error
       end
    
    /*  Cannot delete parent "ORDPRODUCAO" if children still exist in "RESERPRODPED"  */
    if exists (select 1
               from   RESERPRODPED t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRORDPROD = t1.NRORDPROD
                and   t2.NRSUBORDPROD = t1.NRSUBORDPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESERPRODPED". Cannot delete parent "ORDPRODUCAO".'
          goto error
       end
    
    /*  Cannot delete parent "ORDPRODUCAO" if children still exist in "SERVBREAK"  */
    if exists (select 1
               from   SERVBREAK t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRORDPROD = t1.NRORDPROD
                and   t2.NRSUBORDPROD = t1.NRSUBORDPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVBREAK". Cannot delete parent "ORDPRODUCAO".'
          goto error
       end
    
    /*  Cannot delete parent "ORDPRODUCAO" if children still exist in "ORDPRODUCAO"  */
    if exists (select 1
               from   ORDPRODUCAO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRORDPRODCON = t1.NRORDPROD
                and   t2.NRSUBOPCON = t1.NRSUBORDPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORDPRODUCAO". Cannot delete parent "ORDPRODUCAO".'
          goto error
       end
    
    /*  Cannot delete parent "ORDPRODUCAO" if children still exist in "INSUPEDIDOP"  */
    if exists (select 1
               from   INSUPEDIDOP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRORDPROD = t1.NRORDPROD
                and   t2.NRSUBORDPROD = t1.NRSUBORDPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSUPEDIDOP". Cannot delete parent "ORDPRODUCAO".'
          goto error
       end
    
    /*  Delete all children in "AUDITORIA"  */
    delete AUDITORIA
    from   AUDITORIA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORDPROD = t1.NRORDPROD
     and   t2.NRSUBORDPROD = t1.NRSUBORDPROD
    
    /*  Delete all children in "ETAFABRICOP"  */
    delete ETAFABRICOP
    from   ETAFABRICOP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORDPROD = t1.NRORDPROD
     and   t2.NRSUBORDPROD = t1.NRSUBORDPROD
    
    /*  Delete all children in "SUBPRETAPAOP"  */
    delete SUBPRETAPAOP
    from   SUBPRETAPAOP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORDPROD = t1.NRORDPROD
     and   t2.NRSUBORDPROD = t1.NRSUBORDPROD
    
    /*  Delete all children in "PEDIDOP"  */
    delete PEDIDOP
    from   PEDIDOP t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORDPROD = t1.NRORDPROD
     and   t2.NRSUBORDPROD = t1.NRSUBORDPROD

    return

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

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

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

    
    /*  Cannot delete parent "PACIENTE" if children still exist in "HABALIMT"  */
    if exists (select 1
               from   HABALIMT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HABALIMT". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "INTERNAC"  */
    if exists (select 1
               from   INTERNAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INTERNAC". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "CONSPACI"  */
    if exists (select 1
               from   CONSPACI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSPACI". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "INQUERPACI"  */
    if exists (select 1
               from   INQUERPACI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INQUERPACI". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "MARCACAO"  */
    if exists (select 1
               from   MARCACAO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MARCACAO". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "ATENREALIZ"  */
    if exists (select 1
               from   ATENREALIZ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ATENREALIZ". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "RETOPROG"  */
    if exists (select 1
               from   RETOPROG t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RETOPROG". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "LISTAESPR"  */
    if exists (select 1
               from   LISTAESPR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LISTAESPR". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "EXTRMARC"  */
    if exists (select 1
               from   EXTRMARC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXTRMARC". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "CONSMEDI"  */
    if exists (select 1
               from   CONSMEDI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSMEDI". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" 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)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVINTE". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "PRESDIET"  */
    if exists (select 1
               from   PRESDIET t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRESDIET". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "FREQCONSALIM"  */
    if exists (select 1
               from   FREQCONSALIM t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FREQCONSALIM". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "DESCHABIPACI"  */
    if exists (select 1
               from   DESCHABIPACI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESCHABIPACI". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "DESCINTOPACI"  */
    if exists (select 1
               from   DESCINTOPACI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESCINTOPACI". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "INTOALIM"  */
    if exists (select 1
               from   INTOALIM t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INTOALIM". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "OBSERVPACI"  */
    if exists (select 1
               from   OBSERVPACI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSERVPACI". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "ATENHOSP"  */
    if exists (select 1
               from   ATENHOSP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ATENHOSP". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "CONSNUTR"  */
    if exists (select 1
               from   CONSNUTR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSNUTR". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "PARTATIVSALA"  */
    if exists (select 1
               from   PARTATIVSALA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARTATIVSALA". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "TRIAGDOAC"  */
    if exists (select 1
               from   TRIAGDOAC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIRECEPT = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TRIAGDOAC". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "ITAGENFUNC"  */
    if exists (select 1
               from   ITAGENFUNC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITAGENFUNC". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "APURFATUGH"  */
    if exists (select 1
               from   APURFATUGH t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIENTE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURFATUGH". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Cannot delete parent "PACIENTE" if children still exist in "PACIENTE"  */
    if exists (select 1
               from   PACIENTE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDPACIMAE = t1.CDPACIENTE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PACIENTE". Cannot delete parent "PACIENTE".'
          goto error
       end
    
    /*  Delete all children in "ENDEPACI"  */
    delete ENDEPACI
    from   ENDEPACI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
    
    /*  Delete all children in "INDIPACI"  */
    delete INDIPACI
    from   INDIPACI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
    
    /*  Delete all children in "RESPPACI"  */
    delete RESPPACI
    from   RESPPACI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
    
    /*  Delete all children in "MEDICAPACI"  */
    delete MEDICAPACI
    from   MEDICAPACI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE

    return

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

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

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

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

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "PEDCOLETA" if children still exist in "COLETAPR"  */
    if exists (select 1
               from   COLETAPR t2, deleted t1
               where  t2.CDREGIAO = t1.CDREGIAO
                and   t2.CDFORNECED = t1.CDFORNECED
                and   t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRPEDCOLETA = t1.NRPEDCOLETA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COLETAPR". Cannot delete parent "PEDCOLETA".'
          goto error
       end
    
    /*  Delete all children in "ITEMPEDCOL"  */
    delete ITEMPEDCOL
    from   ITEMPEDCOL t2, deleted t1
    where  t2.CDREGIAO = t1.CDREGIAO
     and   t2.CDFORNECED = t1.CDFORNECED
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRPEDCOLETA = t1.NRPEDCOLETA

    return

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

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

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

    
    /*  Cannot delete parent "PEDIDO" if children still exist in "PARPEDIDO"  */
    if exists (select 1
               from   PARPEDIDO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRPEDIDO = t1.NRPEDIDO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARPEDIDO". Cannot delete parent "PEDIDO".'
          goto error
       end
    
    /*  Cannot delete parent "PEDIDO" 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)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INCONSPEDIDO". Cannot delete parent "PEDIDO".'
          goto error
       end
    
    /*  Cannot delete parent "PEDIDO" if children still exist in "SOLICITA"  */
    if exists (select 1
               from   SOLICITA t2, deleted t1
               where  t2.CDFILIALPED = t1.CDFILIAL
                and   t2.NRPEDIDO = t1.NRPEDIDO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICITA". Cannot delete parent "PEDIDO".'
          goto error
       end
    
    /*  Cannot delete parent "PEDIDO" if children still exist in "GRADEDISTR"  */
    if exists (select 1
               from   GRADEDISTR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRPEDIDO = t1.NRPEDIDO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GRADEDISTR". Cannot delete parent "PEDIDO".'
          goto error
       end
    
    /*  Delete all children in "ITEMPEDIDO"  */
    delete ITEMPEDIDO
    from   ITEMPEDIDO t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRPEDIDO = t1.NRPEDIDO
    
    /*  Delete all children in "PRAZOPGTOPED"  */
    delete PRAZOPGTOPED
    from   PRAZOPGTOPED t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRPEDIDO = t1.NRPEDIDO
    
    /*  Delete all children in "OBSPEDIDO"  */
    delete OBSPEDIDO
    from   OBSPEDIDO t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRPEDIDO = t1.NRPEDIDO

    return

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

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

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

    
    /*  Delete all children in "RATECOMILINP"  */
    delete RATECOMILINP
    from   RATECOMILINP t2, deleted t1
    where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
     and   t2.NRVERPROPFAS = t1.NRVERPROPFAS
     and   t2.NRLINPPEDP = t1.NRLINPPEDP
     and   t2.CDCLIENTE = t1.CDCLIENTE

    return

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

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

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

    
    /*  Cannot delete parent "PERIENTR" if children still exist in "PERFILPROD"  */
    if exists (select 1
               from   PERFILPROD t2, deleted t1
               where  t2.CDPERIENTR = t1.CDPERIENTR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERFILPROD". Cannot delete parent "PERIENTR".'
          goto error
       end
    
    /*  Cannot delete parent "PERIENTR" if children still exist in "CALECOMP"  */
    if exists (select 1
               from   CALECOMP t2, deleted t1
               where  t2.CDPERIENTR = t1.CDPERIENTR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CALECOMP". Cannot delete parent "PERIENTR".'
          goto error
       end
    
    /*  Cannot delete parent "PERIENTR" if children still exist in "AGENTARE"  */
    if exists (select 1
               from   AGENTARE t2, deleted t1
               where  t2.CDPERIENTR = t1.CDPERIENTR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENTARE". Cannot delete parent "PERIENTR".'
          goto error
       end
    
    /*  Cannot delete parent "PERIENTR" if children still exist in "REFORFIL"  */
    if exists (select 1
               from   REFORFIL t2, deleted t1
               where  t2.CDPERIENTR = t1.CDPERIENTR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REFORFIL". Cannot delete parent "PERIENTR".'
          goto error
       end
    
    /*  Cannot delete parent "PERIENTR" if children still exist in "REFOREG"  */
    if exists (select 1
               from   REFOREG t2, deleted t1
               where  t2.CDPERIENTR = t1.CDPERIENTR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REFOREG". Cannot delete parent "PERIENTR".'
          goto error
       end
    
    /*  Cannot delete parent "PERIENTR" if children still exist in "ROTATRANS"  */
    if exists (select 1
               from   ROTATRANS t2, deleted t1
               where  t2.CDPERIENTR = t1.CDPERIENTR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROTATRANS". Cannot delete parent "PERIENTR".'
          goto error
       end
    
    /*  Cannot delete parent "PERIENTR" if children still exist in "CLIENFILIAL"  */
    if exists (select 1
               from   CLIENFILIAL t2, deleted t1
               where  t2.CDPERIENTR = t1.CDPERIENTR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENFILIAL". Cannot delete parent "PERIENTR".'
          goto error
       end
    
    /*  Cannot delete parent "PERIENTR" if children still exist in "ROTATRANS"  */
    if exists (select 1
               from   ROTATRANS t2, deleted t1
               where  t2.CDPERIENTEN = t1.CDPERIENTR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROTATRANS". Cannot delete parent "PERIENTR".'
          goto error
       end
    
    /*  Delete all children in "DIASENTR"  */
    delete DIASENTR
    from   DIASENTR t2, deleted t1
    where  t2.CDPERIENTR = t1.CDPERIENTR

    return

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

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

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

    
    /*  Cannot delete parent "PESSOA" if children still exist in "REGISCATRACA"  */
    if exists (select 1
               from   REGISCATRACA t2, deleted t1
               where  t2.NRPESSOA = t1.NRPESSOA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REGISCATRACA". Cannot delete parent "PESSOA".'
          goto error
       end
    
    /*  Delete all children in "FOTODIGITAL"  */
    delete FOTODIGITAL
    from   FOTODIGITAL t2, deleted t1
    where  t2.NRPESSOA = t1.NRPESSOA
    
    /*  Delete all children in "FOTOPESSOA"  */
    delete FOTOPESSOA
    from   FOTOPESSOA t2, deleted t1
    where  t2.NRPESSOA = t1.NRPESSOA
    
    /*  Delete all children in "DIGITALCATRACA"  */
    delete DIGITALCATRACA
    from   DIGITALCATRACA t2, deleted t1
    where  t2.NRPESSOA = t1.NRPESSOA

    return

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

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

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

    
    /*  Cannot delete parent "PLANATIVTARE" if children still exist in "RELATIVTARE"  */
    if exists (select 1
               from   RELATIVTARE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA
                and   t2.NRPLATIVTARE = t1.NRPLATIVTARE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELATIVTARE". Cannot delete parent "PLANATIVTARE".'
          goto error
       end
    
    /*  Cannot delete parent "PLANATIVTARE" if children still exist in "RELATIVTARE"  */
    if exists (select 1
               from   RELATIVTARE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTARERELAC = t1.NRTAREFA
                and   t2.NRATIVRELAC = t1.NRPLATIVTARE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELATIVTARE". Cannot delete parent "PLANATIVTARE".'
          goto error
       end
    
    /*  Cannot delete parent "PLANATIVTARE" if children still exist in "APROATIVTARE"  */
    if exists (select 1
               from   APROATIVTARE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA
                and   t2.NRPLATIVTARE = t1.NRPLATIVTARE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APROATIVTARE". Cannot delete parent "PLANATIVTARE".'
          goto error
       end
    
    /*  Cannot delete parent "PLANATIVTARE" if children still exist in "RESPPLATIV"  */
    if exists (select 1
               from   RESPPLATIV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA
                and   t2.NRPLATIVTARE = t1.NRPLATIVTARE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESPPLATIV". Cannot delete parent "PLANATIVTARE".'
          goto error
       end
    
    /*  Cannot delete parent "PLANATIVTARE" if children still exist in "INSPLANATIV"  */
    if exists (select 1
               from   INSPLANATIV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA
                and   t2.NRPLATIVTARE = t1.NRPLATIVTARE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPLANATIV". Cannot delete parent "PLANATIVTARE".'
          goto error
       end
    
    /*  Cannot delete parent "PLANATIVTARE" if children still exist in "ARQATIVTARE"  */
    if exists (select 1
               from   ARQATIVTARE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA
                and   t2.NRPLATIVTARE = t1.NRPLATIVTARE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ARQATIVTARE". Cannot delete parent "PLANATIVTARE".'
          goto error
       end
    
    /*  Cannot delete parent "PLANATIVTARE" if children still exist in "CRONOPROJETO"  */
    if exists (select 1
               from   CRONOPROJETO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA
                and   t2.NRPLATIVTARE = t1.NRPLATIVTARE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CRONOPROJETO". Cannot delete parent "PLANATIVTARE".'
          goto error
       end
    
    /*  Cannot delete parent "PLANATIVTARE" 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.NRPLATIVTARE = t1.NRPLATIVTARE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VALIDATAREFA". Cannot delete parent "PLANATIVTARE".'
          goto error
       end
    
    /*  Delete all children in "DESCPLATIV"  */
    delete DESCPLATIV
    from   DESCPLATIV t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRTAREFA = t1.NRTAREFA
     and   t2.NRPLATIVTARE = t1.NRPLATIVTARE
    
    /*  Delete all children in "PLATVRELITTR"  */
    delete PLATVRELITTR
    from   PLATVRELITTR t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRTAREFA = t1.NRTAREFA
     and   t2.NRPLATIVTARE = t1.NRPLATIVTARE

    return

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

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

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

    
    /*  Cannot delete parent "PLANOVOO" if children still exist in "DELIVERY"  */
    if exists (select 1
               from   DELIVERY t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA
                and   t2.NRVOO = t1.NRVOO
                and   t2.CDTRECHO = t1.CDTRECHO
                and   t2.DTHRPLAVOOPV = t1.DTHRPLAVOOPV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DELIVERY". Cannot delete parent "PLANOVOO".'
          goto error
       end
    
    /*  Cannot delete parent "PLANOVOO" if children still exist in "PREVCOMPRA"  */
    if exists (select 1
               from   PREVCOMPRA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIACOMP = t1.CDCIA
                and   t2.NRVOOCOMP = t1.NRVOO
                and   t2.CDTRECHOCOMP = t1.CDTRECHO
                and   t2.DTPREVCOMP = t1.DTHRPLAVOOPV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCOMPRA". Cannot delete parent "PLANOVOO".'
          goto error
       end
    
    /*  Cannot delete parent "PLANOVOO" if children still exist in "DELIVERYCANC"  */
    if exists (select 1
               from   DELIVERYCANC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA
                and   t2.NRVOO = t1.NRVOO
                and   t2.CDTRECHO = t1.CDTRECHO
                and   t2.DTHRPLAVOOPV = t1.DTHRPLAVOOPV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DELIVERYCANC". Cannot delete parent "PLANOVOO".'
          goto error
       end
    
    /*  Delete all children in "PREVISAOPAX"  */
    delete PREVISAOPAX
    from   PREVISAOPAX t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDCIA = t1.CDCIA
     and   t2.NRVOO = t1.NRVOO
     and   t2.CDTRECHO = t1.CDTRECHO
     and   t2.DTHRPLAVOOPV = t1.DTHRPLAVOOPV
    
    /*  Delete all children in "SERVPLANO"  */
    delete SERVPLANO
    from   SERVPLANO t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDCIA = t1.CDCIA
     and   t2.NRVOO = t1.NRVOO
     and   t2.CDTRECHO = t1.CDTRECHO
     and   t2.DTHRPLAVOOPV = t1.DTHRPLAVOOPV

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "PRATO" if children still exist in "FOTOPRAFIL"  */
    if exists (select 1
               from   FOTOPRAFIL t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FOTOPRAFIL". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "FOTOPRASER"  */
    if exists (select 1
               from   FOTOPRASER t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FOTOPRASER". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "PRATFILI"  */
    if exists (select 1
               from   PRATFILI t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRATFILI". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "PRATSERV"  */
    if exists (select 1
               from   PRATSERV t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRATSERV". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "FOTOPRAPAD"  */
    if exists (select 1
               from   FOTOPRAPAD t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FOTOPRAPAD". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "PRATPADR"  */
    if exists (select 1
               from   PRATPADR t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRATPADR". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "RECEFILI"  */
    if exists (select 1
               from   RECEFILI t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEFILI". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "RECESERV"  */
    if exists (select 1
               from   RECESERV t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECESERV". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "COMPCARD"  */
    if exists (select 1
               from   COMPCARD t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPCARD". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "MEDIPRATO"  */
    if exists (select 1
               from   MEDIPRATO t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MEDIPRATO". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "HABALIMT"  */
    if exists (select 1
               from   HABALIMT t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HABALIMT". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "CARDADIA"  */
    if exists (select 1
               from   CARDADIA t2, deleted t1
               where  t2.CDFILAUXPRRI = t1.CDFILAUXPRAT
                and   t2.CDMAIORPRERI = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDADIA". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "CARDADIA"  */
    if exists (select 1
               from   CARDADIA t2, deleted t1
               where  t2.CDFILAUXPRSL = t1.CDFILAUXPRAT
                and   t2.CDMAIORPRESL = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDADIA". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "PREPPRASER"  */
    if exists (select 1
               from   PREPPRASER t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREPPRASER". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "PREPPRAFIL"  */
    if exists (select 1
               from   PREPPRAFIL t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREPPRAFIL". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "PREPPRAPAD"  */
    if exists (select 1
               from   PREPPRAPAD t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREPPRAPAD". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "RECDIEPAD"  */
    if exists (select 1
               from   RECDIEPAD t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECDIEPAD". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "FREQALIM"  */
    if exists (select 1
               from   FREQALIM t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FREQALIM". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "RECEPADR"  */
    if exists (select 1
               from   RECEPADR t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEPADR". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "UTILPRATSEM"  */
    if exists (select 1
               from   UTILPRATSEM t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "UTILPRATSEM". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "SERVTRAN"  */
    if exists (select 1
               from   SERVTRAN t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVTRAN". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "CONFESCO"  */
    if exists (select 1
               from   CONFESCO t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONFESCO". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "PREVRETIPRAT"  */
    if exists (select 1
               from   PREVRETIPRAT t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVRETIPRAT". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "ITEMRETIPRAT"  */
    if exists (select 1
               from   ITEMRETIPRAT t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMRETIPRAT". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "ITEQUIVALE"  */
    if exists (select 1
               from   ITEQUIVALE t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEQUIVALE". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "COMCADIAPACI"  */
    if exists (select 1
               from   COMCADIAPACI t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMCADIAPACI". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "ITINQUERPACI"  */
    if exists (select 1
               from   ITINQUERPACI t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITINQUERPACI". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "CARDCONSNUTR"  */
    if exists (select 1
               from   CARDCONSNUTR t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDCONSNUTR". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "EFETDIET"  */
    if exists (select 1
               from   EFETDIET t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFETDIET". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "PARAMPLANEJA"  */
    if exists (select 1
               from   PARAMPLANEJA t2, deleted t1
               where  t2.CDFILCONSFIX = t1.CDFILAUXPRAT
                and   t2.CDPRACONSFIX = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPLANEJA". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "PARAMPLANEJA"  */
    if exists (select 1
               from   PARAMPLANEJA t2, deleted t1
               where  t2.CDFILCONSPER = t1.CDFILAUXPRAT
                and   t2.CDPRACONSPER = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPLANEJA". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "ITEMRETI"  */
    if exists (select 1
               from   ITEMRETI t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMRETI". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "PREVRETI"  */
    if exists (select 1
               from   PREVRETI t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVRETI". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "COMPCARDPME"  */
    if exists (select 1
               from   COMPCARDPME t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATO = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPCARDPME". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "COMPCARD"  */
    if exists (select 1
               from   COMPCARD t2, deleted t1
               where  t2.CDFILAUXPRATMAT = t1.CDFILAUXPRAT
                and   t2.CDPRATOMAT = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPCARD". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Cannot delete parent "PRATO" if children still exist in "PARADIETO"  */
    if exists (select 1
               from   PARADIETO t2, deleted t1
               where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
                and   t2.CDPRATOENTPAR = t1.CDPRATO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARADIETO". Cannot delete parent "PRATO".'
          goto error
       end
    
    /*  Delete all children in "LIMPEPRA"  */
    delete LIMPEPRA
    from   LIMPEPRA t2, deleted t1
    where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
     and   t2.CDPRATO = t1.CDPRATO
    
    /*  Delete all children in "RESTOPCA"  */
    delete RESTOPCA
    from   RESTOPCA t2, deleted t1
    where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
     and   t2.CDPRATO = t1.CDPRATO
    
    /*  Delete all children in "FREQOPCA"  */
    delete FREQOPCA
    from   FREQOPCA t2, deleted t1
    where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
     and   t2.CDPRATO = t1.CDPRATO
    
    /*  Delete all children in "RESTPRAT"  */
    delete RESTPRAT
    from   RESTPRAT t2, deleted t1
    where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
     and   t2.CDPRATO = t1.CDPRATO
    
    /*  Delete all children in "FREQPRAT"  */
    delete FREQPRAT
    from   FREQPRAT t2, deleted t1
    where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
     and   t2.CDPRATO = t1.CDPRATO

    return

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

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

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

    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "PRECOUTI"  */
    if exists (select 1
               from   PRECOUTI t2, deleted t1
               where  t2.CDPRECOBASE = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRECOUTI". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "VENDEDOR"  */
    if exists (select 1
               from   VENDEDOR t2, deleted t1
               where  t2.CDPRECOBASE = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDEDOR". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "PRECOBASECLI"  */
    if exists (select 1
               from   PRECOBASECLI t2, deleted t1
               where  t2.CDPRECOBASE = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRECOBASECLI". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "PEDIDO"  */
    if exists (select 1
               from   PEDIDO t2, deleted t1
               where  t2.CDPRECOBASE = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDO". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "TIPOOPERAC"  */
    if exists (select 1
               from   TIPOOPERAC t2, deleted t1
               where  t2.CDPRECOBASE = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOOPERAC". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "TROCA"  */
    if exists (select 1
               from   TROCA t2, deleted t1
               where  t2.CDPRECOBASE = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TROCA". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "ITDESPREBASE"  */
    if exists (select 1
               from   ITDESPREBASE t2, deleted t1
               where  t2.CDPRECOBASE = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITDESPREBASE". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "PROPOSTA"  */
    if exists (select 1
               from   PROPOSTA t2, deleted t1
               where  t2.CDPRECOBASE = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPOSTA". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "ORCAEVENTO"  */
    if exists (select 1
               from   ORCAEVENTO t2, deleted t1
               where  t2.CDPRECOBASE = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAEVENTO". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "ORCAEVENTO"  */
    if exists (select 1
               from   ORCAEVENTO t2, deleted t1
               where  t2.CDPREBAMOBRA = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAEVENTO". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "PARAMORCAEVE"  */
    if exists (select 1
               from   PARAMORCAEVE t2, deleted t1
               where  t2.CDPREBAMOBRA = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMORCAEVE". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "ACERTOORCEVE"  */
    if exists (select 1
               from   ACERTOORCEVE t2, deleted t1
               where  t2.CDPRBAQUEBRA = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACERTOORCEVE". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "ACERTOORCEVE"  */
    if exists (select 1
               from   ACERTOORCEVE t2, deleted t1
               where  t2.CDPRBAHREXTR = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACERTOORCEVE". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "ACERTOORCEVE"  */
    if exists (select 1
               from   ACERTOORCEVE t2, deleted t1
               where  t2.CDPRBASOBRA = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACERTOORCEVE". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "ACERTOORCEVE"  */
    if exists (select 1
               from   ACERTOORCEVE t2, deleted t1
               where  t2.CDPRBAEXTRA = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACERTOORCEVE". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "PARAMORCAEVE"  */
    if exists (select 1
               from   PARAMORCAEVE t2, deleted t1
               where  t2.CDPBASEPRATA = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMORCAEVE". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Cannot delete parent "PRECOBASE" if children still exist in "PARAMORCAEVE"  */
    if exists (select 1
               from   PARAMORCAEVE t2, deleted t1
               where  t2.CDPBASEREPMT = t1.CDPRECOBASE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMORCAEVE". Cannot delete parent "PRECOBASE".'
          goto error
       end
    
    /*  Delete all children in "ITEMPREBASE"  */
    delete ITEMPREBASE
    from   ITEMPREBASE t2, deleted t1
    where  t2.CDPRECOBASE = t1.CDPRECOBASE

    return

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

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

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

    
    /*  Delete all children in "SISTPRECOPROP"  */
    delete SISTPRECOPROP
    from   SISTPRECOPROP t2, deleted t1
    where  t2.CDPRECOPROP = t1.CDPRECOPROP
    
    /*  Delete all children in "USUPRECOPROP"  */
    delete USUPRECOPROP
    from   USUPRECOPROP t2, deleted t1
    where  t2.CDPRECOPROP = t1.CDPRECOPROP
    
    /*  Delete all children in "OPERPRECOPROP"  */
    delete OPERPRECOPROP
    from   OPERPRECOPROP t2, deleted t1
    where  t2.CDPRECOPROP = t1.CDPRECOPROP

    return

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

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

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

    
    /*  Delete all children in "PROPRECOSE"  */
    delete PROPRECOSE
    from   PROPRECOSE t2, deleted t1
    where  t2.NRTABPRECO = t1.NRTABPRECO
     and   t2.CDSERVICO = t1.CDSERVICO
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDCIA = t1.CDCIA
     and   t2.NRSEQPRECO = t1.NRSEQPRECO

    return

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

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

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

    
    /*  Delete all children in "ITCUSTOSERV"  */
    delete ITCUSTOSERV
    from   ITCUSTOSERV t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDCIA = t1.CDCIA
     and   t2.CDMANCIA = t1.CDMANCIA
     and   t2.IDTPMANUAL = t1.IDTPMANUAL
     and   t2.CDSERVICO = t1.CDSERVICO
     and   t2.CDSUBGRUPO = t1.CDSUBGRUPO
     and   t2.DTCUSTOSERV = t1.DTCUSTOSERV

    return

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

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

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

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

    return

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

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

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

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

    return

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

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

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

    
    /*  Delete all children in "SERVPREVPAXPROD"  */
    delete SERVPREVPAXPROD
    from   SERVPREVPAXPROD t2, deleted t1
    where  t2.CDCLASSE = t1.CDCLASSE
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDCIA = t1.CDCIA
     and   t2.NRVOO = t1.NRVOO
     and   t2.CDTRECHO = t1.CDTRECHO
     and   t2.DTVOO = t1.DTVOO

    return

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

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

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

    
    /*  Delete all children in "PREVRETIPRAT"  */
    delete PREVRETIPRAT
    from   PREVRETIPRAT t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDSERVFILI = t1.CDSERVFILI
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.DTPREVRETI = t1.DTPREVRETI
     and   t2.NRREQUEXTR = t1.NRREQUEXTR
     and   t2.IDPREVRETI = t1.IDPREVRETI
     and   t2.DTLIMITERETI = t1.DTLIMITERETI
     and   t2.HRLIMITERETI = t1.HRLIMITERETI
     and   t2.NRPRPREVRETI = t1.NRPRPREVRETI

    return

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

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

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

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

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "PROCESSO" if children still exist in "TAREFA"  */
    if exists (select 1
               from   TAREFA t2, deleted t1
               where  t2.CDPROCESSO = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAREFA". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "FASETRAMDEST"  */
    if exists (select 1
               from   FASETRAMDEST t2, deleted t1
               where  t2.CDPROCESGRUP = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FASETRAMDEST". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "PLANATIVTARE"  */
    if exists (select 1
               from   PLANATIVTARE t2, deleted t1
               where  t2.CDPROCESSO = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PLANATIVTARE". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "PROCINTECLI"  */
    if exists (select 1
               from   PROCINTECLI t2, deleted t1
               where  t2.CDPROCESSO = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROCINTECLI". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "PROCINTEFOR"  */
    if exists (select 1
               from   PROCINTEFOR t2, deleted t1
               where  t2.CDPROCESSO = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROCINTEFOR". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "PROCINTEPRO"  */
    if exists (select 1
               from   PROCINTEPRO t2, deleted t1
               where  t2.CDPROCESSO = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROCINTEPRO". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "PARFILIALSAC"  */
    if exists (select 1
               from   PARFILIALSAC t2, deleted t1
               where  t2.CDGRUPPRDEVO = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALSAC". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "INSPPROCESSO"  */
    if exists (select 1
               from   INSPPROCESSO t2, deleted t1
               where  t2.CDPROCESSO = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPPROCESSO". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "SERVCONTRAT"  */
    if exists (select 1
               from   SERVCONTRAT t2, deleted t1
               where  t2.CDPROCESSO = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCONTRAT". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "ASSUNTO"  */
    if exists (select 1
               from   ASSUNTO t2, deleted t1
               where  t2.CDPROCESSO = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ASSUNTO". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "QUALIFPROC"  */
    if exists (select 1
               from   QUALIFPROC t2, deleted t1
               where  t2.CDPROCESSO = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "QUALIFPROC". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "PARFILIALGEP"  */
    if exists (select 1
               from   PARFILIALGEP t2, deleted t1
               where  t2.CDPROCMONIT = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALGEP". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "ETLRESULTDES"  */
    if exists (select 1
               from   ETLRESULTDES t2, deleted t1
               where  t2.CDPROCESSO = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLRESULTDES". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "PARFILIALGEP"  */
    if exists (select 1
               from   PARFILIALGEP t2, deleted t1
               where  t2.CDPROCATEINT = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALGEP". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "PARFILIALGEP"  */
    if exists (select 1
               from   PARFILIALGEP t2, deleted t1
               where  t2.CDPROCATEDES = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALGEP". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "PLANEJAEQUIPE"  */
    if exists (select 1
               from   PLANEJAEQUIPE t2, deleted t1
               where  t2.CDPROCESSO = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PLANEJAEQUIPE". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Cannot delete parent "PROCESSO" if children still exist in "PARFILIALGEP"  */
    if exists (select 1
               from   PARFILIALGEP t2, deleted t1
               where  t2.CDPROATIVEXTFLP = t1.CDPROCESSO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARFILIALGEP". Cannot delete parent "PROCESSO".'
          goto error
       end
    
    /*  Delete all children in "HABIPROCESSO"  */
    delete HABIPROCESSO
    from   HABIPROCESSO t2, deleted t1
    where  t2.CDPROCESSO = t1.CDPROCESSO
    
    /*  Delete all children in "DESCPROCESSO"  */
    delete DESCPROCESSO
    from   DESCPROCESSO t2, deleted t1
    where  t2.CDPROCESSO = t1.CDPROCESSO
    
    /*  Delete all children in "PROCESGPROC"  */
    delete PROCESGPROC
    from   PROCESGPROC t2, deleted t1
    where  t2.CDPROCESSO = t1.CDPROCESSO

    return

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

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

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

    
    /*  Cannot delete parent "PROCFABRICA" if children still exist in "ORDPRODUCAO"  */
    if exists (select 1
               from   ORDPRODUCAO t2, deleted t1
               where  t2.CDPROCFABRIC = t1.CDPROCFABRIC
                and   t2.CDPRODUTO = t1.CDPRODUTO
                and   t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORDPRODUCAO". Cannot delete parent "PROCFABRICA".'
          goto error
       end
    
    /*  Cannot delete parent "PROCFABRICA" if children still exist in "PARPRODOPCONS"  */
    if exists (select 1
               from   PARPRODOPCONS t2, deleted t1
               where  t2.CDPROCFABRIC = t1.CDPROCFABRIC
                and   t2.CDPRODUTO = t1.CDPRODUTO
                and   t2.CDFILIAL = t1.CDFILIAL)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARPRODOPCONS". Cannot delete parent "PROCFABRICA".'
          goto error
       end
    
    /*  Delete all children in "ETAFABRIC"  */
    delete ETAFABRIC
    from   ETAFABRIC t2, deleted t1
    where  t2.CDPROCFABRIC = t1.CDPROCFABRIC
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDFILIAL = t1.CDFILIAL

    return

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

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

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

    
    /*  Delete all children in "ITEMCONSSOLOL"  */
    delete ITEMCONSSOLOL
    from   ITEMCONSSOLOL t2, deleted t1
    where  t2.NRSEQUCONSOL = t1.NRSEQUCONSOL
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDFILSOLI = t1.CDFILSOLI
     and   t2.CDFILLAN = t1.CDFILLAN
    
    /*  Delete all children in "ESTOCONSSOLOL"  */
    delete ESTOCONSSOLOL
    from   ESTOCONSSOLOL t2, deleted t1
    where  t2.NRSEQUCONSOL = t1.NRSEQUCONSOL
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDFILSOLI = t1.CDFILSOLI
     and   t2.CDFILLAN = t1.CDFILLAN

    return

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

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

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

    
    /*  Delete all children in "PZOPGTPRFO"  */
    delete PZOPGTPRFO
    from   PZOPGTPRFO t2, deleted t1
    where  t2.CDFORNECED = t1.CDFORNECED
     and   t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "PRODFORNCOMP"  */
    delete PRODFORNCOMP
    from   PRODFORNCOMP t2, deleted t1
    where  t2.CDFORNECED = t1.CDFORNECED
     and   t2.CDPRODUTO = t1.CDPRODUTO

    return

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

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

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

    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODUTOCIA"  */
    if exists (select 1
               from   PRODUTOCIA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTOCIA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CONVPRODUTO"  */
    if exists (select 1
               from   CONVPRODUTO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONVPRODUTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "INCIDENCIA"  */
    if exists (select 1
               from   INCIDENCIA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INCIDENCIA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMPRODUTO"  */
    if exists (select 1
               from   ITEMPRODUTO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPRODUTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODFILI"  */
    if exists (select 1
               from   PRODFILI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFILI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "INVEROTA"  */
    if exists (select 1
               from   INVEROTA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVEROTA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITLANCTOEST"  */
    if exists (select 1
               from   ITLANCTOEST t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITLANCTOEST". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "POSESTDIA"  */
    if exists (select 1
               from   POSESTDIA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "POSESTDIA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ALIQUOTA"  */
    if exists (select 1
               from   ALIQUOTA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ALIQUOTA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CODFISCOPER"  */
    if exists (select 1
               from   CODFISCOPER t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODFISCOPER". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CODSITUTRIB"  */
    if exists (select 1
               from   CODSITUTRIB t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODSITUTRIB". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RECEFILI"  */
    if exists (select 1
               from   RECEFILI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEFILI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RECESERV"  */
    if exists (select 1
               from   RECESERV t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECESERV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RESTPROD"  */
    if exists (select 1
               from   RESTPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESTPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "LIMPESER"  */
    if exists (select 1
               from   LIMPESER t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LIMPESER". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "LIMPEPRA"  */
    if exists (select 1
               from   LIMPEPRA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LIMPEPRA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PREVCONS"  */
    if exists (select 1
               from   PREVCONS t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCONS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PREVRETI"  */
    if exists (select 1
               from   PREVRETI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVRETI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CONSSERV"  */
    if exists (select 1
               from   CONSSERV t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSSERV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "NDPCAL"  */
    if exists (select 1
               from   NDPCAL t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NDPCAL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "NUTRPROD"  */
    if exists (select 1
               from   NUTRPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NUTRPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITLANCTOEST"  */
    if exists (select 1
               from   ITLANCTOEST t2, deleted t1
               where  t2.CDPRODMOVI = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITLANCTOEST". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RATENESTO"  */
    if exists (select 1
               from   RATENESTO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATENESTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTNESTO"  */
    if exists (select 1
               from   CUSTNESTO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTNESTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMRETI"  */
    if exists (select 1
               from   ITEMRETI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMRETI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CONVPROD"  */
    if exists (select 1
               from   CONVPROD t2, deleted t1
               where  t2.CDPRODESTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONVPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CONVPROD"  */
    if exists (select 1
               from   CONVPROD t2, deleted t1
               where  t2.CDPRODCMPR = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONVPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SETOPRODCONS"  */
    if exists (select 1
               from   SETOPRODCONS t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SETOPRODCONS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SETOPRODCARD"  */
    if exists (select 1
               from   SETOPRODCARD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SETOPRODCARD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SERVICO"  */
    if exists (select 1
               from   SERVICO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVICO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SERVCLIE"  */
    if exists (select 1
               from   SERVCLIE t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCLIE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "MOVOPSOL"  */
    if exists (select 1
               from   MOVOPSOL t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOPSOL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMAUTF"  */
    if exists (select 1
               from   ITEMAUTF t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMAUTF". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMCOLE"  */
    if exists (select 1
               from   ITEMCOLE t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMCOLE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMSOLI"  */
    if exists (select 1
               from   ITEMSOLI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMSOLI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARTFILI"  */
    if exists (select 1
               from   PARTFILI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARTFILI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "TESTEPROD"  */
    if exists (select 1
               from   TESTEPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TESTEPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "MOVOCORR"  */
    if exists (select 1
               from   MOVOCORR t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOCORR". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODCMPFILI"  */
    if exists (select 1
               from   PRODCMPFILI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODCMPFILI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PREVCOMPRA"  */
    if exists (select 1
               from   PREVCOMPRA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCOMPRA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "EMITENTEAF"  */
    if exists (select 1
               from   EMITENTEAF t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMITENTEAF". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PERFILPROD"  */
    if exists (select 1
               from   PERFILPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERFILPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODNCMP"  */
    if exists (select 1
               from   PRODNCMP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODNCMP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "MEDSOLI"  */
    if exists (select 1
               from   MEDSOLI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MEDSOLI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RECEPADR"  */
    if exists (select 1
               from   RECEPADR t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEPADR". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "LIMPRODAF"  */
    if exists (select 1
               from   LIMPRODAF t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LIMPRODAF". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "LIMGERPROD"  */
    if exists (select 1
               from   LIMGERPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LIMGERPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMPEDIDO"  */
    if exists (select 1
               from   ITEMPEDIDO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPEDIDO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RETIRADA_CONS"  */
    if exists (select 1
               from   RETIRADA_CONS t2, deleted t1
               where  t2.CDPRODRCONS = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RETIRADA_CONS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARAMINTEGRA"  */
    if exists (select 1
               from   PARAMINTEGRA t2, deleted t1
               where  t2.CDPRODGENE = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMINTEGRA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARAMINTEGRA"  */
    if exists (select 1
               from   PARAMINTEGRA t2, deleted t1
               where  t2.CDPRODPSER = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMINTEGRA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARAMINTEGRA"  */
    if exists (select 1
               from   PARAMINTEGRA t2, deleted t1
               where  t2.CDPRODDESP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMINTEGRA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARAMINTEGRA"  */
    if exists (select 1
               from   PARAMINTEGRA t2, deleted t1
               where  t2.CDPRODREFE = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMINTEGRA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CONSERVREAL"  */
    if exists (select 1
               from   CONSERVREAL t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSERVREAL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CURVAABC"  */
    if exists (select 1
               from   CURVAABC t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CURVAABC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "INVFISCFIL"  */
    if exists (select 1
               from   INVFISCFIL t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVFISCFIL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "INVENTFISC"  */
    if exists (select 1
               from   INVENTFISC t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INVENTFISC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMVENDA"  */
    if exists (select 1
               from   ITEMVENDA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMVENDA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "QFOMARPROD"  */
    if exists (select 1
               from   QFOMARPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "QFOMARPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "MARCCOTA"  */
    if exists (select 1
               from   MARCCOTA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MARCCOTA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SERVCONTRAT"  */
    if exists (select 1
               from   SERVCONTRAT t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCONTRAT". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODUTOAP"  */
    if exists (select 1
               from   PRODUTOAP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTOAP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMPRECO"  */
    if exists (select 1
               from   ITEMPRECO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPRECO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMPEDCOL"  */
    if exists (select 1
               from   ITEMPEDCOL t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPEDCOL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMROMANEIO"  */
    if exists (select 1
               from   ITEMROMANEIO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMROMANEIO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODPREFMED"  */
    if exists (select 1
               from   PRODPREFMED t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODPREFMED". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODPREFMED"  */
    if exists (select 1
               from   PRODPREFMED t2, deleted t1
               where  t2.CDPRODPREF = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODPREFMED". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ROTFABRICA"  */
    if exists (select 1
               from   ROTFABRICA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROTFABRICA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ROTFABRICA"  */
    if exists (select 1
               from   ROTFABRICA t2, deleted t1
               where  t2.CDSUBPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROTFABRICA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "DESCROTEIRO"  */
    if exists (select 1
               from   DESCROTEIRO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESCROTEIRO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ORDPRODUCAO"  */
    if exists (select 1
               from   ORDPRODUCAO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORDPRODUCAO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "TRABEVOLPREC"  */
    if exists (select 1
               from   TRABEVOLPREC t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TRABEVOLPREC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMRETIEST"  */
    if exists (select 1
               from   ITEMRETIEST t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMRETIEST". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARAVEND"  */
    if exists (select 1
               from   PARAVEND t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAVEND". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMRETPCP"  */
    if exists (select 1
               from   ITEMRETPCP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMRETPCP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "LOGPRODUTOAP"  */
    if exists (select 1
               from   LOGPRODUTOAP t2, deleted t1
               where  t2.CDPRODUTO1 = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGPRODUTOAP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "AMOSTRAPROC"  */
    if exists (select 1
               from   AMOSTRAPROC t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AMOSTRAPROC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMAMOSTRA"  */
    if exists (select 1
               from   ITEMAMOSTRA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMAMOSTRA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PROCESSAPROD"  */
    if exists (select 1
               from   PROCESSAPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROCESSAPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMPROCPROD"  */
    if exists (select 1
               from   ITEMPROCPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPROCPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "QFOPRODFORN"  */
    if exists (select 1
               from   QFOPRODFORN t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "QFOPRODFORN". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARAMINTEGRA"  */
    if exists (select 1
               from   PARAMINTEGRA t2, deleted t1
               where  t2.CDPRODPADDOS = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMINTEGRA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODFATOR"  */
    if exists (select 1
               from   PRODFATOR t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFATOR". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "TPDOCTRANSAP"  */
    if exists (select 1
               from   TPDOCTRANSAP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TPDOCTRANSAP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "HABALIMT"  */
    if exists (select 1
               from   HABALIMT t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HABALIMT". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CODTRIBSAP"  */
    if exists (select 1
               from   CODTRIBSAP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODTRIBSAP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODMECA"  */
    if exists (select 1
               from   PRODMECA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODMECA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "VISTEQFORN"  */
    if exists (select 1
               from   VISTEQFORN t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VISTEQFORN". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CONFESCO"  */
    if exists (select 1
               from   CONFESCO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONFESCO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEQUIVALE"  */
    if exists (select 1
               from   ITEQUIVALE t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEQUIVALE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "COMCADIAPACI"  */
    if exists (select 1
               from   COMCADIAPACI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMCADIAPACI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEQUIPPRO"  */
    if exists (select 1
               from   ITEQUIPPRO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEQUIPPRO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ALIQIMPFIS"  */
    if exists (select 1
               from   ALIQIMPFIS t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ALIQIMPFIS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "DESPADMFATUR"  */
    if exists (select 1
               from   DESPADMFATUR t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESPADMFATUR". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODUTO"  */
    if exists (select 1
               from   PRODUTO t2, deleted t1
               where  t2.CDPRODORIG = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CNVPRODUNEST"  */
    if exists (select 1
               from   CNVPRODUNEST t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CNVPRODUNEST". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CNVPRODUNEST"  */
    if exists (select 1
               from   CNVPRODUNEST t2, deleted t1
               where  t2.CDPRUNEST = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CNVPRODUNEST". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODIMPOINVE"  */
    if exists (select 1
               from   PRODIMPOINVE t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODIMPOINVE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARAMCOMIS"  */
    if exists (select 1
               from   PARAMCOMIS t2, deleted t1
               where  t2.CDPRAEROIN = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMCOMIS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARAMCOMIS"  */
    if exists (select 1
               from   PARAMCOMIS t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMCOMIS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTOPRODUTO"  */
    if exists (select 1
               from   CUSTOPRODUTO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOPRODUTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTOPROCIA"  */
    if exists (select 1
               from   CUSTOPROCIA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOPROCIA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "MATDIDATICO"  */
    if exists (select 1
               from   MATDIDATICO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MATDIDATICO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ESTAGIO"  */
    if exists (select 1
               from   ESTAGIO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESTAGIO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITPRECOMATDI"  */
    if exists (select 1
               from   ITPRECOMATDI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITPRECOMATDI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "EMPRESTPROD"  */
    if exists (select 1
               from   EMPRESTPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMPRESTPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RESERVAPROD"  */
    if exists (select 1
               from   RESERVAPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESERVAPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "MOVILOTE"  */
    if exists (select 1
               from   MOVILOTE t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVILOTE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "INSPECPROD"  */
    if exists (select 1
               from   INSPECPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPECPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITINQUERPACI"  */
    if exists (select 1
               from   ITINQUERPACI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITINQUERPACI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMISSAO"  */
    if exists (select 1
               from   ITEMISSAO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMISSAO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ROTFABLOCAL"  */
    if exists (select 1
               from   ROTFABLOCAL t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROTFABLOCAL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ROTFABLOCAL"  */
    if exists (select 1
               from   ROTFABLOCAL t2, deleted t1
               where  t2.CDSUBPRODLOC = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROTFABLOCAL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "NIVELPRODSEG"  */
    if exists (select 1
               from   NIVELPRODSEG t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NIVELPRODSEG". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMPRECO"  */
    if exists (select 1
               from   ITEMPRECO t2, deleted t1
               where  t2.CDPRPAITABPR = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPRECO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PREVCONSPDV"  */
    if exists (select 1
               from   PREVCONSPDV t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCONSPDV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "EXEMPLAR"  */
    if exists (select 1
               from   EXEMPLAR t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EXEMPLAR". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PCHAVEPROD"  */
    if exists (select 1
               from   PCHAVEPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PCHAVEPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "AUTORPROD"  */
    if exists (select 1
               from   AUTORPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUTORPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMAPABASEV"  */
    if exists (select 1
               from   ITEMAPABASEV t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMAPABASEV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "TIPODEPE"  */
    if exists (select 1
               from   TIPODEPE t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPODEPE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRESATHOSP"  */
    if exists (select 1
               from   PRESATHOSP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRESATHOSP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PROGPRATHOSP"  */
    if exists (select 1
               from   PROGPRATHOSP t2, deleted t1
               where  t2.CDPRODSUBST = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROGPRATHOSP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODPROC"  */
    if exists (select 1
               from   PRODPROC t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODPROC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITPREVABASEV"  */
    if exists (select 1
               from   ITPREVABASEV t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITPREVABASEV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PROCED"  */
    if exists (select 1
               from   PROCED t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROCED". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODMETO"  */
    if exists (select 1
               from   PRODMETO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODMETO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CARDCONSNUTR"  */
    if exists (select 1
               from   CARDCONSNUTR t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDCONSNUTR". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "DESCROTLOCAL"  */
    if exists (select 1
               from   DESCROTLOCAL t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESCROTLOCAL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PUBLPROD"  */
    if exists (select 1
               from   PUBLPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PUBLPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PREAUTPROD"  */
    if exists (select 1
               from   PREAUTPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREAUTPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "DESCPROGPR"  */
    if exists (select 1
               from   DESCPROGPR t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESCPROGPR". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PROAGRCONV"  */
    if exists (select 1
               from   PROAGRCONV t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROAGRCONV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRNAUTCONV"  */
    if exists (select 1
               from   PRNAUTCONV t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRNAUTCONV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "FTAUTOCONV"  */
    if exists (select 1
               from   FTAUTOCONV t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FTAUTOCONV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "MOEDUTCONV"  */
    if exists (select 1
               from   MOEDUTCONV t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOEDUTCONV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "VALIHEMOCOMP"  */
    if exists (select 1
               from   VALIHEMOCOMP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VALIHEMOCOMP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "COLETDOAC"  */
    if exists (select 1
               from   COLETDOAC t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COLETDOAC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "HEMOPRDOAC"  */
    if exists (select 1
               from   HEMOPRDOAC t2, deleted t1
               where  t2.CDPRANTICOAG = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HEMOPRDOAC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RECEBDOAC"  */
    if exists (select 1
               from   RECEBDOAC t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEBDOAC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PROTESE"  */
    if exists (select 1
               from   PROTESE t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROTESE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "FORMPRPROT"  */
    if exists (select 1
               from   FORMPRPROT t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORMPRPROT". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARCLIEPRVMN"  */
    if exists (select 1
               from   PARCLIEPRVMN t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARCLIEPRVMN". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "MEDICAMENTO"  */
    if exists (select 1
               from   MEDICAMENTO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MEDICAMENTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITREQUHOSP"  */
    if exists (select 1
               from   ITREQUHOSP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITREQUHOSP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PREVCONSPDV"  */
    if exists (select 1
               from   PREVCONSPDV t2, deleted t1
               where  t2.CDPRPAIPCPDV = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCONSPDV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "DESCVALRPR"  */
    if exists (select 1
               from   DESCVALRPR t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESCVALRPR". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITPREVENDA"  */
    if exists (select 1
               from   ITPREVENDA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITPREVENDA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ESPEPROD"  */
    if exists (select 1
               from   ESPEPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESPEPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "COMPPROD"  */
    if exists (select 1
               from   COMPPROD t2, deleted t1
               where  t2.CDPRODINSUMO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PROCFABRICA"  */
    if exists (select 1
               from   PROCFABRICA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROCFABRICA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "INSUMETAPA"  */
    if exists (select 1
               from   INSUMETAPA t2, deleted t1
               where  t2.CDPRODINSUMO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSUMETAPA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITPREVCOMP"  */
    if exists (select 1
               from   ITPREVCOMP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITPREVCOMP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMAPABASEV"  */
    if exists (select 1
               from   ITEMAPABASEV t2, deleted t1
               where  t2.CDPRODORIGTR = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMAPABASEV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODTURCAIXA"  */
    if exists (select 1
               from   PRODTURCAIXA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODTURCAIXA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "POSESTCONS"  */
    if exists (select 1
               from   POSESTCONS t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "POSESTCONS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "AUDITORIA"  */
    if exists (select 1
               from   AUDITORIA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUDITORIA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SUBPRETAPAOP"  */
    if exists (select 1
               from   SUBPRETAPAOP t2, deleted t1
               where  t2.CDSUBPRODETA = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SUBPRETAPAOP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTPROD"  */
    if exists (select 1
               from   CUSTPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PERDAOP"  */
    if exists (select 1
               from   PERDAOP t2, deleted t1
               where  t2.CDPRODPERDA = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERDAOP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "APURFATUGH"  */
    if exists (select 1
               from   APURFATUGH t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURFATUGH". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RESPOSINSPEC"  */
    if exists (select 1
               from   RESPOSINSPEC t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESPOSINSPEC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PERDAETAPA"  */
    if exists (select 1
               from   PERDAETAPA t2, deleted t1
               where  t2.CDPRODPERDA = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERDAETAPA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITPROPOSTA"  */
    if exists (select 1
               from   ITPROPOSTA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITPROPOSTA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRECCONCORR"  */
    if exists (select 1
               from   PRECCONCORR t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRECCONCORR". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "POSESTATU"  */
    if exists (select 1
               from   POSESTATU t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "POSESTATU". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "FORMPRECPROD"  */
    if exists (select 1
               from   FORMPRECPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORMPRECPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RESERPRODPED"  */
    if exists (select 1
               from   RESERPRODPED t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESERPRODPED". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CALCCUST"  */
    if exists (select 1
               from   CALCCUST t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CALCCUST". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SERVPADR"  */
    if exists (select 1
               from   SERVPADR t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVPADR". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARAMPRODSUP"  */
    if exists (select 1
               from   PARAMPRODSUP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPRODSUP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "DESPHOSP"  */
    if exists (select 1
               from   DESPHOSP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESPHOSP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PERCPRODFATU"  */
    if exists (select 1
               from   PERCPRODFATU t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERCPRODFATU". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODACABADO"  */
    if exists (select 1
               from   PRODACABADO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODACABADO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "AUDITORIA"  */
    if exists (select 1
               from   AUDITORIA t2, deleted t1
               where  t2.CDPRODANTE = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUDITORIA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PREVIVENDA"  */
    if exists (select 1
               from   PREVIVENDA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVIVENDA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "VERSAOPROD"  */
    if exists (select 1
               from   VERSAOPROD t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VERSAOPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDPRODCOMP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODLIBPATCH"  */
    if exists (select 1
               from   PRODLIBPATCH t2, deleted t1
               where  t2.CDPRODCOMP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODLIBPATCH". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODLIBEVERS"  */
    if exists (select 1
               from   PRODLIBEVERS t2, deleted t1
               where  t2.CDPRODCOMP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODLIBEVERS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "REQUISITPROD"  */
    if exists (select 1
               from   REQUISITPROD t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REQUISITPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODREQPROD"  */
    if exists (select 1
               from   PRODREQPROD t2, deleted t1
               where  t2.CDPRODCOMP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODREQPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODSUBCLIE"  */
    if exists (select 1
               from   PRODSUBCLIE t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODSUBCLIE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODTIPCONS"  */
    if exists (select 1
               from   PRODTIPCONS t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODTIPCONS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITREQUESTO"  */
    if exists (select 1
               from   ITREQUESTO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITREQUESTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODFABRIC"  */
    if exists (select 1
               from   PRODFABRIC t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFABRIC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODPRINATIV"  */
    if exists (select 1
               from   PRODPRINATIV t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODPRINATIV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODCENTCUST"  */
    if exists (select 1
               from   PRODCENTCUST t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODCENTCUST". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RECITEMAPABA"  */
    if exists (select 1
               from   RECITEMAPABA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECITEMAPABA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITVENDAVMN"  */
    if exists (select 1
               from   ITVENDAVMN t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITVENDAVMN". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "COMPOSINSPEC"  */
    if exists (select 1
               from   COMPOSINSPEC t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPOSINSPEC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "COMPOSINSPEC"  */
    if exists (select 1
               from   COMPOSINSPEC t2, deleted t1
               where  t2.CDPRODINSPEC = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPOSINSPEC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODBASELINE"  */
    if exists (select 1
               from   PRODBASELINE t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODBASELINE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITPREVABASAU"  */
    if exists (select 1
               from   ITPREVABASAU t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITPREVABASAU". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PREVENDAEQU"  */
    if exists (select 1
               from   PREVENDAEQU t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVENDAEQU". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PREVENDAVMN"  */
    if exists (select 1
               from   PREVENDAVMN t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVENDAVMN". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PREVCONSVMN"  */
    if exists (select 1
               from   PREVCONSVMN t2, deleted t1
               where  t2.CDSUBPRODVMN = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCONSVMN". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "IMPITVENDAVM"  */
    if exists (select 1
               from   IMPITVENDAVM t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPITVENDAVM". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITABASTEROTA"  */
    if exists (select 1
               from   ITABASTEROTA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITABASTEROTA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITPREVABAMAP"  */
    if exists (select 1
               from   ITPREVABAMAP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITPREVABAMAP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "JUSTIFICAVMN"  */
    if exists (select 1
               from   JUSTIFICAVMN t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "JUSTIFICAVMN". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "TAXAADMFATUR"  */
    if exists (select 1
               from   TAXAADMFATUR t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAXAADMFATUR". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "DESPPREV"  */
    if exists (select 1
               from   DESPPREV t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESPPREV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "DESPPREVFILI"  */
    if exists (select 1
               from   DESPPREVFILI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESPPREVFILI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODFILIFATU"  */
    if exists (select 1
               from   PRODFILIFATU t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFILIFATU". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "EMITAFFORN"  */
    if exists (select 1
               from   EMITAFFORN t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMITAFFORN". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "HISITEMPED"  */
    if exists (select 1
               from   HISITEMPED t2, deleted t1
               where  t2.CDPRODORIG = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HISITEMPED". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODMETAVEND"  */
    if exists (select 1
               from   PRODMETAVEND t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODMETAVEND". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RELAT_MANDATO"  */
    if exists (select 1
               from   RELAT_MANDATO t2, deleted t1
               where  t2.CDPRODCMPMAN = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELAT_MANDATO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RELAT_MANDATO"  */
    if exists (select 1
               from   RELAT_MANDATO t2, deleted t1
               where  t2.CDPRODCMPDES = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELAT_MANDATO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARAMCMPPROD"  */
    if exists (select 1
               from   PARAMCMPPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMCMPPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ARVOREQPROD"  */
    if exists (select 1
               from   ARVOREQPROD t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ARVOREQPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTOSUP"  */
    if exists (select 1
               from   CUSTOSUP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOSUP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "GRUPSIST"  */
    if exists (select 1
               from   GRUPSIST t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GRUPSIST". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SISTEMA"  */
    if exists (select 1
               from   SISTEMA t2, deleted t1
               where  t2.CDPRODCOMP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SISTEMA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "EQUIPAPROD"  */
    if exists (select 1
               from   EQUIPAPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQUIPAPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODTPMANUT"  */
    if exists (select 1
               from   PRODTPMANUT t2, deleted t1
               where  t2.CDPRODCOMP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODTPMANUT". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODSOLISERV"  */
    if exists (select 1
               from   PRODSOLISERV t2, deleted t1
               where  t2.CDPRODCOMP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODSOLISERV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITCOMANDAVEN"  */
    if exists (select 1
               from   ITCOMANDAVEN t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITCOMANDAVEN". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CTBPRDTPOPER"  */
    if exists (select 1
               from   CTBPRDTPOPER t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTBPRDTPOPER". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PROLIVROPREC"  */
    if exists (select 1
               from   PROLIVROPREC t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROLIVROPREC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTOPROCIA"  */
    if exists (select 1
               from   CUSTOPROCIA t2, deleted t1
               where  t2.CDRECPRIPC = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOPROCIA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTORECIA"  */
    if exists (select 1
               from   CUSTORECIA t2, deleted t1
               where  t2.CDRECPRIC = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTORECIA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTORECEITA"  */
    if exists (select 1
               from   CUSTORECEITA t2, deleted t1
               where  t2.CDRECPRI = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTORECEITA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTOPRODUTO"  */
    if exists (select 1
               from   CUSTOPRODUTO t2, deleted t1
               where  t2.CDRECPRIP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOPRODUTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTORECIA"  */
    if exists (select 1
               from   CUSTORECIA t2, deleted t1
               where  t2.CDSUBGRU = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTORECIA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTOPROCIA"  */
    if exists (select 1
               from   CUSTOPROCIA t2, deleted t1
               where  t2.CDSUBGRU = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOPROCIA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRECOSECIA"  */
    if exists (select 1
               from   PRECOSECIA t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRECOSECIA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PROPSERVICO"  */
    if exists (select 1
               from   PROPSERVICO t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPSERVICO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PADEQUIP"  */
    if exists (select 1
               from   PADEQUIP t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PADEQUIP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PADPREFIXO"  */
    if exists (select 1
               from   PADPREFIXO t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PADPREFIXO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SERVPLANO"  */
    if exists (select 1
               from   SERVPLANO t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVPLANO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PADVOOTRE"  */
    if exists (select 1
               from   PADVOOTRE t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PADVOOTRE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTORECIA"  */
    if exists (select 1
               from   CUSTORECIA t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTORECIA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTOPROCIA"  */
    if exists (select 1
               from   CUSTOPROCIA t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOPROCIA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "FRCICLOSER"  */
    if exists (select 1
               from   FRCICLOSER t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FRCICLOSER". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PREVCOMPRA"  */
    if exists (select 1
               from   PREVCOMPRA t2, deleted t1
               where  t2.CDSERVCOMP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCOMPRA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMRETPCP"  */
    if exists (select 1
               from   ITEMRETPCP t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMRETPCP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "LOJA"  */
    if exists (select 1
               from   LOJA t2, deleted t1
               where  t2.CDPRODCOUVER = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOJA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "LOJA"  */
    if exists (select 1
               from   LOJA t2, deleted t1
               where  t2.CDPRODCONSUM = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOJA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITCOMANDACXA"  */
    if exists (select 1
               from   ITCOMANDACXA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITCOMANDACXA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITVENDACAN"  */
    if exists (select 1
               from   ITVENDACAN t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITVENDACAN". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTOSEMA"  */
    if exists (select 1
               from   CUSTOSEMA t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOSEMA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SERVBREAK"  */
    if exists (select 1
               from   SERVBREAK t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVBREAK". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "LOJA"  */
    if exists (select 1
               from   LOJA t2, deleted t1
               where  t2.CDPRODCOMVEN = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOJA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODOCORRE"  */
    if exists (select 1
               from   PRODOCORRE t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODOCORRE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "VRPRODBCIMPO"  */
    if exists (select 1
               from   VRPRODBCIMPO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VRPRODBCIMPO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "TEMPOPREPSER"  */
    if exists (select 1
               from   TEMPOPREPSER t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TEMPOPREPSER". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "TEMPOPREPSUB"  */
    if exists (select 1
               from   TEMPOPREPSUB t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TEMPOPREPSUB". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "TEMPOPREPSUB"  */
    if exists (select 1
               from   TEMPOPREPSUB t2, deleted t1
               where  t2.CDSUBGRU = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TEMPOPREPSUB". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "TEMPOPREPREC"  */
    if exists (select 1
               from   TEMPOPREPREC t2, deleted t1
               where  t2.CDRECEITA = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TEMPOPREPREC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ESTOSEGU"  */
    if exists (select 1
               from   ESTOSEGU t2, deleted t1
               where  t2.CDPRODESTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESTOSEGU". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RELACEXPPROD"  */
    if exists (select 1
               from   RELACEXPPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELACEXPPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODUTO"  */
    if exists (select 1
               from   PRODUTO t2, deleted t1
               where  t2.CDPRODFATURA = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CDBARRAPROD"  */
    if exists (select 1
               from   CDBARRAPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CDBARRAPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARAMPRODMRP"  */
    if exists (select 1
               from   PARAMPRODMRP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPRODMRP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PROGPRODU"  */
    if exists (select 1
               from   PROGPRODU t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROGPRODU". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PREMATPPROGP"  */
    if exists (select 1
               from   PREMATPPROGP t2, deleted t1
               where  t2.CDPRODPREVMP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREMATPPROGP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CALEPRODMRP"  */
    if exists (select 1
               from   CALEPRODMRP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CALEPRODMRP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RELAT_CDRS"  */
    if exists (select 1
               from   RELAT_CDRS t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELAT_CDRS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "EFETCDRS"  */
    if exists (select 1
               from   EFETCDRS t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFETCDRS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "EFETRATCDRS"  */
    if exists (select 1
               from   EFETRATCDRS t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFETRATCDRS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SERVCONTRAT"  */
    if exists (select 1
               from   SERVCONTRAT t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCONTRAT". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMENTREGA"  */
    if exists (select 1
               from   ITEMENTREGA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMENTREGA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODLOJA"  */
    if exists (select 1
               from   PRODLOJA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODLOJA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "DEMAEXTRAMRP"  */
    if exists (select 1
               from   DEMAEXTRAMRP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DEMAEXTRAMRP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "IMPOCONTRETI"  */
    if exists (select 1
               from   IMPOCONTRETI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOCONTRETI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "VRMAXVENPROD"  */
    if exists (select 1
               from   VRMAXVENPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VRMAXVENPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "INCONSMRP"  */
    if exists (select 1
               from   INCONSMRP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INCONSMRP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PERLISINFLAC"  */
    if exists (select 1
               from   PERLISINFLAC t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERLISINFLAC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ESPEPROD"  */
    if exists (select 1
               from   ESPEPROD t2, deleted t1
               where  t2.CDPRODUTOPRI = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESPEPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "TPSUBPRODPRO"  */
    if exists (select 1
               from   TPSUBPRODPRO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TPSUBPRODPRO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "LOJA"  */
    if exists (select 1
               from   LOJA t2, deleted t1
               where  t2.CDPRODCONSUF = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOJA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "COMPITVENDA"  */
    if exists (select 1
               from   COMPITVENDA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPITVENDA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITPRODORIEXP"  */
    if exists (select 1
               from   ITPRODORIEXP t2, deleted t1
               where  t2.CDPRODORIEXP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITPRODORIEXP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SERVTIPOCARD"  */
    if exists (select 1
               from   SERVTIPOCARD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVTIPOCARD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ASSUNTO"  */
    if exists (select 1
               from   ASSUNTO t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ASSUNTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODCLIE"  */
    if exists (select 1
               from   PRODCLIE t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODCLIE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PREVCONSPME"  */
    if exists (select 1
               from   PREVCONSPME t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCONSPME". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CMPCENTENDIR"  */
    if exists (select 1
               from   CMPCENTENDIR t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CMPCENTENDIR". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "OCORRERFATUR"  */
    if exists (select 1
               from   OCORRERFATUR t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OCORRERFATUR". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMVENDMERE"  */
    if exists (select 1
               from   ITEMVENDMERE t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMVENDMERE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMVENDMERE"  */
    if exists (select 1
               from   ITEMVENDMERE t2, deleted t1
               where  t2.CDPRODFATUR = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMVENDMERE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RELACPRODOL"  */
    if exists (select 1
               from   RELACPRODOL t2, deleted t1
               where  t2.CDPRODARVOL = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELACPRODOL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RELACPRODOL"  */
    if exists (select 1
               from   RELACPRODOL t2, deleted t1
               where  t2.CDPRARVORIG = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELACPRODOL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMSAIDOL"  */
    if exists (select 1
               from   ITEMSAIDOL t2, deleted t1
               where  t2.CDPRODSAIDOL = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMSAIDOL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "OPCPRODREC"  */
    if exists (select 1
               from   OPCPRODREC t2, deleted t1
               where  t2.CDPRORIGREC = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPCPRODREC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "OPCPRODREC"  */
    if exists (select 1
               from   OPCPRODREC t2, deleted t1
               where  t2.CDPROPCREC = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OPCPRODREC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODNECECOMP"  */
    if exists (select 1
               from   PRODNECECOMP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODNECECOMP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMINVOICE"  */
    if exists (select 1
               from   ITEMINVOICE t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMINVOICE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "FAMSALDOPROD"  */
    if exists (select 1
               from   FAMSALDOPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FAMSALDOPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODUTOFAM"  */
    if exists (select 1
               from   PRODUTOFAM t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTOFAM". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "TIPOSERV"  */
    if exists (select 1
               from   TIPOSERV t2, deleted t1
               where  t2.CDPRODFATU = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOSERV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "APURFATUFAS"  */
    if exists (select 1
               from   APURFATUFAS t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APURFATUFAS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PAR_GERAL_FAS"  */
    if exists (select 1
               from   PAR_GERAL_FAS t2, deleted t1
               where  t2.CDPROFATLUSO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PAR_GERAL_FAS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PAR_GERAL_FAS"  */
    if exists (select 1
               from   PAR_GERAL_FAS t2, deleted t1
               where  t2.CDPROFATMANU = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PAR_GERAL_FAS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PAR_GERAL_FAS"  */
    if exists (select 1
               from   PAR_GERAL_FAS t2, deleted t1
               where  t2.CDPROFATLOCA = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PAR_GERAL_FAS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "MARCPRODUTO"  */
    if exists (select 1
               from   MARCPRODUTO t2, deleted t1
               where  t2.CDPRODCOMP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MARCPRODUTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARAVEND"  */
    if exists (select 1
               from   PARAVEND t2, deleted t1
               where  t2.CDPRODTAXSER = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAVEND". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CONSSERVPME"  */
    if exists (select 1
               from   CONSSERVPME t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSSERVPME". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODRALIMENT"  */
    if exists (select 1
               from   PRODRALIMENT t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODRALIMENT". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMPEDEXOL"  */
    if exists (select 1
               from   ITEMPEDEXOL t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPEDEXOL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMORCAEVEN"  */
    if exists (select 1
               from   ITEMORCAEVEN t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMORCAEVEN". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRECUSTOSERV"  */
    if exists (select 1
               from   PRECUSTOSERV t2, deleted t1
               where  t2.CDSUBGRUPO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRECUSTOSERV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRECUSTOSERV"  */
    if exists (select 1
               from   PRECUSTOSERV t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRECUSTOSERV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITCUSTOSERV"  */
    if exists (select 1
               from   ITCUSTOSERV t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITCUSTOSERV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITMAOOBRAORCA"  */
    if exists (select 1
               from   ITMAOOBRAORCA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITMAOOBRAORCA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITDEGUSTACAO"  */
    if exists (select 1
               from   ITDEGUSTACAO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITDEGUSTACAO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "CUSTOLOGIST"  */
    if exists (select 1
               from   CUSTOLOGIST t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTOLOGIST". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMSOLI"  */
    if exists (select 1
               from   ITEMSOLI t2, deleted t1
               where  t2.CDPRODORIG = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMSOLI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "AJUSTEQTDSOL"  */
    if exists (select 1
               from   AJUSTEQTDSOL t2, deleted t1
               where  t2.CDPRODORIG = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJUSTEQTDSOL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "AJUSTEQTDSOL"  */
    if exists (select 1
               from   AJUSTEQTDSOL t2, deleted t1
               where  t2.CDPRODDEST = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJUSTEQTDSOL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ACERTOQUEBRA"  */
    if exists (select 1
               from   ACERTOQUEBRA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACERTOQUEBRA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ACERTOSOBRA"  */
    if exists (select 1
               from   ACERTOSOBRA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACERTOSOBRA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ACERTOHREXTR"  */
    if exists (select 1
               from   ACERTOHREXTR t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACERTOHREXTR". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ACERTOEXTRA"  */
    if exists (select 1
               from   ACERTOEXTRA t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACERTOEXTRA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODSERVTCON"  */
    if exists (select 1
               from   PRODSERVTCON t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODSERVTCON". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEXCLISPREC"  */
    if exists (select 1
               from   ITEXCLISPREC t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEXCLISPREC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "MOVOPAF"  */
    if exists (select 1
               from   MOVOPAF t2, deleted t1
               where  t2.CDPRODMOV = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOPAF". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SERVPLANOCANC"  */
    if exists (select 1
               from   SERVPLANOCANC t2, deleted t1
               where  t2.CDSERVICOCANC = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVPLANOCANC". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SALAOEVENTO"  */
    if exists (select 1
               from   SALAOEVENTO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SALAOEVENTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "TURNOPRODUTO"  */
    if exists (select 1
               from   TURNOPRODUTO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TURNOPRODUTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODCONSSOLOL"  */
    if exists (select 1
               from   PRODCONSSOLOL t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODCONSSOLOL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ESTOCONSSOLOL"  */
    if exists (select 1
               from   ESTOCONSSOLOL t2, deleted t1
               where  t2.CDPRODARVOL = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESTOCONSSOLOL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "JUSTCONSSOLOL"  */
    if exists (select 1
               from   JUSTCONSSOLOL t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "JUSTCONSSOLOL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "DATAEMBARQUEOL"  */
    if exists (select 1
               from   DATAEMBARQUEOL t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DATAEMBARQUEOL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "TIPOSERVPROD"  */
    if exists (select 1
               from   TIPOSERVPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOSERVPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PAR_GERAL_FAS"  */
    if exists (select 1
               from   PAR_GERAL_FAS t2, deleted t1
               where  t2.CDPROFATSOLI = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PAR_GERAL_FAS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODUTOROP"  */
    if exists (select 1
               from   PRODUTOROP t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTOROP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ETL_FORSALE"  */
    if exists (select 1
               from   ETL_FORSALE t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_FORSALE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PREVCONSPDVDIA"  */
    if exists (select 1
               from   PREVCONSPDVDIA t2, deleted t1
               where  t2.CDPRPAIPCPDV = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCONSPDVDIA". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARPRODOPCONS"  */
    if exists (select 1
               from   PARPRODOPCONS t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARPRODOPCONS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARPRODOPCONS"  */
    if exists (select 1
               from   PARPRODOPCONS t2, deleted t1
               where  t2.CDMATPRIMA = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARPRODOPCONS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ETL_RESUFOOD"  */
    if exists (select 1
               from   ETL_RESUFOOD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUFOOD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ETL_RESUESTO"  */
    if exists (select 1
               from   ETL_RESUESTO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUESTO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "INSUPEDIDOP"  */
    if exists (select 1
               from   INSUPEDIDOP t2, deleted t1
               where  t2.CDPRODINSUMO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSUPEDIDOP". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "DEVOLORCAEVEN"  */
    if exists (select 1
               from   DEVOLORCAEVEN t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DEVOLORCAEVEN". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARAMORCAEVE"  */
    if exists (select 1
               from   PARAMORCAEVE t2, deleted t1
               where  t2.CDPRODFRETE = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMORCAEVE". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITCOMISSAO"  */
    if exists (select 1
               from   ITCOMISSAO t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITCOMISSAO". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODBLOQVND"  */
    if exists (select 1
               from   PRODBLOQVND t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODBLOQVND". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PARAMDESCOREV"  */
    if exists (select 1
               from   PARAMDESCOREV t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMDESCOREV". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRMARCPEDCOL"  */
    if exists (select 1
               from   PRMARCPEDCOL t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRMARCPEDCOL". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ETLCOMPRASGER"  */
    if exists (select 1
               from   ETLCOMPRASGER t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCOMPRASGER". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "MAPDISTALMOX"  */
    if exists (select 1
               from   MAPDISTALMOX t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPDISTALMOX". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "NFRELACCDRS"  */
    if exists (select 1
               from   NFRELACCDRS t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NFRELACCDRS". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PRODTIPOSUPRI"  */
    if exists (select 1
               from   PRODTIPOSUPRI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODTIPOSUPRI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMSOLISUPRI"  */
    if exists (select 1
               from   ITEMSOLISUPRI t2, deleted t1
               where  t2.CDPRODSOLISUP = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMSOLISUPRI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "COMPITEMPED"  */
    if exists (select 1
               from   COMPITEMPED t2, deleted t1
               where  t2.CDPRODCOMPED = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPITEMPED". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITCOMANDAPEND"  */
    if exists (select 1
               from   ITCOMANDAPEND t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITCOMANDAPEND". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "ITEMINVENTRETI"  */
    if exists (select 1
               from   ITEMINVENTRETI t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMINVENTRETI". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PROPSGPROD"  */
    if exists (select 1
               from   PROPSGPROD t2, deleted t1
               where  t2.CDSERVICO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPSGPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "PROPSGPROD"  */
    if exists (select 1
               from   PROPSGPROD t2, deleted t1
               where  t2.CDSUBGRUPO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPSGPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SERVPREVPAXPROD"  */
    if exists (select 1
               from   SERVPREVPAXPROD t2, deleted t1
               where  t2.CDPRODUTO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVPREVPAXPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "SGPREVPAXPROD"  */
    if exists (select 1
               from   SGPREVPAXPROD t2, deleted t1
               where  t2.CDSUBGRUPO = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SGPREVPAXPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTO" if children still exist in "RECEPREVPAXPROD"  */
    if exists (select 1
               from   RECEPREVPAXPROD t2, deleted t1
               where  t2.CDRECEITA = t1.CDPRODUTO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEPREVPAXPROD". Cannot delete parent "PRODUTO".'
          goto error
       end
    
    /*  Delete all children in "PRODFORN"  */
    delete PRODFORN
    from   PRODFORN t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "PRODCOMP"  */
    delete PRODCOMP
    from   PRODCOMP t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "LIMISEGPROD"  */
    delete LIMISEGPROD
    from   LIMISEGPROD t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "MARCPROD"  */
    delete MARCPROD
    from   MARCPROD t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "ITEMPREBASE"  */
    delete ITEMPREBASE
    from   ITEMPREBASE t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "PRODINTDOS"  */
    delete PRODINTDOS
    from   PRODINTDOS t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "APREPROD"  */
    delete APREPROD
    from   APREPROD t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "NVSUBGRPROD"  */
    delete NVSUBGRPROD
    from   NVSUBGRPROD t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "PRSUBGRPROD"  */
    delete PRSUBGRPROD
    from   PRSUBGRPROD t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "PRODFORNCOMP"  */
    delete PRODFORNCOMP
    from   PRODFORNCOMP t2, deleted t1
    where  t2.CDPRODCOMPL = t1.CDPRODUTO
    
    /*  Delete all children in "MARCPRODCOMP"  */
    delete MARCPRODCOMP
    from   MARCPRODCOMP t2, deleted t1
    where  t2.CDPRMARCOMPL = t1.CDPRODUTO
    
    /*  Delete all children in "PRODLCESTOQ"  */
    delete PRODLCESTOQ
    from   PRODLCESTOQ t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "LOTE"  */
    delete LOTE
    from   LOTE t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "PRODUTOSEG"  */
    delete PRODUTOSEG
    from   PRODUTOSEG t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "PRODITEMORCA"  */
    delete PRODITEMORCA
    from   PRODITEMORCA t2, deleted t1
    where  t2.CDPRODGRUP = t1.CDPRODUTO
    
    /*  Delete all children in "ITEMTRABPROD"  */
    delete ITEMTRABPROD
    from   ITEMTRABPROD t2, deleted t1
    where  t2.CDPRODCOMP = t1.CDPRODUTO
    
    /*  Delete all children in "ITDESPREBASE"  */
    delete ITDESPREBASE
    from   ITDESPREBASE t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "CUSTORECIA"  */
    delete CUSTORECIA
    from   CUSTORECIA t2, deleted t1
    where  t2.CDRECEITA = t1.CDPRODUTO
    
    /*  Delete all children in "CUSTORECEITA"  */
    delete CUSTORECEITA
    from   CUSTORECEITA t2, deleted t1
    where  t2.CDRECEITA = t1.CDPRODUTO
    
    /*  Delete all children in "TRABNUTRIREC"  */
    delete TRABNUTRIREC
    from   TRABNUTRIREC t2, deleted t1
    where  t2.CDRECEITA = t1.CDPRODUTO
    
    /*  Delete all children in "REGRAREVRECE"  */
    delete REGRAREVRECE
    from   REGRAREVRECE t2, deleted t1
    where  t2.CDPRODORIGRE = t1.CDPRODUTO
    
    /*  Delete all children in "REGRAREVRECE"  */
    delete REGRAREVRECE
    from   REGRAREVRECE t2, deleted t1
    where  t2.CDPRODDESTRE = t1.CDPRODUTO
    
    /*  Delete all children in "PERCACRPROD"  */
    delete PERCACRPROD
    from   PERCACRPROD t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "RESTPRODCLIE"  */
    delete RESTPRODCLIE
    from   RESTPRODCLIE t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "PREFPRODCLIE"  */
    delete PREFPRODCLIE
    from   PREFPRODCLIE t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO

    return

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

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

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

    
    /*  Cannot delete parent "PRODUTOCIA" if children still exist in "EQESPEPROD"  */
    if exists (select 1
               from   EQESPEPROD t2, deleted t1
               where  t2.CDPRODEQUIP = t1.CDPRODUTO
                and   t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQESPEPROD". Cannot delete parent "PRODUTOCIA".'
          goto error
       end
    
    /*  Cannot delete parent "PRODUTOCIA" if children still exist in "SUBPRODCIA"  */
    if exists (select 1
               from   SUBPRODCIA t2, deleted t1
               where  t2.CDSUBPROCIA = t1.CDPRODUTO
                and   t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDCIA = t1.CDCIA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SUBPRODCIA". Cannot delete parent "PRODUTOCIA".'
          goto error
       end
    
    /*  Delete all children in "SUBPRODCIA"  */
    delete SUBPRODCIA
    from   SUBPRODCIA t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDCIA = t1.CDCIA

    return

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

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

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

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

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "PROPOSTAFAS" if children still exist in "CONTRATOLINP"  */
    if exists (select 1
               from   CONTRATOLINP t2, deleted t1
               where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
                and   t2.NRVERPROPFAS = t1.NRVERPROPFAS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTRATOLINP". Cannot delete parent "PROPOSTAFAS".'
          goto error
       end
    
    /*  Cannot delete parent "PROPOSTAFAS" if children still exist in "CONTRATOSERV"  */
    if exists (select 1
               from   CONTRATOSERV t2, deleted t1
               where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
                and   t2.NRVERPROPFAS = t1.NRVERPROPFAS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTRATOSERV". Cannot delete parent "PROPOSTAFAS".'
          goto error
       end
    
    /*  Cannot delete parent "PROPOSTAFAS" if children still exist in "PROJETO"  */
    if exists (select 1
               from   PROJETO t2, deleted t1
               where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
                and   t2.NRVERPROPFAS = t1.NRVERPROPFAS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROJETO". Cannot delete parent "PROPOSTAFAS".'
          goto error
       end
    
    /*  Delete all children in "TXTPROPFAS"  */
    delete TXTPROPFAS
    from   TXTPROPFAS t2, deleted t1
    where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
     and   t2.NRVERPROPFAS = t1.NRVERPROPFAS
    
    /*  Delete all children in "PROPOSTALINP"  */
    delete PROPOSTALINP
    from   PROPOSTALINP t2, deleted t1
    where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
     and   t2.NRVERPROPFAS = t1.NRVERPROPFAS
    
    /*  Delete all children in "PEDILINPPROP"  */
    delete PEDILINPPROP
    from   PEDILINPPROP t2, deleted t1
    where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
     and   t2.NRVERPROPFAS = t1.NRVERPROPFAS
    
    /*  Delete all children in "PEDISERVPROP"  */
    delete PEDISERVPROP
    from   PEDISERVPROP t2, deleted t1
    where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
     and   t2.NRVERPROPFAS = t1.NRVERPROPFAS
    
    /*  Delete all children in "PROPOSTASERV"  */
    delete PROPOSTASERV
    from   PROPOSTASERV t2, deleted t1
    where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
     and   t2.NRVERPROPFAS = t1.NRVERPROPFAS
    
    /*  Delete all children in "OBSPROPFAS"  */
    delete OBSPROPFAS
    from   OBSPROPFAS t2, deleted t1
    where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
     and   t2.NRVERPROPFAS = t1.NRVERPROPFAS

    return

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

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

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

    
    /*  Cannot delete parent "PROPOSTALINP" if children still exist in "PEDILINPPROP"  */
    if exists (select 1
               from   PEDILINPPROP t2, deleted t1
               where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
                and   t2.NRVERPROPFAS = t1.NRVERPROPFAS
                and   t2.CDLINPPROP = t1.CDLINPPROP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDILINPPROP". Cannot delete parent "PROPOSTALINP".'
          goto error
       end
    
    /*  Delete all children in "SISTPROPLINP"  */
    delete SISTPROPLINP
    from   SISTPROPLINP t2, deleted t1
    where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
     and   t2.NRVERPROPFAS = t1.NRVERPROPFAS
     and   t2.CDLINPPROP = t1.CDLINPPROP
    
    /*  Delete all children in "VENCLUSOPROP"  */
    delete VENCLUSOPROP
    from   VENCLUSOPROP t2, deleted t1
    where  t2.NRPROPOSFAS = t1.NRPROPOSFAS
     and   t2.NRVERPROPFAS = t1.NRVERPROPFAS
     and   t2.CDLINPPROP = t1.CDLINPPROP

    return

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

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

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

    
    /*  Cannot delete parent "PROSPECT" if children still exist in "CONTATO"  */
    if exists (select 1
               from   CONTATO t2, deleted t1
               where  t2.CDPROSPECT = t1.CDPROSPECT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTATO". Cannot delete parent "PROSPECT".'
          goto error
       end
    
    /*  Cannot delete parent "PROSPECT" if children still exist in "ACAO"  */
    if exists (select 1
               from   ACAO t2, deleted t1
               where  t2.CDPROSPECT = t1.CDPROSPECT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACAO". Cannot delete parent "PROSPECT".'
          goto error
       end
    
    /*  Cannot delete parent "PROSPECT" if children still exist in "ESTINTERESSE"  */
    if exists (select 1
               from   ESTINTERESSE t2, deleted t1
               where  t2.CDPROSPECT = t1.CDPROSPECT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESTINTERESSE". Cannot delete parent "PROSPECT".'
          goto error
       end
    
    /*  Cannot delete parent "PROSPECT" if children still exist in "CLIENTE"  */
    if exists (select 1
               from   CLIENTE t2, deleted t1
               where  t2.CDPROSPECT = t1.CDPROSPECT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENTE". Cannot delete parent "PROSPECT".'
          goto error
       end
    
    /*  Cannot delete parent "PROSPECT" if children still exist in "ESCOPROS"  */
    if exists (select 1
               from   ESCOPROS t2, deleted t1
               where  t2.CDPROSPECT = t1.CDPROSPECT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESCOPROS". Cannot delete parent "PROSPECT".'
          goto error
       end
    
    /*  Cannot delete parent "PROSPECT" if children still exist in "PROSPFILIAL"  */
    if exists (select 1
               from   PROSPFILIAL t2, deleted t1
               where  t2.CDPROSPECT = t1.CDPROSPECT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROSPFILIAL". Cannot delete parent "PROSPECT".'
          goto error
       end
    
    /*  Cannot delete parent "PROSPECT" if children still exist in "TAREFA"  */
    if exists (select 1
               from   TAREFA t2, deleted t1
               where  t2.CDPROSPECT = t1.CDPROSPECT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAREFA". Cannot delete parent "PROSPECT".'
          goto error
       end
    
    /*  Cannot delete parent "PROSPECT" if children still exist in "AUDITORIA"  */
    if exists (select 1
               from   AUDITORIA t2, deleted t1
               where  t2.CDPROSPECT = t1.CDPROSPECT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUDITORIA". Cannot delete parent "PROSPECT".'
          goto error
       end
    
    /*  Cannot delete parent "PROSPECT" if children still exist in "MOVOCORR"  */
    if exists (select 1
               from   MOVOCORR t2, deleted t1
               where  t2.CDPROSPECT = t1.CDPROSPECT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOCORR". Cannot delete parent "PROSPECT".'
          goto error
       end
    
    /*  Cannot delete parent "PROSPECT" if children still exist in "PROPOSTAFAS"  */
    if exists (select 1
               from   PROPOSTAFAS t2, deleted t1
               where  t2.CDPROSPECT = t1.CDPROSPECT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPOSTAFAS". Cannot delete parent "PROSPECT".'
          goto error
       end
    
    /*  Cannot delete parent "PROSPECT" if children still exist in "CURSOPROSPECT"  */
    if exists (select 1
               from   CURSOPROSPECT t2, deleted t1
               where  t2.CDPROSPECT = t1.CDPROSPECT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CURSOPROSPECT". Cannot delete parent "PROSPECT".'
          goto error
       end
    
    /*  Cannot delete parent "PROSPECT" if children still exist in "QUALIFPROSP"  */
    if exists (select 1
               from   QUALIFPROSP t2, deleted t1
               where  t2.CDPROSPECT = t1.CDPROSPECT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "QUALIFPROSP". Cannot delete parent "PROSPECT".'
          goto error
       end
    
    /*  Cannot delete parent "PROSPECT" if children still exist in "QUALIFPROSP"  */
    if exists (select 1
               from   QUALIFPROSP t2, deleted t1
               where  t2.CDPROSPECMATRIZ = t1.CDPROSPECT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "QUALIFPROSP". Cannot delete parent "PROSPECT".'
          goto error
       end
    
    /*  Delete all children in "ENDEPROSPECT"  */
    delete ENDEPROSPECT
    from   ENDEPROSPECT t2, deleted t1
    where  t2.CDPROSPECT = t1.CDPROSPECT
    
    /*  Delete all children in "OBSERVPROSP"  */
    delete OBSERVPROSP
    from   OBSERVPROSP t2, deleted t1
    where  t2.CDPROSPECT = t1.CDPROSPECT
    
    /*  Delete all children in "INTEPROSP"  */
    delete INTEPROSP
    from   INTEPROSP t2, deleted t1
    where  t2.CDPROSPECT = t1.CDPROSPECT
    
    /*  Delete all children in "HISTQUALPROS"  */
    delete HISTQUALPROS
    from   HISTQUALPROS t2, deleted t1
    where  t2.CDPROSPECT = t1.CDPROSPECT

    return

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

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

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

    
    /*  Delete all children in "PARTFILI"  */
    delete PARTFILI
    from   PARTFILI t2, deleted t1
    where  t2.CDGRUPPROD = t1.CDGRUPPROD
     and   t2.CDSUBGRPROD = t1.CDSUBGRPROD
     and   t2.CDFILISUBGR = t1.CDFILISUBGR
     and   t2.CDPRODUTO = t1.CDPRODUTO
    
    /*  Delete all children in "PARAMPRODSUP"  */
    delete PARAMPRODSUP
    from   PARAMPRODSUP t2, deleted t1
    where  t2.CDGRUPPROD = t1.CDGRUPPROD
     and   t2.CDSUBGRPROD = t1.CDSUBGRPROD
     and   t2.CDFILISUBGR = t1.CDFILISUBGR
     and   t2.CDPRODUTO = t1.CDPRODUTO

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "REGIAO" if children still exist in "ITEMCOLE"  */
    if exists (select 1
               from   ITEMCOLE t2, deleted t1
               where  t2.CDREGIAO = t1.CDREGIAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMCOLE". Cannot delete parent "REGIAO".'
          goto error
       end
    
    /*  Cannot delete parent "REGIAO" if children still exist in "FILIAL"  */
    if exists (select 1
               from   FILIAL t2, deleted t1
               where  t2.CDREGIAO = t1.CDREGIAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIAL". Cannot delete parent "REGIAO".'
          goto error
       end
    
    /*  Cannot delete parent "REGIAO" if children still exist in "PEDCOLETA"  */
    if exists (select 1
               from   PEDCOLETA t2, deleted t1
               where  t2.CDREGIAO = t1.CDREGIAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDCOLETA". Cannot delete parent "REGIAO".'
          goto error
       end
    
    /*  Cannot delete parent "REGIAO" if children still exist in "MUNICIPIO"  */
    if exists (select 1
               from   MUNICIPIO t2, deleted t1
               where  t2.CDREGIAO = t1.CDREGIAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MUNICIPIO". Cannot delete parent "REGIAO".'
          goto error
       end
    
    /*  Cannot delete parent "REGIAO" if children still exist in "PARAMPRODSUP"  */
    if exists (select 1
               from   PARAMPRODSUP t2, deleted t1
               where  t2.CDREGIAO = t1.CDREGIAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPRODSUP". Cannot delete parent "REGIAO".'
          goto error
       end
    
    /*  Cannot delete parent "REGIAO" if children still exist in "MINFORNREG"  */
    if exists (select 1
               from   MINFORNREG t2, deleted t1
               where  t2.CDREGIAO = t1.CDREGIAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MINFORNREG". Cannot delete parent "REGIAO".'
          goto error
       end
    
    /*  Cannot delete parent "REGIAO" if children still exist in "GSCRESULTADO"  */
    if exists (select 1
               from   GSCRESULTADO t2, deleted t1
               where  t2.CDREGIAO = t1.CDREGIAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GSCRESULTADO". Cannot delete parent "REGIAO".'
          goto error
       end
    
    /*  Cannot delete parent "REGIAO" if children still exist in "ITEXCLISPREC"  */
    if exists (select 1
               from   ITEXCLISPREC t2, deleted t1
               where  t2.CDREGIAO = t1.CDREGIAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEXCLISPREC". Cannot delete parent "REGIAO".'
          goto error
       end
    
    /*  Cannot delete parent "REGIAO" if children still exist in "REGIAOPEDCOL"  */
    if exists (select 1
               from   REGIAOPEDCOL t2, deleted t1
               where  t2.CDREGIAO = t1.CDREGIAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REGIAOPEDCOL". Cannot delete parent "REGIAO".'
          goto error
       end
    
    /*  Cannot delete parent "REGIAO" if children still exist in "ETLCOMPRASGER"  */
    if exists (select 1
               from   ETLCOMPRASGER t2, deleted t1
               where  t2.CDREGIAO = t1.CDREGIAO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCOMPRASGER". Cannot delete parent "REGIAO".'
          goto error
       end
    
    /*  Delete all children in "REFOREG"  */
    delete REFOREG
    from   REFOREG t2, deleted t1
    where  t2.CDREGIAO = t1.CDREGIAO

    return

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

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

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

    
    /*  Delete all children in "NFRELACCDRS"  */
    delete NFRELACCDRS
    from   NFRELACCDRS t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDSERVFILI = t1.CDSERVFILI
     and   t2.NRCDRS = t1.NRCDRS

    return

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

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

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

    
    /*  Delete all children in "COMPRELUSER"  */
    delete COMPRELUSER
    from   COMPRELUSER t2, deleted t1
    where  t2.CDRELATORIO = t1.CDRELATORIO
    
    /*  Delete all children in "RELATIMPOS"  */
    delete RELATIMPOS
    from   RELATIMPOS t2, deleted t1
    where  t2.CDRELATORIO = t1.CDRELATORIO

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "REQUISITPROD" if children still exist in "RELACREQPROD"  */
    if exists (select 1
               from   RELACREQPROD t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODGRUP
                and   t2.CDREQPROD = t1.CDREQPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELACREQPROD". Cannot delete parent "REQUISITPROD".'
          goto error
       end
    
    /*  Cannot delete parent "REQUISITPROD" if children still exist in "RELACREQPROD"  */
    if exists (select 1
               from   RELACREQPROD t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODGRUP
                and   t2.CDREQPRELAC = t1.CDREQPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELACREQPROD". Cannot delete parent "REQUISITPROD".'
          goto error
       end
    
    /*  Cannot delete parent "REQUISITPROD" if children still exist in "PRODREQPROD"  */
    if exists (select 1
               from   PRODREQPROD t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODGRUP
                and   t2.CDREQPROD = t1.CDREQPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODREQPROD". Cannot delete parent "REQUISITPROD".'
          goto error
       end
    
    /*  Cannot delete parent "REQUISITPROD" if children still exist in "EVOLREQPROD"  */
    if exists (select 1
               from   EVOLREQPROD t2, deleted t1
               where  t2.CDPRODGRUP = t1.CDPRODGRUP
                and   t2.CDREQPROD = t1.CDREQPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EVOLREQPROD". Cannot delete parent "REQUISITPROD".'
          goto error
       end
    
    /*  Delete all children in "PCHAVREQPROD"  */
    delete PCHAVREQPROD
    from   PCHAVREQPROD t2, deleted t1
    where  t2.CDPRODUTO = t1.CDPRODGRUP
     and   t2.CDREQPROD = t1.CDREQPROD

    return

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

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

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

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

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "ROMANEIO" if children still exist in "ROMANEIONF"  */
    if exists (select 1
               from   ROMANEIONF t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRROMANEIO = t1.NRROMANEIO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ROMANEIONF". Cannot delete parent "ROMANEIO".'
          goto error
       end
    
    /*  Cannot delete parent "ROMANEIO" if children still exist in "OCORRERFATUR"  */
    if exists (select 1
               from   OCORRERFATUR t2, deleted t1
               where  t2.CDFILIROMA = t1.CDFILIAL
                and   t2.NRROMANEIO = t1.NRROMANEIO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OCORRERFATUR". Cannot delete parent "ROMANEIO".'
          goto error
       end
    
    /*  Delete all children in "ITEMROMANEIO"  */
    delete ITEMROMANEIO
    from   ITEMROMANEIO t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRROMANEIO = t1.NRROMANEIO

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "ROTATRANS" if children still exist in "SERVICO"  */
    if exists (select 1
               from   SERVICO t2, deleted t1
               where  t2.CDROTATRANS = t1.CDROTATRANS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVICO". Cannot delete parent "ROTATRANS".'
          goto error
       end
    
    /*  Cannot delete parent "ROTATRANS" if children still exist in "CLIENROTA"  */
    if exists (select 1
               from   CLIENROTA t2, deleted t1
               where  t2.CDROTATRANS = t1.CDROTATRANS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENROTA". Cannot delete parent "ROTATRANS".'
          goto error
       end
    
    /*  Cannot delete parent "ROTATRANS" if children still exist in "MAPABASEV"  */
    if exists (select 1
               from   MAPABASEV t2, deleted t1
               where  t2.CDROTATRANS = t1.CDROTATRANS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPABASEV". Cannot delete parent "ROTATRANS".'
          goto error
       end
    
    /*  Cannot delete parent "ROTATRANS" if children still exist in "SOLICTROCO"  */
    if exists (select 1
               from   SOLICTROCO t2, deleted t1
               where  t2.CDROTATRANS = t1.CDROTATRANS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICTROCO". Cannot delete parent "ROTATRANS".'
          goto error
       end
    
    /*  Cannot delete parent "ROTATRANS" if children still exist in "TRAJETOROTA"  */
    if exists (select 1
               from   TRAJETOROTA t2, deleted t1
               where  t2.CDROTATRANS = t1.CDROTATRANS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TRAJETOROTA". Cannot delete parent "ROTATRANS".'
          goto error
       end
    
    /*  Cannot delete parent "ROTATRANS" if children still exist in "MUNICIPIROTA"  */
    if exists (select 1
               from   MUNICIPIROTA t2, deleted t1
               where  t2.CDROTATRANS = t1.CDROTATRANS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MUNICIPIROTA". Cannot delete parent "ROTATRANS".'
          goto error
       end
    
    /*  Cannot delete parent "ROTATRANS" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDROTATRANS = t1.CDROTATRANS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "ROTATRANS".'
          goto error
       end
    
    /*  Cannot delete parent "ROTATRANS" if children still exist in "ABASTEROTA"  */
    if exists (select 1
               from   ABASTEROTA t2, deleted t1
               where  t2.CDROTATRANS = t1.CDROTATRANS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ABASTEROTA". Cannot delete parent "ROTATRANS".'
          goto error
       end
    
    /*  Cannot delete parent "ROTATRANS" if children still exist in "PREVABASEV"  */
    if exists (select 1
               from   PREVABASEV t2, deleted t1
               where  t2.CDROTATRANS = t1.CDROTATRANS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVABASEV". Cannot delete parent "ROTATRANS".'
          goto error
       end
    
    /*  Cannot delete parent "ROTATRANS" if children still exist in "FILIALROTA"  */
    if exists (select 1
               from   FILIALROTA t2, deleted t1
               where  t2.CDROTATRANS = t1.CDROTATRANS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIALROTA". Cannot delete parent "ROTATRANS".'
          goto error
       end
    
    /*  Delete all children in "CALEABAS"  */
    delete CALEABAS
    from   CALEABAS t2, deleted t1
    where  t2.CDROTATRANS = t1.CDROTATRANS
    
    /*  Delete all children in "CALENTREGA"  */
    delete CALENTREGA
    from   CALENTREGA t2, deleted t1
    where  t2.CDROTATRANS = t1.CDROTATRANS

    return

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

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

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

    
    /*  Cannot delete parent "SALAOEVENTO" if children still exist in "ORCAEVENTO"  */
    if exists (select 1
               from   ORCAEVENTO t2, deleted t1
               where  t2.CDSALAOEVEN = t1.CDSALAOEVEN)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAEVENTO". Cannot delete parent "SALAOEVENTO".'
          goto error
       end
    
    /*  Cannot delete parent "SALAOEVENTO" if children still exist in "AGENDASALAO"  */
    if exists (select 1
               from   AGENDASALAO t2, deleted t1
               where  t2.CDSALAOEVEN = t1.CDSALAOEVEN)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGENDASALAO". Cannot delete parent "SALAOEVENTO".'
          goto error
       end
    
    /*  Delete all children in "ENDESALAOEVEN"  */
    delete ENDESALAOEVEN
    from   ENDESALAOEVEN t2, deleted t1
    where  t2.CDSALAOEVEN = t1.CDSALAOEVEN
    
    /*  Delete all children in "SALAOSUBGRPROD"  */
    delete SALAOSUBGRPROD
    from   SALAOSUBGRPROD t2, deleted t1
    where  t2.CDSALAOEVEN = t1.CDSALAOEVEN

    return

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

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

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

    
    /*  Cannot delete parent "SEGMERCADO" if children still exist in "CLIENTE"  */
    if exists (select 1
               from   CLIENTE t2, deleted t1
               where  t2.CDSEGMER = t1.CDSEGMER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENTE". Cannot delete parent "SEGMERCADO".'
          goto error
       end
    
    /*  Cannot delete parent "SEGMERCADO" if children still exist in "PROSPECT"  */
    if exists (select 1
               from   PROSPECT t2, deleted t1
               where  t2.CDSEGMER = t1.CDSEGMER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROSPECT". Cannot delete parent "SEGMERCADO".'
          goto error
       end
    
    /*  Cannot delete parent "SEGMERCADO" if children still exist in "FILIAL"  */
    if exists (select 1
               from   FILIAL t2, deleted t1
               where  t2.CDSEGMER = t1.CDSEGMER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIAL". Cannot delete parent "SEGMERCADO".'
          goto error
       end
    
    /*  Cannot delete parent "SEGMERCADO" if children still exist in "PRATO"  */
    if exists (select 1
               from   PRATO t2, deleted t1
               where  t2.CDSEGMER = t1.CDSEGMER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRATO". Cannot delete parent "SEGMERCADO".'
          goto error
       end
    
    /*  Cannot delete parent "SEGMERCADO" if children still exist in "NIVELPRODSEG"  */
    if exists (select 1
               from   NIVELPRODSEG t2, deleted t1
               where  t2.CDSEGMER = t1.CDSEGMER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NIVELPRODSEG". Cannot delete parent "SEGMERCADO".'
          goto error
       end
    
    /*  Cannot delete parent "SEGMERCADO" if children still exist in "FORNECEDOR"  */
    if exists (select 1
               from   FORNECEDOR t2, deleted t1
               where  t2.CDSEGMER = t1.CDSEGMER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORNECEDOR". Cannot delete parent "SEGMERCADO".'
          goto error
       end
    
    /*  Cannot delete parent "SEGMERCADO" if children still exist in "MOVOCORR"  */
    if exists (select 1
               from   MOVOCORR t2, deleted t1
               where  t2.CDSEGMER = t1.CDSEGMER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOCORR". Cannot delete parent "SEGMERCADO".'
          goto error
       end
    
    /*  Cannot delete parent "SEGMERCADO" if children still exist in "FATORESUFAT"  */
    if exists (select 1
               from   FATORESUFAT t2, deleted t1
               where  t2.CDSEGMER = t1.CDSEGMER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FATORESUFAT". Cannot delete parent "SEGMERCADO".'
          goto error
       end
    
    /*  Cannot delete parent "SEGMERCADO" if children still exist in "ETL_RESUGER"  */
    if exists (select 1
               from   ETL_RESUGER t2, deleted t1
               where  t2.CDSEGMER = t1.CDSEGMER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUGER". Cannot delete parent "SEGMERCADO".'
          goto error
       end
    
    /*  Cannot delete parent "SEGMERCADO" if children still exist in "ETL_RESUFOOD"  */
    if exists (select 1
               from   ETL_RESUFOOD t2, deleted t1
               where  t2.CDSEGMER = t1.CDSEGMER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUFOOD". Cannot delete parent "SEGMERCADO".'
          goto error
       end
    
    /*  Cannot delete parent "SEGMERCADO" if children still exist in "ETL_RESUESTO"  */
    if exists (select 1
               from   ETL_RESUESTO t2, deleted t1
               where  t2.CDSEGMER = t1.CDSEGMER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUESTO". Cannot delete parent "SEGMERCADO".'
          goto error
       end
    
    /*  Cannot delete parent "SEGMERCADO" if children still exist in "ETL_RESUSERV"  */
    if exists (select 1
               from   ETL_RESUSERV t2, deleted t1
               where  t2.CDSEGMER = t1.CDSEGMER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETL_RESUSERV". Cannot delete parent "SEGMERCADO".'
          goto error
       end
    
    /*  Cannot delete parent "SEGMERCADO" if children still exist in "ETLDRO"  */
    if exists (select 1
               from   ETLDRO t2, deleted t1
               where  t2.CDSEGMER = t1.CDSEGMER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLDRO". Cannot delete parent "SEGMERCADO".'
          goto error
       end
    
    /*  Delete all children in "PRODUTOSEG"  */
    delete PRODUTOSEG
    from   PRODUTOSEG t2, deleted t1
    where  t2.CDSEGMER = t1.CDSEGMER

    return

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

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

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

    
    /*  Cannot delete parent "SERIENF" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDINSCMUNI = t1.CDINSCMUNI
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA
                and   t2.CDSERIE = t1.CDSERIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "SERIENF".'
          goto error
       end
    
    /*  Cannot delete parent "SERIENF" if children still exist in "CIAFILI"  */
    if exists (select 1
               from   CIAFILI t2, deleted t1
               where  t2.CDINSCMUNI = t1.CDINSCMUNI
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA
                and   t2.CDSERIE = t1.CDSERIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CIAFILI". Cannot delete parent "SERIENF".'
          goto error
       end
    
    /*  Cannot delete parent "SERIENF" if children still exist in "ITREPLICDOC"  */
    if exists (select 1
               from   ITREPLICDOC t2, deleted t1
               where  t2.CDINSCMUNI = t1.CDINSCMUNI
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA
                and   t2.CDSERIE = t1.CDSERIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITREPLICDOC". Cannot delete parent "SERIENF".'
          goto error
       end
    
    /*  Cannot delete parent "SERIENF" if children still exist in "SERVCONTRAT"  */
    if exists (select 1
               from   SERVCONTRAT t2, deleted t1
               where  t2.CDINSCMUNI = t1.CDINSCMUNI
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA
                and   t2.CDSERIE = t1.CDSERIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCONTRAT". Cannot delete parent "SERIENF".'
          goto error
       end
    
    /*  Cannot delete parent "SERIENF" if children still exist in "GERANFROMA"  */
    if exists (select 1
               from   GERANFROMA t2, deleted t1
               where  t2.CDINSCMUNI = t1.CDINSCMUNI
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA
                and   t2.CDSERIE = t1.CDSERIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERANFROMA". Cannot delete parent "SERIENF".'
          goto error
       end
    
    /*  Cannot delete parent "SERIENF" if children still exist in "EMPRCNTRFIL"  */
    if exists (select 1
               from   EMPRCNTRFIL t2, deleted t1
               where  t2.CDINSCMUNI = t1.CDINSCMUNI
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA
                and   t2.CDSERIE = t1.CDSERIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EMPRCNTRFIL". Cannot delete parent "SERIENF".'
          goto error
       end
    
    /*  Cannot delete parent "SERIENF" if children still exist in "FILIAL"  */
    if exists (select 1
               from   FILIAL t2, deleted t1
               where  t2.CDINSCMUNI = t1.CDINSCMUNI
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA
                and   t2.CDSERIE = t1.CDSERIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FILIAL". Cannot delete parent "SERIENF".'
          goto error
       end
    
    /*  Cannot delete parent "SERIENF" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDINSCMUNI = t1.CDINSCMUNI
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA
                and   t2.CDSERIEEMIS = t1.CDSERIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "SERIENF".'
          goto error
       end
    
    /*  Cannot delete parent "SERIENF" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDINSCMUNI = t1.CDINSCMUNI
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA
                and   t2.CDSERIE = t1.CDSERIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "SERIENF".'
          goto error
       end
    
    /*  Cannot delete parent "SERIENF" if children still exist in "PARAVEND"  */
    if exists (select 1
               from   PARAVEND t2, deleted t1
               where  t2.CDINSCMUNI = t1.CDINSCMUNI
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA
                and   t2.CDSERIE = t1.CDSERIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAVEND". Cannot delete parent "SERIENF".'
          goto error
       end
    
    /*  Cannot delete parent "SERIENF" if children still exist in "PARAMSERIE"  */
    if exists (select 1
               from   PARAMSERIE t2, deleted t1
               where  t2.CDINSCMUNI = t1.CDINSCMUNI
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA
                and   t2.CDSERIE = t1.CDSERIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMSERIE". Cannot delete parent "SERIENF".'
          goto error
       end
    
    /*  Cannot delete parent "SERIENF" if children still exist in "PARAVEND"  */
    if exists (select 1
               from   PARAVEND t2, deleted t1
               where  t2.CDINSCMUNI = t1.CDINSCMUNI
                and   t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDINSCESTA = t1.CDINSCESTA
                and   t2.CDSERIECNTREC = t1.CDSERIE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAVEND". Cannot delete parent "SERIENF".'
          goto error
       end
    
    /*  Delete all children in "IMPOSERIE"  */
    delete IMPOSERIE
    from   IMPOSERIE t2, deleted t1
    where  t2.CDINSCMUNI = t1.CDINSCMUNI
     and   t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDINSCESTA = t1.CDINSCESTA
     and   t2.CDSERIE = t1.CDSERIE

    return

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

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

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

    
    /*  Cannot delete parent "SERVCLIE" if children still exist in "HISTSERVFATU"  */
    if exists (select 1
               from   HISTSERVFATU t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HISTSERVFATU". Cannot delete parent "SERVCLIE".'
          goto error
       end
    
    /*  Cannot delete parent "SERVCLIE" if children still exist in "RELAT_CDRS"  */
    if exists (select 1
               from   RELAT_CDRS t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELAT_CDRS". Cannot delete parent "SERVCLIE".'
          goto error
       end
    
    /*  Cannot delete parent "SERVCLIE" if children still exist in "EFETCDRS"  */
    if exists (select 1
               from   EFETCDRS t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFETCDRS". Cannot delete parent "SERVCLIE".'
          goto error
       end
    
    /*  Cannot delete parent "SERVCLIE" if children still exist in "EFETRATCDRS"  */
    if exists (select 1
               from   EFETRATCDRS t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFETRATCDRS". Cannot delete parent "SERVCLIE".'
          goto error
       end
    
    /*  Cannot delete parent "SERVCLIE" if children still exist in "SERVCLIETURN"  */
    if exists (select 1
               from   SERVCLIETURN t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCLIETURN". Cannot delete parent "SERVCLIE".'
          goto error
       end
    
    /*  Delete all children in "CORRPRECSERV"  */
    delete CORRPRECSERV
    from   CORRPRECSERV t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDSERVFILI = t1.CDSERVFILI

    return

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

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

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

    
    /*  Cannot delete parent "SERVCONTRAT" if children still exist in "PREPSERVFATU"  */
    if exists (select 1
               from   PREPSERVFATU t2, deleted t1
               where  t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.CDSERVCONT = t1.CDSERVCONT)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREPSERVFATU". Cannot delete parent "SERVCONTRAT".'
          goto error
       end
    
    /*  Delete all children in "PZPGSERCONT"  */
    delete PZPGSERCONT
    from   PZPGSERCONT t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDSERVCONT = t1.CDSERVCONT
    
    /*  Delete all children in "VALSERVCONT"  */
    delete VALSERVCONT
    from   VALSERVCONT t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDSERVCONT = t1.CDSERVCONT
    
    /*  Delete all children in "SERVFATU"  */
    delete SERVFATU
    from   SERVFATU t2, deleted t1
    where  t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.CDSERVCONT = t1.CDSERVCONT

    return

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

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

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

    
    /*  Cannot delete parent "SERVICO" if children still exist in "SERVCLIE"  */
    if exists (select 1
               from   SERVCLIE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCLIE". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "FOTOPRASER"  */
    if exists (select 1
               from   FOTOPRASER t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FOTOPRASER". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "PRATSERV"  */
    if exists (select 1
               from   PRATSERV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRATSERV". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "RECESERV"  */
    if exists (select 1
               from   RECESERV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECESERV". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "EFETCLIE"  */
    if exists (select 1
               from   EFETCLIE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFETCLIE". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "LIMPESER"  */
    if exists (select 1
               from   LIMPESER t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LIMPESER". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "RESTPROD"  */
    if exists (select 1
               from   RESTPROD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESTPROD". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "LIMNUTRI"  */
    if exists (select 1
               from   LIMNUTRI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LIMNUTRI". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "LIMPEPRA"  */
    if exists (select 1
               from   LIMPEPRA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LIMPEPRA". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "RESTOPCA"  */
    if exists (select 1
               from   RESTOPCA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESTOPCA". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "FREQOPCA"  */
    if exists (select 1
               from   FREQOPCA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FREQOPCA". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "RESTPRAT"  */
    if exists (select 1
               from   RESTPRAT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESTPRAT". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "FREQPRAT"  */
    if exists (select 1
               from   FREQPRAT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FREQPRAT". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "PREVCONS"  */
    if exists (select 1
               from   PREVCONS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCONS". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "PREVRETI"  */
    if exists (select 1
               from   PREVRETI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVRETI". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "DIASEMCONS"  */
    if exists (select 1
               from   DIASEMCONS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIASEMCONS". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "CONSSERV"  */
    if exists (select 1
               from   CONSSERV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSSERV". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "CNFCAMES"  */
    if exists (select 1
               from   CNFCAMES t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CNFCAMES". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "CARDADIA"  */
    if exists (select 1
               from   CARDADIA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDADIA". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "SERVOPER"  */
    if exists (select 1
               from   SERVOPER t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVOPER". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "RATENESTO"  */
    if exists (select 1
               from   RATENESTO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RATENESTO". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "PREPPRASER"  */
    if exists (select 1
               from   PREPPRASER t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREPPRASER". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "GRUPSERVFAT"  */
    if exists (select 1
               from   GRUPSERVFAT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERAJEFEGSF = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GRUPSERVFAT". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "EFECLIFAT"  */
    if exists (select 1
               from   EFECLIFAT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFECLIFAT". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "DIETSERV"  */
    if exists (select 1
               from   DIETSERV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIETSERV". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "LOGPREVRETI"  */
    if exists (select 1
               from   LOGPREVRETI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGPREVRETI". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "UTILPRATSEM"  */
    if exists (select 1
               from   UTILPRATSEM t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "UTILPRATSEM". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "CARDPACI"  */
    if exists (select 1
               from   CARDPACI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDPACI". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "LOGSERVTRAN"  */
    if exists (select 1
               from   LOGSERVTRAN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGSERVTRAN". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "SERVTRAN"  */
    if exists (select 1
               from   SERVTRAN t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVTRAN". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "SERVREAL"  */
    if exists (select 1
               from   SERVREAL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVREAL". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "CATRSERV"  */
    if exists (select 1
               from   CATRSERV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CATRSERV". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "MENSREQU"  */
    if exists (select 1
               from   MENSREQU t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MENSREQU". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "ITINQUERPACI"  */
    if exists (select 1
               from   ITINQUERPACI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITINQUERPACI". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "MOVICOME"  */
    if exists (select 1
               from   MOVICOME t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVICOME". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "PAROPERSERV"  */
    if exists (select 1
               from   PAROPERSERV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PAROPERSERV". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "SERVINTE"  */
    if exists (select 1
               from   SERVINTE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVINTE". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "PLCALOSERV"  */
    if exists (select 1
               from   PLCALOSERV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PLCALOSERV". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "PARAVEND"  */
    if exists (select 1
               from   PARAVEND t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAVEND". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "VENDA"  */
    if exists (select 1
               from   VENDA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDA". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "RESTITREFTUR"  */
    if exists (select 1
               from   RESTITREFTUR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESTITREFTUR". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "DESTSRVCLI"  */
    if exists (select 1
               from   DESTSRVCLI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DESTSRVCLI". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "CARDPADR"  */
    if exists (select 1
               from   CARDPADR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDPADR". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "CARDCONSNUTR"  */
    if exists (select 1
               from   CARDCONSNUTR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDCONSNUTR". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "SERVTURCAIXA"  */
    if exists (select 1
               from   SERVTURCAIXA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVTURCAIXA". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "SUBSTIPOCONS"  */
    if exists (select 1
               from   SUBSTIPOCONS t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SUBSTIPOCONS". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "CARDIAPME"  */
    if exists (select 1
               from   CARDIAPME t2, deleted t1
               where  t2.CDFILCENTPME = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDIAPME". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "CONFCARDPME"  */
    if exists (select 1
               from   CONFCARDPME t2, deleted t1
               where  t2.CDFILCENTPME = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONFCARDPME". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "SERVTIPOCARD"  */
    if exists (select 1
               from   SERVTIPOCARD t2, deleted t1
               where  t2.CDFILCENTPME = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVTIPOCARD". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "PARAMPLANEJA"  */
    if exists (select 1
               from   PARAMPLANEJA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVRETPME = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPLANEJA". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "ITEMPRODUTO"  */
    if exists (select 1
               from   ITEMPRODUTO t2, deleted t1
               where  t2.CDFILIAPUR = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMPRODUTO". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "EFETDIET"  */
    if exists (select 1
               from   EFETDIET t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EFETDIET". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "CONSSERVPME"  */
    if exists (select 1
               from   CONSSERVPME t2, deleted t1
               where  t2.CDFILCENTPME = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSSERVPME". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "DIASEMCONSPME"  */
    if exists (select 1
               from   DIASEMCONSPME t2, deleted t1
               where  t2.CDFILCENTPME = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "DIASEMCONSPME". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "FATORESUFAT"  */
    if exists (select 1
               from   FATORESUFAT t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FATORESUFAT". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "SERVICO"  */
    if exists (select 1
               from   SERVICO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVPREQUI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVICO". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "CTRLAPROVCARD"  */
    if exists (select 1
               from   CTRLAPROVCARD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTRLAPROVCARD". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "LOGAPROVCARD"  */
    if exists (select 1
               from   LOGAPROVCARD t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGAPROVCARD". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "COMPCARDDEL"  */
    if exists (select 1
               from   COMPCARDDEL t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPCARDDEL". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "SERVICOROP"  */
    if exists (select 1
               from   SERVICOROP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVICOROP". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "ESTATDIET"  */
    if exists (select 1
               from   ESTATDIET t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ESTATDIET". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Cannot delete parent "SERVICO" if children still exist in "CARDAMES"  */
    if exists (select 1
               from   CARDAMES t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CARDAMES". Cannot delete parent "SERVICO".'
          goto error
       end
    
    /*  Delete all children in "TURNOSERV"  */
    delete TURNOSERV
    from   TURNOSERV t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDSERVFILI = t1.CDSERVFILI
    
    /*  Delete all children in "CUSTPADR"  */
    delete CUSTPADR
    from   CUSTPADR t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDSERVFILI = t1.CDSERVFILI
    
    /*  Delete all children in "CONFCARD"  */
    delete CONFCARD
    from   CONFCARD t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDSERVFILI = t1.CDSERVFILI
    
    /*  Delete all children in "LOGPREVCONS"  */
    delete LOGPREVCONS
    from   LOGPREVCONS t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDSERVFILI = t1.CDSERVFILI

    return

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

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

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

    
    /*  Delete all children in "CONFESCO"  */
    delete CONFESCO
    from   CONFESCO t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDPACIENTE = t1.CDPACIENTE
     and   t2.DTSERVINTE = t1.DTSERVINTE
     and   t2.NRSEQUSERV = t1.NRSEQUSERV

    return

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

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

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

    
    /*  Cannot delete parent "SERVPREST" if children still exist in "APROATIVCSA"  */
    if exists (select 1
               from   APROATIVCSA t2, deleted t1
               where  t2.CDEMPRESA = t1.CDEMPRESA
                and   t2.CDFUNC = t1.CDFUNC
                and   t2.CDCLIENTE = t1.CDCLIENTE
                and   t2.NRLICENCAUSO = t1.NRLICENCAUSO
                and   t2.DTSERVPREST = t1.DTSERVPREST)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APROATIVCSA". Cannot delete parent "SERVPREST".'
          goto error
       end
    
    /*  Delete all children in "DESCSERVPRES"  */
    delete DESCSERVPRES
    from   DESCSERVPRES t2, deleted t1
    where  t2.CDEMPRESA = t1.CDEMPRESA
     and   t2.CDFUNC = t1.CDFUNC
     and   t2.CDCLIENTE = t1.CDCLIENTE
     and   t2.NRLICENCAUSO = t1.NRLICENCAUSO
     and   t2.DTSERVPREST = t1.DTSERVPREST

    return

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

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

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

    
    /*  Delete all children in "SGPREVPAXPROD"  */
    delete SGPREVPAXPROD
    from   SGPREVPAXPROD t2, deleted t1
    where  t2.CDCLASSE = t1.CDCLASSE
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDCIA = t1.CDCIA
     and   t2.NRVOO = t1.NRVOO
     and   t2.CDTRECHO = t1.CDTRECHO
     and   t2.DTVOO = t1.DTVOO
     and   t2.CDSERVICO = t1.CDPRODUTO

    return

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

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

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

    
    /*  Cannot delete parent "SERVTIPOCARD" if children still exist in "CONFPADTCARD"  */
    if exists (select 1
               from   CONFPADTCARD t2, deleted t1
               where  t2.CDFILCENTPME = t1.CDFILCENTPME
                and   t2.CDTIPOCARD = t1.CDTIPOCARD
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONFPADTCARD". Cannot delete parent "SERVTIPOCARD".'
          goto error
       end
    
    /*  Cannot delete parent "SERVTIPOCARD" if children still exist in "CUSTPADTCARD"  */
    if exists (select 1
               from   CUSTPADTCARD t2, deleted t1
               where  t2.CDFILCENTPME = t1.CDFILCENTPME
                and   t2.CDTIPOCARD = t1.CDTIPOCARD
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CUSTPADTCARD". Cannot delete parent "SERVTIPOCARD".'
          goto error
       end
    
    /*  Cannot delete parent "SERVTIPOCARD" if children still exist in "SERVCLIEPME"  */
    if exists (select 1
               from   SERVCLIEPME t2, deleted t1
               where  t2.CDFILCENTPME = t1.CDFILCENTPME
                and   t2.CDTIPOCARD = t1.CDTIPOCARD
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVCLIEPME". Cannot delete parent "SERVTIPOCARD".'
          goto error
       end
    
    /*  Cannot delete parent "SERVTIPOCARD" if children still exist in "PREVCONSPME"  */
    if exists (select 1
               from   PREVCONSPME t2, deleted t1
               where  t2.CDFILCENTPME = t1.CDFILCENTPME
                and   t2.CDTIPOCARD = t1.CDTIPOCARD
                and   t2.CDSERVFILI = t1.CDSERVFILI)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCONSPME". Cannot delete parent "SERVTIPOCARD".'
          goto error
       end
    
    /*  Delete all children in "COMESERVTCAR"  */
    delete COMESERVTCAR
    from   COMESERVTCAR t2, deleted t1
    where  t2.CDFILCENTPME = t1.CDFILCENTPME
     and   t2.CDTIPOCARD = t1.CDTIPOCARD
     and   t2.CDSERVFILI = t1.CDSERVFILI

    return

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

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

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

    
    /*  Delete all children in "ETIQUETRAN"  */
    delete ETIQUETRAN
    from   ETIQUETRAN t2, deleted t1
    where  t2.CDFILAUXPRAT = t1.CDFILAUXPRAT
     and   t2.CDPRATO = t1.CDPRATO
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDSERVFILI = t1.CDSERVFILI
     and   t2.CDDIETPADST = t1.CDDIETPADST
     and   t2.DTSERVTRAN = t1.DTSERVTRAN
     and   t2.NRSERVTRAN = t1.NRSERVTRAN

    return

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

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

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

    
    /*  Delete all children in "RECEPREVPAXPROD"  */
    delete RECEPREVPAXPROD
    from   RECEPREVPAXPROD t2, deleted t1
    where  t2.CDCLASSE = t1.CDCLASSE
     and   t2.CDFILIAL = t1.CDFILIAL
     and   t2.CDCIA = t1.CDCIA
     and   t2.NRVOO = t1.NRVOO
     and   t2.CDTRECHO = t1.CDTRECHO
     and   t2.DTVOO = t1.DTVOO
     and   t2.CDSERVICO = t1.CDSERVICO
     and   t2.CDSUBGRUPO = t1.CDSUBGRUPO

    return

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

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

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

    
    /*  Cannot delete parent "SOLICITA" if children still exist in "PEDIDO"  */
    if exists (select 1
               from   PEDIDO t2, deleted t1
               where  t2.CDFILLANCA = t1.CDFILLANCA
                and   t2.CDFILSOLI = t1.CDFILSOLI
                and   t2.NRSOLICMP = t1.NRSOLICMP)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDO". Cannot delete parent "SOLICITA".'
          goto error
       end
    
    /*  Delete all children in "ITEMSOLI"  */
    delete ITEMSOLI
    from   ITEMSOLI t2, deleted t1
    where  t2.CDFILLAN = t1.CDFILLANCA
     and   t2.CDFILSOLI = t1.CDFILSOLI
     and   t2.NRSOLICMP = t1.NRSOLICMP
    
    /*  Delete all children in "MOVSOLEXT"  */
    delete MOVSOLEXT
    from   MOVSOLEXT t2, deleted t1
    where  t2.CDFILLANCEXT = t1.CDFILLANCA
     and   t2.CDFILSOLEXT = t1.CDFILSOLI
     and   t2.NRSOLIMOVEXT = t1.NRSOLICMP

    return

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

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

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

    
    /*  Cannot delete parent "SOLISERV" if children still exist in "TARESERV"  */
    if exists (select 1
               from   TARESERV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TARESERV". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "LOGFASETRAM"  */
    if exists (select 1
               from   LOGFASETRAM t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGFASETRAM". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "LOGDATACOMP"  */
    if exists (select 1
               from   LOGDATACOMP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGDATACOMP". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "COMISSAOFUNC"  */
    if exists (select 1
               from   COMISSAOFUNC t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMISSAOFUNC". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "SERVFATU"  */
    if exists (select 1
               from   SERVFATU t2, deleted t1
               where  t2.CDFILISOLI = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SERVFATU". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "LOGDATAPREV"  */
    if exists (select 1
               from   LOGDATAPREV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGDATAPREV". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "APRHRTRAB"  */
    if exists (select 1
               from   APRHRTRAB t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APRHRTRAB". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "ATIVAPRHR"  */
    if exists (select 1
               from   ATIVAPRHR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ATIVAPRHR". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "SOLIPATCH"  */
    if exists (select 1
               from   SOLIPATCH t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLIPATCH". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "SOLIPATCHPRO"  */
    if exists (select 1
               from   SOLIPATCHPRO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLIPATCHPRO". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "EVOLREQSOLI"  */
    if exists (select 1
               from   EVOLREQSOLI t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EVOLREQSOLI". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "ORCASOLICITA"  */
    if exists (select 1
               from   ORCASOLICITA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCASOLICITA". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "SOLIBASELINE"  */
    if exists (select 1
               from   SOLIBASELINE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLIBASELINE". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "TAREFA"  */
    if exists (select 1
               from   TAREFA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAREFA". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "PRODSOLISERV"  */
    if exists (select 1
               from   PRODSOLISERV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODSOLISERV". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "OBSOLIOPER"  */
    if exists (select 1
               from   OBSOLIOPER t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "OBSOLIOPER". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "LOGSOLISLA"  */
    if exists (select 1
               from   LOGSOLISLA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGSOLISLA". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "SOLICGERTARE"  */
    if exists (select 1
               from   SOLICGERTARE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICGERTARE". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "PARCSOLISERV"  */
    if exists (select 1
               from   PARCSOLISERV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARCSOLISERV". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "CRONOPROJETO"  */
    if exists (select 1
               from   CRONOPROJETO t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CRONOPROJETO". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLIORIG = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "ETLRESULTDES"  */
    if exists (select 1
               from   ETLRESULTDES t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLRESULTDES". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "LOGEMAILGEP"  */
    if exists (select 1
               from   LOGEMAILGEP t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGEMAILGEP". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Cannot delete parent "SOLISERV" if children still exist in "SOLGRACORDOPROJ"  */
    if exists (select 1
               from   SOLGRACORDOPROJ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLGRACORDOPROJ". Cannot delete parent "SOLISERV".'
          goto error
       end
    
    /*  Delete all children in "DESCSOLI"  */
    delete DESCSOLI
    from   DESCSOLI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRSOLISERV = t1.NRSOLISERV
    
    /*  Delete all children in "IMPLSOLI"  */
    delete IMPLSOLI
    from   IMPLSOLI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRSOLISERV = t1.NRSOLISERV
    
    /*  Delete all children in "ESPESOLI"  */
    delete ESPESOLI
    from   ESPESOLI t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRSOLISERV = t1.NRSOLISERV

    return

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

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

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

    
    /*  Delete all children in "PRODACABADO"  */
    delete PRODACABADO
    from   PRODACABADO t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRORDPROD = t1.NRORDPROD
     and   t2.NRSUBORDPROD = t1.NRSUBORDPROD
     and   t2.CDSUBPRODETA = t1.CDSUBPRODETA
     and   t2.NRSUBPRETAOP = t1.NRSUBPRETAOP

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "TABELADW" if children still exist in "ITEMDW"  */
    if exists (select 1
               from   ITEMDW t2, deleted t1
               where  t2.CDTABDRILL = t1.CDTABELADW)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMDW". Cannot delete parent "TABELADW".'
          goto error
       end
    
    /*  Delete all children in "ITEMDW"  */
    delete ITEMDW
    from   ITEMDW t2, deleted t1
    where  t2.CDTABELADW = t1.CDTABELADW
    
    /*  Delete all children in "DIMENSAODW"  */
    delete DIMENSAODW
    from   DIMENSAODW t2, deleted t1
    where  t2.CDTABELADW = t1.CDTABELADW
    
    /*  Delete all children in "ITEMDRILL"  */
    delete ITEMDRILL
    from   ITEMDRILL t2, deleted t1
    where  t2.CDTABDRILL = t1.CDTABELADW
    
    /*  Delete all children in "DIMENSAODRILL"  */
    delete DIMENSAODRILL
    from   DIMENSAODRILL t2, deleted t1
    where  t2.CDTABDRILL = t1.CDTABELADW

    return

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

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

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

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

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "TAREFA" if children still exist in "PLANATIVTARE"  */
    if exists (select 1
               from   PLANATIVTARE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PLANATIVTARE". Cannot delete parent "TAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "TAREFA" if children still exist in "MOVOCORR"  */
    if exists (select 1
               from   MOVOCORR t2, deleted t1
               where  t2.CDFILILANCMO = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVOCORR". Cannot delete parent "TAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "TAREFA" if children still exist in "TAREFA"  */
    if exists (select 1
               from   TAREFA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFAORIG = t1.NRTAREFA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TAREFA". Cannot delete parent "TAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "TAREFA" if children still exist in "RELACTAREFA"  */
    if exists (select 1
               from   RELACTAREFA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELACTAREFA". Cannot delete parent "TAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "TAREFA" if children still exist in "RELACTAREFA"  */
    if exists (select 1
               from   RELACTAREFA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTARERELAC = t1.NRTAREFA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELACTAREFA". Cannot delete parent "TAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "TAREFA" if children still exist in "ASSINATAREFA"  */
    if exists (select 1
               from   ASSINATAREFA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ASSINATAREFA". Cannot delete parent "TAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "TAREFA" if children still exist in "UTILBANCSOLU"  */
    if exists (select 1
               from   UTILBANCSOLU t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "UTILBANCSOLU". Cannot delete parent "TAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "TAREFA" if children still exist in "SOLICGERTARE"  */
    if exists (select 1
               from   SOLICGERTARE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLICGERTARE". Cannot delete parent "TAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "TAREFA" if children still exist in "INDMONITPROJ"  */
    if exists (select 1
               from   INDMONITPROJ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INDMONITPROJ". Cannot delete parent "TAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "TAREFA" if children still exist in "RISCOMONIPRJ"  */
    if exists (select 1
               from   RISCOMONIPRJ t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RISCOMONIPRJ". Cannot delete parent "TAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "TAREFA" if children still exist in "AGEATIVISALA"  */
    if exists (select 1
               from   AGEATIVISALA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AGEATIVISALA". Cannot delete parent "TAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "TAREFA" if children still exist in "RASTREABTARE"  */
    if exists (select 1
               from   RASTREABTARE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RASTREABTARE". Cannot delete parent "TAREFA".'
          goto error
       end
    
    /*  Cannot delete parent "TAREFA" if children still exist in "MOVPESQUISA"  */
    if exists (select 1
               from   MOVPESQUISA t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRTAREFA = t1.NRTAREFA)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MOVPESQUISA". Cannot delete parent "TAREFA".'
          goto error
       end
    
    /*  Delete all children in "DESCTAREFA"  */
    delete DESCTAREFA
    from   DESCTAREFA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRTAREFA = t1.NRTAREFA
    
    /*  Delete all children in "ITEMTAREFA"  */
    delete ITEMTAREFA
    from   ITEMTAREFA t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRTAREFA = t1.NRTAREFA

    return

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

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

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

    
    /*  Cannot delete parent "TARESERV" if children still exist in "TESTTARE"  */
    if exists (select 1
               from   TESTTARE t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV
                and   t2.NRTARESERV = t1.NRTARESERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TESTTARE". Cannot delete parent "TARESERV".'
          goto error
       end
    
    /*  Cannot delete parent "TARESERV" if children still exist in "APRHRTRAB"  */
    if exists (select 1
               from   APRHRTRAB t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV
                and   t2.NRTARESERV = t1.NRTARESERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "APRHRTRAB". Cannot delete parent "TARESERV".'
          goto error
       end
    
    /*  Cannot delete parent "TARESERV" if children still exist in "ATIVAPRHR"  */
    if exists (select 1
               from   ATIVAPRHR t2, deleted t1
               where  t2.CDFILIAL = t1.CDFILIAL
                and   t2.NRSOLISERV = t1.NRSOLISERV
                and   t2.NRTARESERV = t1.NRTARESERV)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ATIVAPRHR". Cannot delete parent "TARESERV".'
          goto error
       end
    
    /*  Delete all children in "DESCTARE"  */
    delete DESCTARE
    from   DESCTARE t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRSOLISERV = t1.NRSOLISERV
     and   t2.NRTARESERV = t1.NRTARESERV
    
    /*  Delete all children in "IMPLTARE"  */
    delete IMPLTARE
    from   IMPLTARE t2, deleted t1
    where  t2.CDFILIAL = t1.CDFILIAL
     and   t2.NRSOLISERV = t1.NRSOLISERV
     and   t2.NRTARESERV = t1.NRTARESERV

    return

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

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

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

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

    return

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

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

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

    
    /*  Delete all children in "COMPPROD"  */
    delete COMPPROD
    from   COMPPROD t2, deleted t1
    where  t2.CDTIPOCOMP = t1.CDTIPOCOMP
     and   t2.CDPRODUTO = t1.CDPRODUTO
     and   t2.CDESPEPROD = t1.CDESPEPROD

    return

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

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

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

    
    /*  Cannot delete parent "TESTEPROD" if children still exist in "TESTEPROD"  */
    if exists (select 1
               from   TESTEPROD t2, deleted t1
               where  t2.NRTESPRDORIG = t1.NRTESTEPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TESTEPROD". Cannot delete parent "TESTEPROD".'
          goto error
       end
    
    /*  Cannot delete parent "TESTEPROD" if children still exist in "QFOMARPROD"  */
    if exists (select 1
               from   QFOMARPROD t2, deleted t1
               where  t2.NRTESTEPROD = t1.NRTESTEPROD)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "QFOMARPROD". Cannot delete parent "TESTEPROD".'
          goto error
       end
    
    /*  Delete all children in "TPPRATTEST"  */
    delete TPPRATTEST
    from   TPPRATTEST t2, deleted t1
    where  t2.NRTESTEPROD = t1.NRTESTEPROD
    
    /*  Delete all children in "MARCTESTPR"  */
    delete MARCTESTPR
    from   MARCTESTPR t2, deleted t1
    where  t2.NRTESTEPROD = t1.NRTESTEPROD
    
    /*  Delete all children in "CUSBENTEST"  */
    delete CUSBENTEST
    from   CUSBENTEST t2, deleted t1
    where  t2.NRTESTEPROD = t1.NRTESTEPROD
    
    /*  Delete all children in "OBSTESTPROD"  */
    delete OBSTESTPROD
    from   OBSTESTPROD t2, deleted t1
    where  t2.NRTESTEPROD = t1.NRTESTEPROD

    return

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

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

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

    
    /*  Cannot delete parent "TIPOAJPAGAR" if children still exist in "AJCTPAGAR"  */
    if exists (select 1
               from   AJCTPAGAR t2, deleted t1
               where  t2.CDAJUCTPAG = t1.CDAJUCTPAG)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJCTPAGAR". Cannot delete parent "TIPOAJPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJPAGAR" if children still exist in "AJBOLPAG"  */
    if exists (select 1
               from   AJBOLPAG t2, deleted t1
               where  t2.CDAJUCTPAG = t1.CDAJUCTPAG)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJBOLPAG". Cannot delete parent "TIPOAJPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJPAGAR" if children still exist in "GERAL_PAG"  */
    if exists (select 1
               from   GERAL_PAG t2, deleted t1
               where  t2.CDAJMORAPAG = t1.CDAJUCTPAG)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERAL_PAG". Cannot delete parent "TIPOAJPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJPAGAR" if children still exist in "GERAL_PAG"  */
    if exists (select 1
               from   GERAL_PAG t2, deleted t1
               where  t2.CDAJMULTAPAG = t1.CDAJUCTPAG)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERAL_PAG". Cannot delete parent "TIPOAJPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJPAGAR" if children still exist in "GERAL_PAG"  */
    if exists (select 1
               from   GERAL_PAG t2, deleted t1
               where  t2.CDAJACREPAG = t1.CDAJUCTPAG)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERAL_PAG". Cannot delete parent "TIPOAJPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJPAGAR" if children still exist in "GERAL_PAG"  */
    if exists (select 1
               from   GERAL_PAG t2, deleted t1
               where  t2.CDAJDESCPAG = t1.CDAJUCTPAG)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERAL_PAG". Cannot delete parent "TIPOAJPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJPAGAR" if children still exist in "FORNECEDOR"  */
    if exists (select 1
               from   FORNECEDOR t2, deleted t1
               where  t2.CDAJUCTPAG = t1.CDAJUCTPAG)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORNECEDOR". Cannot delete parent "TIPOAJPAGAR".'
          goto error
       end
    
    /*  Delete all children in "RATTPAJP"  */
    delete RATTPAJP
    from   RATTPAJP t2, deleted t1
    where  t2.CDAJUCTPAG = t1.CDAJUCTPAG

    return

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

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

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

    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "AJCTRECEBER"  */
    if exists (select 1
               from   AJCTRECEBER t2, deleted t1
               where  t2.CDAJUCTREC = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJCTRECEBER". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "AJBOLREC"  */
    if exists (select 1
               from   AJBOLREC t2, deleted t1
               where  t2.CDAJUCTREC = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJBOLREC". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "GERAL_REC"  */
    if exists (select 1
               from   GERAL_REC t2, deleted t1
               where  t2.CDAJMULTAREC = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERAL_REC". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "GERAL_REC"  */
    if exists (select 1
               from   GERAL_REC t2, deleted t1
               where  t2.CDAJMORAREC = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERAL_REC". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "GERAL_REC"  */
    if exists (select 1
               from   GERAL_REC t2, deleted t1
               where  t2.CDAJACREREC = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERAL_REC". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "GERAL_REC"  */
    if exists (select 1
               from   GERAL_REC t2, deleted t1
               where  t2.CDAJDESCREC = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERAL_REC". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "IMPOSTO"  */
    if exists (select 1
               from   IMPOSTO t2, deleted t1
               where  t2.CDAJUCTREC = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSTO". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "AJDUPBOLET"  */
    if exists (select 1
               from   AJDUPBOLET t2, deleted t1
               where  t2.CDAJUCTREC = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AJDUPBOLET". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "CONVENIO"  */
    if exists (select 1
               from   CONVENIO t2, deleted t1
               where  t2.CDAJUCTRECDE = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONVENIO". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDAJUCTRECCR = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDAJCTRECCON = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "GERAL_REC"  */
    if exists (select 1
               from   GERAL_REC t2, deleted t1
               where  t2.CDAJDESCBDES = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERAL_REC". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "TIPORECE"  */
    if exists (select 1
               from   TIPORECE t2, deleted t1
               where  t2.CDAJTXADMREC = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPORECE". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "PARAMORCAEVE"  */
    if exists (select 1
               from   PARAMORCAEVE t2, deleted t1
               where  t2.CDAJCTRECSOB = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMORCAEVE". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOAJRECEBER" if children still exist in "PARAMORCAEVE"  */
    if exists (select 1
               from   PARAMORCAEVE t2, deleted t1
               where  t2.CDAJRECFECHA = t1.CDAJUCTREC)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMORCAEVE". Cannot delete parent "TIPOAJRECEBER".'
          goto error
       end
    
    /*  Delete all children in "RATTPAJR"  */
    delete RATTPAJR
    from   RATTPAJR t2, deleted t1
    where  t2.CDAJUCTREC = t1.CDAJUCTREC

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "TIPOCTPAGAR" if children still exist in "CONTAPAGAR"  */
    if exists (select 1
               from   CONTAPAGAR t2, deleted t1
               where  t2.CDTPCTPAGAR = t1.CDTPCTPAGAR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTAPAGAR". Cannot delete parent "TIPOCTPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTPAGAR" if children still exist in "PRODUTO"  */
    if exists (select 1
               from   PRODUTO t2, deleted t1
               where  t2.CDTPCTPAGAR = t1.CDTPCTPAGAR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTO". Cannot delete parent "TIPOCTPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTPAGAR" if children still exist in "TIPOOPERAC"  */
    if exists (select 1
               from   TIPOOPERAC t2, deleted t1
               where  t2.CDTPCTPAGAR = t1.CDTPCTPAGAR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOOPERAC". Cannot delete parent "TIPOCTPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTPAGAR" if children still exist in "FORNECEDOR"  */
    if exists (select 1
               from   FORNECEDOR t2, deleted t1
               where  t2.CDTPCTPAGAR = t1.CDTPCTPAGAR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORNECEDOR". Cannot delete parent "TIPOCTPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTPAGAR" if children still exist in "PARAMFISCAL"  */
    if exists (select 1
               from   PARAMFISCAL t2, deleted t1
               where  t2.CDTPCTPAGAR = t1.CDTPCTPAGAR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMFISCAL". Cannot delete parent "TIPOCTPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTPAGAR" if children still exist in "AUTOFORNE"  */
    if exists (select 1
               from   AUTOFORNE t2, deleted t1
               where  t2.CDTPCTPAGAR = t1.CDTPCTPAGAR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AUTOFORNE". Cannot delete parent "TIPOCTPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTPAGAR" if children still exist in "IMPOSTO"  */
    if exists (select 1
               from   IMPOSTO t2, deleted t1
               where  t2.CDTPCTPAGAR = t1.CDTPCTPAGAR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "IMPOSTO". Cannot delete parent "TIPOCTPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTPAGAR" if children still exist in "PAREXPEDICAO"  */
    if exists (select 1
               from   PAREXPEDICAO t2, deleted t1
               where  t2.CDTPCTPAGFRE = t1.CDTPCTPAGAR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PAREXPEDICAO". Cannot delete parent "TIPOCTPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTPAGAR" if children still exist in "PEDIDOEXOL"  */
    if exists (select 1
               from   PEDIDOEXOL t2, deleted t1
               where  t2.CDTPCTPAGAR = t1.CDTPCTPAGAR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDOEXOL". Cannot delete parent "TIPOCTPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTPAGAR" if children still exist in "COMPRA"  */
    if exists (select 1
               from   COMPRA t2, deleted t1
               where  t2.CDTPCTPAGAR = t1.CDTPCTPAGAR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPRA". Cannot delete parent "TIPOCTPAGAR".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTPAGAR" if children still exist in "ETLCONTAPAG"  */
    if exists (select 1
               from   ETLCONTAPAG t2, deleted t1
               where  t2.CDTPCTPAGAR = t1.CDTPCTPAGAR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCONTAPAG". Cannot delete parent "TIPOCTPAGAR".'
          goto error
       end
    
    /*  Delete all children in "RATTPCTP"  */
    delete RATTPCTP
    from   RATTPCTP t2, deleted t1
    where  t2.CDTPCTPAGAR = t1.CDTPCTPAGAR

    return

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

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

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

    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "CONTARECEBER"  */
    if exists (select 1
               from   CONTARECEBER t2, deleted t1
               where  t2.CDTPCTRECEB = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTARECEBER". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "PRODUTO"  */
    if exists (select 1
               from   PRODUTO t2, deleted t1
               where  t2.CDTPCTRECEB = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTO". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "TIPOOPERAC"  */
    if exists (select 1
               from   TIPOOPERAC t2, deleted t1
               where  t2.CDTPCTRECEB = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOOPERAC". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "CLIENTE"  */
    if exists (select 1
               from   CLIENTE t2, deleted t1
               where  t2.CDTPCTRECEB = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENTE". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDTCTRECMATR = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDTCTRECTRNF = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDTCTRECMDID = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDTCTRECTRNC = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDTCTRECMENS = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDTCTRECREMA = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDTCTRECCANC = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDTCTRECRENH = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "TIPOCTRECFIL"  */
    if exists (select 1
               from   TIPOCTRECFIL t2, deleted t1
               where  t2.CDTPCTRECEB = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOCTRECFIL". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDTCTRECTRTU = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.CDTCTRECCHA = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "TIPORECE"  */
    if exists (select 1
               from   TIPORECE t2, deleted t1
               where  t2.CDTPCTRECEB = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPORECE". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOCTRECEBER" if children still exist in "ETLCONTAREC"  */
    if exists (select 1
               from   ETLCONTAREC t2, deleted t1
               where  t2.CDTPCTRECEB = t1.CDTPCTRECEB)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ETLCONTAREC". Cannot delete parent "TIPOCTRECEBER".'
          goto error
       end
    
    /*  Delete all children in "RATTPCTR"  */
    delete RATTPCTR
    from   RATTPCTR t2, deleted t1
    where  t2.CDTPCTRECEB = t1.CDTPCTRECEB

    return

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

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

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

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

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "INCIDENCIA"  */
    if exists (select 1
               from   INCIDENCIA t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INCIDENCIA". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "CODFISCOPER"  */
    if exists (select 1
               from   CODFISCOPER t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODFISCOPER". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "CODSITUTRIB"  */
    if exists (select 1
               from   CODSITUTRIB t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODSITUTRIB". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "PARCMPSUP"  */
    if exists (select 1
               from   PARCMPSUP t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARCMPSUP". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "RELACTIPOPER"  */
    if exists (select 1
               from   RELACTIPOPER t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELACTIPOPER". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "REPLICDOC"  */
    if exists (select 1
               from   REPLICDOC t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REPLICDOC". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "ITREPLICDOC"  */
    if exists (select 1
               from   ITREPLICDOC t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITREPLICDOC". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "LANCTOESTOQ"  */
    if exists (select 1
               from   LANCTOESTOQ t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LANCTOESTOQ". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "ITLANCTOEST"  */
    if exists (select 1
               from   ITLANCTOEST t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITLANCTOEST". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "GERANFROMA"  */
    if exists (select 1
               from   GERANFROMA t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "GERANFROMA". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "PARAMINTEGRA"  */
    if exists (select 1
               from   PARAMINTEGRA t2, deleted t1
               where  t2.IDENTSAIOPDV = t1.IDENTRSAIDOP
                and   t2.CDTPOPERPDV = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMINTEGRA". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "TPDOCTRANSAP"  */
    if exists (select 1
               from   TPDOCTRANSAP t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TPDOCTRANSAP". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "CODTRIBSAP"  */
    if exists (select 1
               from   CODTRIBSAP t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CODTRIBSAP". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "PARAMACA"  */
    if exists (select 1
               from   PARAMACA t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMACA". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "CAIXA"  */
    if exists (select 1
               from   CAIXA t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CAIXA". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "PARAMINTEGRA"  */
    if exists (select 1
               from   PARAMINTEGRA t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMINTEGRA". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "PEDIDO"  */
    if exists (select 1
               from   PEDIDO t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDO". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "CTBIMPTPOPER"  */
    if exists (select 1
               from   CTBIMPTPOPER t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTBIMPTPOPER". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "CTBPRDTPOPER"  */
    if exists (select 1
               from   CTBPRDTPOPER t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CTBPRDTPOPER". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "VRPRODBCIMPO"  */
    if exists (select 1
               from   VRPRODBCIMPO t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VRPRODBCIMPO". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "PARAMPEDIDO"  */
    if exists (select 1
               from   PARAMPEDIDO t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPEDIDO". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "PARAVEND"  */
    if exists (select 1
               from   PARAVEND t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAVEND". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "COMPFATCGO"  */
    if exists (select 1
               from   COMPFATCGO t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPFATCGO". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "PARAMORCAEVE"  */
    if exists (select 1
               from   PARAMORCAEVE t2, deleted t1
               where  t2.IDESOPEROREV = t1.IDENTRSAIDOP
                and   t2.CDTPOPEROREV = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMORCAEVE". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "PARAMORCAEVE"  */
    if exists (select 1
               from   PARAMORCAEVE t2, deleted t1
               where  t2.IDESOPENTACE = t1.IDENTRSAIDOP
                and   t2.CDTPOPENTACE = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMORCAEVE". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "PARAMORCAEVE"  */
    if exists (select 1
               from   PARAMORCAEVE t2, deleted t1
               where  t2.IDESOPSAIACE = t1.IDENTRSAIDOP
                and   t2.CDTPOPSAIACE = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMORCAEVE". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "PARAMORCAEVE"  */
    if exists (select 1
               from   PARAMORCAEVE t2, deleted t1
               where  t2.IDESOPFINACE = t1.IDENTRSAIDOP
                and   t2.CDTPOPFINACE = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMORCAEVE". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "TIPOOPERACROP"  */
    if exists (select 1
               from   TIPOOPERACROP t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOOPERACROP". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "PARAMSERIE"  */
    if exists (select 1
               from   PARAMSERIE t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTIPOOPER = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMSERIE". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Cannot delete parent "TIPOOPERAC" if children still exist in "PARAVEND"  */
    if exists (select 1
               from   PARAVEND t2, deleted t1
               where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
                and   t2.CDTPOPERCNTREC = t1.CDTIPOOPER)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAVEND". Cannot delete parent "TIPOOPERAC".'
          goto error
       end
    
    /*  Delete all children in "EXPORTACAOTG"  */
    delete EXPORTACAOTG
    from   EXPORTACAOTG t2, deleted t1
    where  t2.IDENTRSAIDOP = t1.IDENTRSAIDOP
     and   t2.CDTIPOOPER = t1.CDTIPOOPER

    return

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

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

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

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

    return

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

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

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

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

    return

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

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

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

    
    /*  Delete all children in "ERROLANCCTBL"  */
    delete ERROLANCCTBL
    from   ERROLANCCTBL t2, deleted t1
    where  t2.CDEMPRLCTBCABE = t1.CDEMPRLCTBCABE
     and   t2.NRLANCCTBLCABE = t1.NRLANCCTBLCABE
    
    /*  Delete all children in "TRABLANCCTBL"  */
    delete TRABLANCCTBL
    from   TRABLANCCTBL t2, deleted t1
    where  t2.CDEMPRLCTBCABE = t1.CDEMPRLCTBCABE
     and   t2.NRLANCCTBLCABE = t1.NRLANCCTBLCABE

    return

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

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

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

    
    /*  Delete all children in "TRABLANCCUSTO"  */
    delete TRABLANCCUSTO
    from   TRABLANCCUSTO t2, deleted t1
    where  t2.CDEMPRLCTBCABE = t1.CDEMPRLCTBCABE
     and   t2.NRLANCCTBLCABE = t1.NRLANCCTBLCABE
     and   t2.NRSEQULCTBTEMP = t1.NRSEQULCTBTEMP

    return

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

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

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

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

    return

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

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

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

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

    return

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

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

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

    
    /*  Cannot delete parent "TURNO" if children still exist in "TRANSVEIC"  */
    if exists (select 1
               from   TRANSVEIC t2, deleted t1
               where  t2.NRTURNO = t1.NRTURNO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TRANSVEIC". Cannot delete parent "TURNO".'
          goto error
       end
    
    /*  Cannot delete parent "TURNO" if children still exist in "TURMAPAD"  */
    if exists (select 1
               from   TURMAPAD t2, deleted t1
               where  t2.NRTURNO = t1.NRTURNO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TURMAPAD". Cannot delete parent "TURNO".'
          goto error
       end
    
    /*  Cannot delete parent "TURNO" if children still exist in "TURMAPERIODO"  */
    if exists (select 1
               from   TURMAPERIODO t2, deleted t1
               where  t2.NRTURNO = t1.NRTURNO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TURMAPERIODO". Cannot delete parent "TURNO".'
          goto error
       end
    
    /*  Cannot delete parent "TURNO" if children still exist in "PROSPECT"  */
    if exists (select 1
               from   PROSPECT t2, deleted t1
               where  t2.NRTURNO = t1.NRTURNO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROSPECT". Cannot delete parent "TURNO".'
          goto error
       end
    
    /*  Cannot delete parent "TURNO" if children still exist in "RESTITREFTUR"  */
    if exists (select 1
               from   RESTITREFTUR t2, deleted t1
               where  t2.NRTURNO = t1.NRTURNO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESTITREFTUR". Cannot delete parent "TURNO".'
          goto error
       end
    
    /*  Cannot delete parent "TURNO" if children still exist in "AULATURMA"  */
    if exists (select 1
               from   AULATURMA t2, deleted t1
               where  t2.NRTURNO = t1.NRTURNO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "AULATURMA". Cannot delete parent "TURNO".'
          goto error
       end
    
    /*  Cannot delete parent "TURNO" if children still exist in "CLIENTE"  */
    if exists (select 1
               from   CLIENTE t2, deleted t1
               where  t2.NRTURNO = t1.NRTURNO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENTE". Cannot delete parent "TURNO".'
          goto error
       end
    
    /*  Cannot delete parent "TURNO" if children still exist in "ITREGRACONS"  */
    if exists (select 1
               from   ITREGRACONS t2, deleted t1
               where  t2.NRTURNO = t1.NRTURNO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITREGRACONS". Cannot delete parent "TURNO".'
          goto error
       end
    
    /*  Cannot delete parent "TURNO" if children still exist in "TURNOFILI"  */
    if exists (select 1
               from   TURNOFILI t2, deleted t1
               where  t2.NRTURNO = t1.NRTURNO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TURNOFILI". Cannot delete parent "TURNO".'
          goto error
       end
    
    /*  Cannot delete parent "TURNO" if children still exist in "MAPABASEV"  */
    if exists (select 1
               from   MAPABASEV t2, deleted t1
               where  t2.NRTURNO = t1.NRTURNO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MAPABASEV". Cannot delete parent "TURNO".'
          goto error
       end
    
    /*  Cannot delete parent "TURNO" if children still exist in "ITMAOOBRAORCA"  */
    if exists (select 1
               from   ITMAOOBRAORCA t2, deleted t1
               where  t2.NRTURNO = t1.NRTURNO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITMAOOBRAORCA". Cannot delete parent "TURNO".'
          goto error
       end
    
    /*  Cannot delete parent "TURNO" if children still exist in "TIPOSERVTURNO"  */
    if exists (select 1
               from   TIPOSERVTURNO t2, deleted t1
               where  t2.NRTURNO = t1.NRTURNO)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TIPOSERVTURNO". Cannot delete parent "TURNO".'
          goto error
       end
    
    /*  Delete all children in "TURNOPRODUTO"  */
    delete TURNOPRODUTO
    from   TURNOPRODUTO t2, deleted t1
    where  t2.NRTURNO = t1.NRTURNO

    return

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

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

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

    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "PRODUTO"  */
    if exists (select 1
               from   PRODUTO t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODUTO". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "CONVUNID"  */
    if exists (select 1
               from   CONVUNID t2, deleted t1
               where  t2.SGUNIDPARA = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONVUNID". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "CONVPRODUTO"  */
    if exists (select 1
               from   CONVPRODUTO t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONVPRODUTO". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "PRATO"  */
    if exists (select 1
               from   PRATO t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRATO". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "RECEFILI"  */
    if exists (select 1
               from   RECEFILI t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEFILI". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "RECESERV"  */
    if exists (select 1
               from   RECESERV t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECESERV". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "NUTRIENTE"  */
    if exists (select 1
               from   NUTRIENTE t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NUTRIENTE". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "ITATENREAL"  */
    if exists (select 1
               from   ITATENREAL t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITATENREAL". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "PRODFORN"  */
    if exists (select 1
               from   PRODFORN t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRODFORN". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "PREVCOMPRA"  */
    if exists (select 1
               from   PREVCOMPRA t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PREVCOMPRA". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "RECEPADR"  */
    if exists (select 1
               from   RECEPADR t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEPADR". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "FORNECEDOR"  */
    if exists (select 1
               from   FORNECEDOR t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FORNECEDOR". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "RECEITA2"  */
    if exists (select 1
               from   RECEITA2 t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RECEITA2". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "INSPECAO"  */
    if exists (select 1
               from   INSPECAO t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INSPECAO". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "PRINATIVMEDI"  */
    if exists (select 1
               from   PRINATIVMEDI t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PRINATIVMEDI". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "TPCONTROLE"  */
    if exists (select 1
               from   TPCONTROLE t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "TPCONTROLE". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "COMPPROD"  */
    if exists (select 1
               from   COMPPROD t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "COMPPROD". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "RESPOSINSPEC"  */
    if exists (select 1
               from   RESPOSINSPEC t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RESPOSINSPEC". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "ITEMINSAUDIT"  */
    if exists (select 1
               from   ITEMINSAUDIT t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMINSAUDIT". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "FICHAFPR"  */
    if exists (select 1
               from   FICHAFPR t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "FICHAFPR". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "ITFICHAFPR"  */
    if exists (select 1
               from   ITFICHAFPR t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITFICHAFPR". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "PERDAOP"  */
    if exists (select 1
               from   PERDAOP t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PERDAOP". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "ITEMORCAPROD"  */
    if exists (select 1
               from   ITEMORCAPROD t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ITEMORCAPROD". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "METAVENDA"  */
    if exists (select 1
               from   METAVENDA t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "METAVENDA". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "EQUIPAPROD"  */
    if exists (select 1
               from   EQUIPAPROD t2, deleted t1
               where  t2.SGUNIDCONSEP = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "EQUIPAPROD". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "PARAMETRO"  */
    if exists (select 1
               from   PARAMETRO t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMETRO". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "BEM"  */
    if exists (select 1
               from   BEM t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "BEM". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "UNIDMEDIFILI"  */
    if exists (select 1
               from   UNIDMEDIFILI t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "UNIDMEDIFILI". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Cannot delete parent "UNIDMEDIDA" if children still exist in "INDICADOR"  */
    if exists (select 1
               from   INDICADOR t2, deleted t1
               where  t2.SGUNIDADE = t1.SGUNIDADE)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "INDICADOR". Cannot delete parent "UNIDMEDIDA".'
          goto error
       end
    
    /*  Delete all children in "CONVUNID"  */
    delete CONVUNID
    from   CONVUNID t2, deleted t1
    where  t2.SGUNIDDE = t1.SGUNIDADE

    return

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

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

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

    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "CLIENTE"  */
    if exists (select 1
               from   CLIENTE t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CLIENTE". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "CONTATO"  */
    if exists (select 1
               from   CONTATO t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONTATO". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "VENDA"  */
    if exists (select 1
               from   VENDA t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDA". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "PARAMPEDIDO"  */
    if exists (select 1
               from   PARAMPEDIDO t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PARAMPEDIDO". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "ACAO"  */
    if exists (select 1
               from   ACAO t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ACAO". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "VENDITEMPED"  */
    if exists (select 1
               from   VENDITEMPED t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDITEMPED". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "PROPOSTA"  */
    if exists (select 1
               from   PROPOSTA t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PROPOSTA". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "NOTAFISCAL"  */
    if exists (select 1
               from   NOTAFISCAL t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "NOTAFISCAL". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "PEDIDO"  */
    if exists (select 1
               from   PEDIDO t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "PEDIDO". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "MENSVEND"  */
    if exists (select 1
               from   MENSVEND t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MENSVEND". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "HISITEMPED"  */
    if exists (select 1
               from   HISITEMPED t2, deleted t1
               where  t2.CDVENDORIG = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HISITEMPED". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "METAVENDEDOR"  */
    if exists (select 1
               from   METAVENDEDOR t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "METAVENDEDOR". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "HISITEMPED"  */
    if exists (select 1
               from   HISITEMPED t2, deleted t1
               where  t2.CDVENDATUAL = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "HISITEMPED". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "REGIAOPEDVEN"  */
    if exists (select 1
               from   REGIAOPEDVEN t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "REGIAOPEDVEN". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "MESAVENDE"  */
    if exists (select 1
               from   MESAVENDE t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "MESAVENDE". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "VENDAREST"  */
    if exists (select 1
               from   VENDAREST t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "VENDAREST". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "LOJA"  */
    if exists (select 1
               from   LOJA t2, deleted t1
               where  t2.CDVENDPADRAO = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOJA". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "CONSUMIDOR"  */
    if exists (select 1
               from   CONSUMIDOR t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "CONSUMIDOR". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Cannot delete parent "VENDEDOR" if children still exist in "ORCAEVENTO"  */
    if exists (select 1
               from   ORCAEVENTO t2, deleted t1
               where  t2.CDVENDEDOR = t1.CDVENDEDOR)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "ORCAEVENTO". Cannot delete parent "VENDEDOR".'
          goto error
       end
    
    /*  Delete all children in "ENDEVENDE"  */
    delete ENDEVENDE
    from   ENDEVENDE t2, deleted t1
    where  t2.CDVENDEDOR = t1.CDVENDEDOR

    return

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

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

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

    
    /*  Cannot delete parent "VERSGSIS" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDGRUPSIST = t1.CDGRUPSIST
                and   t2.CDVERSLIBE = t1.CDVERSGSIS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "VERSGSIS".'
          goto error
       end
    
    /*  Cannot delete parent "VERSGSIS" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDGRUPSIST = t1.CDGRUPSIST
                and   t2.CDVERSPLAN = t1.CDVERSGSIS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "VERSGSIS".'
          goto error
       end
    
    /*  Cannot delete parent "VERSGSIS" if children still exist in "SOLISERV"  */
    if exists (select 1
               from   SOLISERV t2, deleted t1
               where  t2.CDGRUPSIST = t1.CDGRUPSIST
                and   t2.CDVERSORIG = t1.CDVERSGSIS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "SOLISERV". Cannot delete parent "VERSGSIS".'
          goto error
       end
    
    /*  Cannot delete parent "VERSGSIS" if children still exist in "LOGDATACOMP"  */
    if exists (select 1
               from   LOGDATACOMP t2, deleted t1
               where  t2.CDGRUPSIST = t1.CDGRUPSIST
                and   t2.CDVERSGSIS = t1.CDVERSGSIS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "LOGDATACOMP". Cannot delete parent "VERSGSIS".'
          goto error
       end
    
    /*  Cannot delete parent "VERSGSIS" if children still exist in "RELEASEVERS"  */
    if exists (select 1
               from   RELEASEVERS t2, deleted t1
               where  t2.CDGRUPSIST = t1.CDGRUPSIST
                and   t2.CDVERSGSIS = t1.CDVERSGSIS)
       begin
          select @errno  = 30006,
                 @errmsg = 'Children still exist in "RELEASEVERS". Cannot delete parent "VERSGSIS".'
          goto error
       end
    
    /*  Delete all children in "SISTLIBEVERS"  */
    delete SISTLIBEVERS
    from   SISTLIBEVERS t2, deleted t1
    where  t2.CDGRUPSIST = t1.CDGRUPSIST
     and   t2.CDVERSGSIS = t1.CDVERSGSIS

    return

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

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

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

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

    return

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

