pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Hélder M. Vieira <hmv@mail.telepac.pt>
To: pgsql-sql@postgresql.org
Subject: Re: Problemas com Procedure no PostgreSQL
Date: Fri, 17 Apr 2009 14:53:34 +0100
Message-ID: <9E98F3198C86482FA265D865E8CBC83B@hmv02> (raw)
References: <49E86A4A.4060507@o2tecnologia.com.br>

Boa tarde.

> acusado o seguinte erro "sintax error at or near "SELECT" at character 
> 487", e infelizmente não conseguimos achar o que esta errado, dai peço 



Refiz todo o texto, p.f. experimente e diga se resultou.
Algumas das alterações que fiz são meramente estilísticas, outras têm a ver com questões gramaticais. As mais significativas são:
1. Transcrição para minúsculas. Do ponto de vista dos comandos plpgsql julgo que é indiferente, mas o uso de maiúsculas para nomear objectos no Postgres não é vantajoso
2. Declaração de parâmetros. Os alias já não são necessários, os parâmetros podem ser utilizados por nome em vez de $1, $2, etc...
2. Correcção das estruturas if .. then .. end if 
3. Reformulação dos ciclos for .. in .. loop .. end loop, que passam a usar uma variável de tipo registo ( declarada como 'rdat record' )
4. Supressão de alias nos select e update, uma vez que não há join envolvidos 

Cumprimentos,

Hélder M. Vieira
create or replace function trava_inadimplentes ( pdias_vencido integer, pund_local varchar(3), pcnpj varchar(18) ) returns boolean as
   $$
   declare
      spermite_bloqueio varchar(1);
      inro_registros integer;
      --
      rdat record;
   begin
      if ( pcnpj = '' ) then
   
         for rdat in
            select tipo_doc as sfat_documento , 
                   fil_orig as sfat_emissora  , 
                   fatura   as ifat_numero    , 
                   ano      as sfat_ano       , 
                   parcela  as ifat_parcela   , 
                   cgc      as scod_cliente
            from ctas_receber
            where ( status not in ( 'ca', 'co' ) ) and ( dt_pagto is null )
              and ( ( current_timestamp - dt_vencto ) >= pdias_vencido )
            order by dt_vencto, tipo_doc, fil_orig, fatura, ano
         loop
            select permite_protesto
            from clientes
            where cgc = rdat.scod_cliente
            into spermite_bloqueio;
            if (spermite_bloqueio = 't') then
               --
               update clientes
               set inadimplente = 't'                   , 
                   operador     = 'bloqueio automatico' , 
                   dt_alteracao = current_timestamp
               where cgc = rdat.scod_cliente;
               --
               insert into hist_cobranca
               values ( rdat.sfat_documento, rdat.sfat_emissora, rdat.ifat_numero, rdat.sfat_ano, rdat.ifat_parcela, current_timestamp, pund_local, 'bloqueio automatico de inadimplente', 'bloqueio automatico', current_timestamp );
            end if;
         end loop;
      
      else
   
         for rdat in
            select tipo_doc as sfat_documento , 
                   fil_orig as sfat_emissora  , 
                   fatura   as ifat_numero    , 
                   ano      as sfat_ano       , 
                   parcela  as ifat_parcela   , 
                   cgc      as scod_cliente
            from ctas_receber
            where ( status not in ( 'ca', 'co' )) and ( dt_pagto is null )
              and ( ( current_timestamp - cob.dt_vencto ) >= pdias_vencido )
              and ( cgc = pcnpj )
            order by dt_vencto, tipo_doc, fil_orig, fatura, ano
         loop
         
            select permite_protesto
            from clientes
            where cgc = scod_cliente
            into spermite_bloqueio;

            if ( spermite_bloqueio = 't' ) then
               --
               update clientes
               set inadimplente = 't'                   ,
                   operador     = 'bloqueio automatico' , 
                   dt_alteracao = current_timestamp
               where cgc = scod_cliente;
               --
               select count( fat_numero )
               from hist_cobranca 
               where ( fat_documento  = rdat.sfat_documento ) 
                 and ( fat_emissora   = rdat.sfat_emissora ) 
                 and ( fat_numero     = rdat.ifat_numero )
                 and ( fat_ano        = rdta.sfat_ano ) 
                 and ( fat_parcela    = ifat_parcela ) 
                 and ( dta_lancamento = current_timestamp )
               into inro_registros;
               --
               if ( inro_registros = 0 ) then
                  insert into hist_cobranca
                  values ( rdat.sfat_documento, rdat.sfat_emissora, rdat.ifat_numero, rdat.sfat_ano, rdat.ifat_parcela, current_timestamp, pund_local, 'bloqueio automatico de inadimplente', 'bloqueio automatico', current_timestamp );
               end if;
               --
            end if;
            
         end loop;
      
      end if;
      return true;
   end;
   $$
   language 'plpgsql';

view thread (5+ messages)  latest in thread

Message-ID: <9E98F3198C86482FA265D865E8CBC83B@hmv02>
Permalink:  ../9E98F3198C86482FA265D865E8CBC83B@hmv02/
Also on:    postgresql.org/message-id/9E98F3198C86482FA265D865E8CBC83B@hmv02

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: hmv@mail.telepac.pt
  Subject: Re: Problemas com Procedure no PostgreSQL
  In-Reply-To: <9E98F3198C86482FA265D865E8CBC83B@hmv02>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox