Problema Nextval Postgres Delphi

Question:

I'm making 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, I should ignore it and generate a new value for the sequence until I find a single 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 running in Delphi the value returned in nextval is a strange character from the character map.

DM.ProximoControle.FieldByName(Proximo).AsInteger;

How do I get the value correctly?

Answer:

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 wrongly, because even without testing it is possible to notice that the question in while is wrong because even when we have only one record and we want to iterate it, we must put not eof so that the block runs at least one turn.

So the result of this routine is unexpected.

Scroll to Top