Move SQL data from one table to another

后端 未结 13 2857
梦如初夏
梦如初夏 2020-11-29 20:34

I was wondering if it is possible to move all rows of data from one table to another, that match a certain query?

For example, I need to move all table rows from Tab

13条回答
  •  失恋的感觉
    2020-11-29 21:25

    Use this single sql statement which is safe no need of commit/rollback with multiple statements.

    INSERT Table2 (
          username,password
    ) SELECT username,password
          FROM    (
               DELETE Table1
               OUTPUT
                       DELETED.username,
                       DELETED.password
               WHERE username = 'X' and password = 'X'
          ) AS RowsToMove ;
    

    Works on SQL server make appropriate changes for MySql

提交回复
热议问题