Update statement to update multiple rows

后端 未结 4 1444
盖世英雄少女心
盖世英雄少女心 2021-01-30 13:38

I have a question regarding the following syntax. Is there a cleaner way to roll this up into one statement rather than two. I\'ve tried several iterations but this seems to b

4条回答
  •  情书的邮戳
    2021-01-30 14:12

    Try this one, this will combine multiple selects and returns them as if they come from the DB:

    UPDATE e
    SET hire_date = t.hire_date
    FROM dbo.employee e
    JOIN (
        SELECT emp_id = 'PMA42628M', hire_date = '1979-03-15'
        UNION ALL
        SELECT emp_id = 'PSA89086M', hire_date = '1988-12-22'
    ) t ON t.emp_id = e.emp_id
    

    If you are using SQL Server 2008 or later version, you could also use a different syntax for the derived table:

    UPDATE e
    SET hire_date = t.hire_date
    FROM dbo.employee e
    JOIN (
        VALUES
            ('PMA42628M', '1979-03-15'),
            ('PSA89086M', '1988-12-22')
    ) t (emp_id, hire_date) ON t.emp_id = e.emp_id
    

提交回复
热议问题