Nextval Postgres Delphi problem

Asked

Viewed 464 times

3

I’m making an application, in which I need to increment a quence and check if that value has already been used in another table, because if it has already been I must ignore and generate a new value for the quence 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 running in Delphi the value returned in nextval is a strange character of the character map.

DM.ProximoControle.FieldByName(Proximo).AsInteger;

How do I get the value correctly?

  • You said when you run in Delphi it returns a strange character, what is it? If you try directly in the database sql works normally?

  • 1

    Could you show a little more code? Where did you see the strange character? It was in debug?

  • 2

    Directly in the database sql works normally without problem.

  • Delphi cannot convert the returned value to integer, when I switch to another return, for example, string the return is not an integer is a character.

  • //Procedure that opens sql, and executes query just opens and closes sql DM.Getproximocontrol() 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; end;

  • You tried to cast for bigint ? type: select cast(nextval('controlesequencia') as bigint) as next, -1 as soma ...

  • How did this code VC post is working if your while question is eof when it should be not eof ? @Priscila Almeida

Show 2 more comments

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 the wrong way, because even without doing tests and possible to realize, that the question in while this 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 once.

So the result of this routine is unexpected.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.