Move SQL data from one table to another

后端 未结 13 2865
梦如初夏
梦如初夏 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:15

    Yes it is. First INSERT + SELECT and then DELETE orginals.

    INSERT INTO Table2 (UserName,Password)
    SELECT UserName,Password FROM Table1 WHERE UserName='X' AND Password='X'
    

    then delete orginals

    DELETE FROM Table1 WHERE UserName='X' AND Password='X'
    

    you may want to preserve UserID or someother primary key, then you can use IDENTITY INSERT to preserve the key.

    see more on SET IDENTITY_INSERT on MSDN

提交回复
热议问题