Why do update with join does not obey the WHERE conditional

Asked

Viewed 37 times

2

I will replace the values of a column in table A by values in table B, being conditioned by a column in table C.

When doing UPDATE, the condition is not being respected and all values are being changed.

UPDATE
    tabela_precos_produtos
SET
    valor_canal = pg.jan_valor, 
    valor_partida = pg.dez_valor
FROM
    precos_geral pg
JOIN 
    tabela_precos tp ON cod_tabela = cod_tabela
WHERE
    tp.uf = 'SP'

How should UPDATE be done following the mentioned case? The above query does not bring the expected result.

1 answer

2


I think you forgot to put in Join the table you want to change, try:

UPDATE tpp
SET valor_canal = pg.jan_valor, 
    valor_partida = pg.dez_valor
FROM precos_geral pg
INNER JOIN tabela_precos tp ON tp.cod_tabela = pg.cod_tabela
INNER JOIN tabela_precos_produtos tpp ON tpp.<campo1> = tp.<campo2>
WHERE
    tp.uf = 'SP'

I don’t know if this is the right Join (tpp with tp), but I think you can get an idea.

Browser other questions tagged

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