How can I do an UPDATE statement with JOIN in SQL Server?

前端 未结 16 1265
名媛妹妹
名媛妹妹 2020-11-21 11:51

I need to update this table in SQL Server with data from its \'parent\' table, see below:

Table: sale

id (int)
udid         


        
相关标签:
16条回答
  • 2020-11-21 12:16

    PostgreSQL:

    CREATE TABLE ud (id integer, assid integer);
    CREATE TABLE sales (id integer, udid integer, assid integer);
    
    UPDATE ud
    SET assid = sales.assid
    FROM sales
    WHERE sales.id = ud.id;
    
    0 讨论(0)
  • 2020-11-21 12:17

    And in MS ACCESS:

    UPDATE ud 
    INNER JOIN sale ON ud.id = sale.udid
    SET ud.assid = sale.assid;
    
    0 讨论(0)
  • 2020-11-21 12:20

    The following statement with FROM keyword is used to update multiple rows with a join

    UPDATE users 
    set users.DivisionId=divisions.DivisionId
    from divisions join users on divisions.Name=users.Division
    
    0 讨论(0)
  • 2020-11-21 12:21

    I was thinking the SQL-Server one in the top post would work for Sybase since they are both T-SQL but unfortunately not.

    For Sybase I found the update needs to be on the table itself not the alias:

    update ud
    set u.assid = s.assid
    from ud u
        inner join sale s on
            u.id = s.udid
    
    0 讨论(0)
提交回复
热议问题