Why to perform UPDATE with JOIN does not conform to conditional WHERE

2

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

When you perform UPDATE, the conditional 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 the UPDATE be done obeying the mentioned case? The above query does not bring the expected result.

    
asked by anonymous 28.01.2016 / 17:30

1 answer

2

I think you forgot to 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 do not know if the correct join is (tpp with tp), but I think you can get an idea.

    
28.01.2016 / 17:43