Problem Nextval Postgres Delphi

3

I'm doing an application, in which I need to increment a sequence and check if that value has already been used in another table, because if it has already been I should ignore it and generate a new value for sequence until you find a unique value.

This is my SQL:

select nextval('controlesequencia')as proximo,  -1 as soma
union
select currval('controlesequencia') as proximo, sum(quantidade) as soma from (
select count(*) as quantidade from manobrista where 
CAST(('0' || COALESCE(segundo_codigo,'0')) AS INTEGER) = currval('controlesequencia')
union
select count(*) as quantidade from lancamento where 
CAST(('0' || COALESCE(num_registro,'0')) AS INTEGER) = currval('controlesequencia') and data_saida = '') as mensalista 

When I run Delphi, the value returned in nextval is a strange character map.

DM.ProximoControle.FieldByName(Proximo).AsInteger;

How do I get the value correctly?

    
asked by anonymous 04.03.2015 / 19:33

1 answer

1
DM.GetProximoControle(); 
While DM.ProximoControle.eof do
 begin
   if(DM.ProximoControle.FieldByName(SOMA).AsInteger    =0) then begin  unico := DM.ProximoControle.FieldByName(PROXIMO).AsInteger; break;  
 end; 
DM.ProximoControle.next;

This routine is being executed in a wrong way, because even without testing and possible to realize, that the question in while is wrong because even when we have only one record and want to iterate it we must put not eof so that the block is run at least once.

So the result of this routine is unexpected.

    
20.05.2015 / 04:07