Update tables with the same data in row

1

When I add a A category, and add a X product with the A category (categories table), after that it will edit the category name A to category B , the category of product X (table products) remains with the category A .

My update query is

UPDATE Categorias set Categoria = @Categoria WHERE IDCatg = @IDCatg

Basically, when the A category was edited (categories table), check if there is any product with the category A (table products), and in addition to updating to A category for B , edit the product category X to B . If the category is not in any product, edit normally, for that purpose the query described above.

    
asked by anonymous 09.06.2015 / 19:13

1 answer

1

There are two major problems with its structure because it does not save IDCatg in the Produtos table.

  • When your Category changes in the description it will become outdated in the Produtos table. (Your current situation)
  • When you update the category description in the Produtos table, you do not know if that description belongs to the IDCatg you want to change. Then you may happen to update the different IDCatg Category.
  • -

    If you want to continue anyway:

    UPDATE Produtos, Categorias
    SET Produtos.Categoria = @Categoria,
        Categorias.Categoria = @Categoria
    WHERE Produtos.Categoria = Produtos.Categoria
      AND Categorias.IDCatg = @IDCatg
    
        
    09.06.2015 / 19:37