Search code examples
sqlsql-serverazure-sql-database

Sql update column wont regonize diffeent table


The code looks like this but it shows me multipart identifier loty and users could not be found

UPDATE zabukowane
SET cenabiletu = zabukowane.cenabagazu + loty.cena
WHERE zabukowane.idlotu = loty.idlotu AND zabukowane.userid = users.userid
FROM zabukowane JOIN loty, users;

I want to set column(cenabiletu) in zabukowane to take from table called loty column cena where usersid in users is equal to userid in zabukowane and idlotu in loty is equal to idlotu in zabukowane and adding to cenabagazu and summing that up in cenabiletu column so it may look like this cenabiletu.zabukowane=cena.loty+cenabagazu.zabukowane


Solution

  • Seems that you are missing a few points. Try it in this way:

    UPDATE zabukowane
    SET cenabiletu = zabukowane.cenabagazu + loty.cena
    FROM zabukowane 
    INNER JOIN loty on  zabukowane.idlotu = loty.idlotu
    Jo
    INNER JOIN users on zabukowane.userid = users.userid
    

    This post question has a great explanation: How do I UPDATE from a SELECT in SQL Server?.