UPDATE WITH CASE DOESN’T WORK

Asked

Viewed 33 times

0

I am working with a bunch of data (Postgres) on vestibular and I was looking to do an update joining two tables and using UPDATE CASE WHEN THEN. I was relating the two tables through their ids and used a field of each to create the table update condition with new values. NOTE: These values that would be added do not exist in the tables and I didn’t want to waste time in doing several separate updates or separate Inserts.

Follows the code:

-- UPDATE PARA ADIÇÃO DO ID SISTEMA VAGA FINAL PARA CANDIDATOS INDEFERIDOS NO TESTE DE APTIDÃO E PCD (MUSICA)
UPDATE sigconcursos.inscricao AS insc 
SET id_vaga_final = id_vaga_segunda_opcao,
id_vaga_sistema_vagas_final CASE
WHEN id_vaga_segunda_opcao = 262 THEN id_vaga_sistema_vagas_final = 812 -- ID VAGA SEGUNDA OPÇÃO DETERMINA QUAL VAGA SEGUIR PCD
WHEN id_vaga_segunda_opcao = 270 THEN id_vaga_sistema_vagas_final = 835
WHEN id_vaga_segunda_opcao = 286 THEN id_vaga_sistema_vagas_final = 881
FROM sigconcursos.analise_inscricao AS aninsc
WHERE insc.id_inscricao = aninsc.id_inscricao AND insc.id_vaga = 284
AND insc.id_vaga_sistema_vagas = 875 AND aninsc.id_status = 3 AND insc.id_concurso = 16

The Error:

O campo id_vaga_sistema_vagas_final é um inteiro e a operação retorna um booleano.

1 answer

1


You are not using the CASE in a syntactically correct way. Note that in the case of (id_vaga_segunda_opcao = 262) you are returning the result of the expression id_vaga_sistema_vagas_final = 812, which will be true or false, and not attributing 812 à id_vaga_sistema_vagas_final.
The correct syntax is:

CASE expression
    WHEN value THEN result
    [WHEN ...]
    [ELSE result]
END

That way your command should be:

UPDATE sigconcursos.inscricao AS insc 
SET id_vaga_final = id_vaga_segunda_opcao,
        id_vaga_sistema_vagas_final =   
            CASE    WHEN id_vaga_segunda_opcao = 262 THEN 812 -- ID VAGA SEGUNDA OPÇÃO DETERMINA QUAL VAGA SEGUIR PCD
                    WHEN id_vaga_segunda_opcao = 270 THEN 835
                    WHEN id_vaga_segunda_opcao = 286 THEN 881
                    ELSE 0
            END
FROM sigconcursos.analise_inscricao AS aninsc
WHERE insc.id_inscricao = aninsc.id_inscricao
  AND insc.id_vaga = 284
  AND insc.id_vaga_sistema_vagas = 875
  AND aninsc.id_status = 3
  AND insc.id_concurso = 16
  • Our bro, really I was doing very badly kkkk. Really why was always returning compatibility error. As I was working with several tasks, I just missed this detail. Thanks for the tip :)

Browser other questions tagged

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