SQL UPDATE order of evaluation

前端 未结 3 1197
青春惊慌失措
青春惊慌失措 2020-11-29 07:48

What is the order of evaluation in the following query:

UPDATE tbl SET q = q + 1, p = q;

That is, will \"tbl\".\"p\" be set to

3条回答
  •  天涯浪人
    2020-11-29 08:27

    The UPDATE does not see the results of its work.

    p will be set to q as of before update.

    The following code will just swap the columns:

    DECLARE @test TABLE (p INT, q INT)
    
    INSERT
    INTO    @test
    VALUES  (2, 3)
    
    SELECT  *
    FROM    @test
    
    p    q
    ---  ---
      2    3
    
    UPDATE  @test
    SET     p = q,
            q = p
    
    SELECT  *
    FROM    @test
    
    p    q
    ---  ---
      3    2
    

提交回复
热议问题