Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

636
Visualizações
How to use variables in PostgreSQL transaction

How in Postgresql inside a transaction to get values ​​into a variable, and if SELECT did not return anything, throw an error, and if SELECT returned data, then use them in the transaction?

Like this:

BEGIN;

@activeRounds = SELECT * FROM "rounds" WHERE status = 'active';

if(!@activeRounds) {
  RAISE ERROR "Has no Active rounds"
};

INSERT INTO "bet"
  (user_id, round_id)
VALUES
  (100, @activeRound[0].id)


COMMIT;

how to do something similar in one request within a transaction?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can adapt following code to your table structure:

begin;
do
$$
declare
 v int;
begin
 select c1 into v from t1 where  k1=1;
 if not found 
 then
  raise exception 'no row found';
 else
  insert into t2(c2) values(v);
 end if;
end;
$$;
commit;

Note the difference bewteen begin; to start a transaction and begin to start a pl/pgSQL block.

over 4 years ago · Santiago Trujillo Relatório

0

Write a DO statement in PL/pgSQL.

Like every SQL statement, DO runs in a single transaction. PL/pgSQL is a procedural language that has variables and conditional processing.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda