Assign values from a select to a variable

Asked

Viewed 38 times

0

Well I am mounting a procedure in Mysql, and inside it I have the following code:

  DECLARE valorConta DECIMAL DEFAULT 0;
  DECLARE valorPGConta DECIMAL DEFAULT 0;

  SELECT Valor INTO valorConta
  FROM Contas
  WHERE Cod = CodConta;

  IF valorPGConta >= valorConta THEN
    
  ELSE
    
  END IF;

Well I declared 2 variables being them valorConta and valorPGConta. On the table Contas I have both fields Valor and ValorPG.

I need to know how to assign the values in the 2 variables and then do the verification in the IF.

1 answer

0

First you have to declare the Codconta, then do two "SELECT INTO":

--RECEBE CodConta para realizar o filtro
DECLARE CodConta DECIMAL DEFAULT 0;

--VALOR CONTA
SELECT Valor 
INTO valorConta
FROM Contas
WHERE Cod = CodConta;

--VALOR PG CONTA
SELECT valorPGConta 
INTO valorPGConta
FROM Contas
WHERE Cod = CodConta;

The IF is correct, then in case just execute the desired commands inside it.

  • I ended up finding a way to do it using just one select. Thus: SELECT Valor, ValorPG INTO valorConta, valorPGConta

  • You can post your solution to your question.

Browser other questions tagged

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