Move SQL data from one table to another

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

    0 讨论(0)
  • 2020-11-29 21:31

    It will create a table and copy all the data from old table to new table

    SELECT * INTO event_log_temp FROM event_log

    And you can clear the old table data.

    DELETE FROM event_log

    0 讨论(0)
  • 2020-11-29 21:33

    A cleaner representation of what some other answers have hinted at:

    DELETE sourceTable
    OUTPUT DELETED.*
    INTO destTable (Comma, separated, list, of, columns)
    WHERE <conditions (if any)>
    
    0 讨论(0)
  • 2020-11-29 21:34

    You may use "Logical Partitioning" to switch data between tables:

    By updating the Partition Column, data will be automatically moved to the other table:

    here is the sample:

    CREATE TABLE TBL_Part1
    (id  INT NOT NULL,
     val VARCHAR(10) NULL,
     PartitionColumn  VARCHAR(10) CONSTRAINT CK_Part1 CHECK(PartitionColumn = 'TBL_Part1'),
     CONSTRAINT TBL_Part1_PK PRIMARY KEY(PartitionColumn, id)
    );
    
    CREATE TABLE TBL_Part2
    (id  INT NOT NULL,
     val VARCHAR(10) NULL,
     PartitionColumn  VARCHAR(10) CONSTRAINT CK_Part2 CHECK(PartitionColumn = 'TBL_Part2'),
     CONSTRAINT TBL_Part2_PK  PRIMARY KEY(PartitionColumn, id)
    );
    
    GO
    
    CREATE VIEW TBL(id, val, PartitionColumn)
    WITH SCHEMABINDING
    AS
         SELECT id, val, PartitionColumn FROM dbo.TBL_Part1
         UNION ALL  
         SELECT id, val, PartitionColumn FROM dbo.TBL_Part2;
    
    GO
    
    --Insert sample to TBL ( will be inserted to Part1 )
    INSERT INTO TBL
    VALUES(1, 'rec1', 'TBL_Part1');
    
    INSERT INTO TBL
    VALUES(2, 'rec2', 'TBL_Part1');
    
    GO
    
    --Query sub table to verify
    SELECT * FROM TBL_Part1
    
    GO
    --move the data to table TBL_Part2 by Logical Partition switching technique
    UPDATE TBL
      SET
          PartitionColumn = 'TBL_Part2';
    
    GO
    
    --Query sub table to verify
    SELECT * FROM TBL_Part2
    
    0 讨论(0)
  • 2020-11-29 21:35

    Here is how do it with single statement

    WITH deleted_rows AS (
    DELETE FROM source_table WHERE id = 1
    RETURNING *
    ) 
    INSERT INTO destination_table 
    SELECT * FROM deleted_rows;
    

    EXAMPLE:

        postgres=# select * from test1 ;
     id |  name
    ----+--------
      1 | yogesh
      2 | Raunak
      3 | Varun
    (3 rows)
    
    
    postgres=# select * from test2;
     id | name
    ----+------
    (0 rows)
    
    
    postgres=# WITH deleted_rows AS (
    postgres(# DELETE FROM test1 WHERE id = 1
    postgres(# RETURNING *
    postgres(# )
    postgres-# INSERT INTO test2
    postgres-# SELECT * FROM deleted_rows;
    INSERT 0 1
    
    
    postgres=# select * from test2;
     id |  name
    ----+--------
      1 | yogesh
    (1 row)
    
    postgres=# select * from test1;
     id |  name
    ----+--------
      2 | Raunak
      3 | Varun
    
    0 讨论(0)
  • 2020-11-29 21:40

    All these answers run the same query for the INSERT and DELETE. As mentioned previously, this risks the DELETE picking up records inserted between statements and could be slow if the query is complex (although clever engines "should" make the second call fast).

    The correct way (assuming the INSERT is into a fresh table) is to do the DELETE against table1 using the key field of table2.

    The delete should be:

    DELETE FROM tbl_OldTableName WHERE id in (SELECT id FROM tbl_NewTableName)
    

    Excuse my syntax, I'm jumping between engines but you get the idea.

    0 讨论(0)
提交回复
热议问题