Move row from one table to another?

后端 未结 5 1251
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 06:25

I have two tables with the same column definitions. I need to move (not copy) a row from one table to another. Before I go off and use INSERT INTO/DELETE (in a transaction

5条回答
  •  佛祖请我去吃肉
    2020-12-15 07:26

    INSERT dbo.newtable(
          name,
          department,
          Salary
    ) SELECT 
                name,
                FirstName,
                Lastname
          FROM    (
               DELETE dbo.oldtable
               OUTPUT
                       DELETED.name,
                       DELETED.department,
                       DELETED.Salary
               WHERE ID  IN ( 1001, 1003, 1005 )
          ) AS RowsToMove  
    
    SELECT * FROM dbo.newtable
    SELECT * FROM dbo.oldtable
    

提交回复
热议问题