Update query using Subquery in Sql Server

后端 未结 5 1355
走了就别回头了
走了就别回头了 2020-12-02 07:14

I have a simple table Structure like this:

Table tempData

╔══════════╦═══════╗
║   NAME   ║ MARKS ║
╠══════════╬═══         


        
5条回答
  •  猫巷女王i
    2020-12-02 07:51

    you can join both tables even on UPDATE statements,

    UPDATE  a
    SET     a.marks = b.marks
    FROM    tempDataView a
            INNER JOIN tempData b
                ON a.Name = b.Name
    
    • SQLFiddle Demo

    for faster performance, define an INDEX on column marks on both tables.

    using SUBQUERY

    UPDATE  tempDataView 
    SET     marks = 
            (
              SELECT marks 
              FROM tempData b 
              WHERE tempDataView.Name = b.Name
            )
    
    • SQLFiddle Demo

提交回复
热议问题