Efficient Data Transfer Between SQL Server Tables with Identical Structures
Several methods exist for copying or appending data between SQL Server tables sharing the same schema. A simple and efficient technique utilizes the INSERT INTO ... SELECT
statement.
To create a complete duplicate of a table (including schema and data), use this command:
<code class="language-sql">SELECT * INTO newTable FROM oldTable;</code>
This efficiently copies all data from oldTable
into a newly created table, newTable
.
Alternatively, to append data from one table to another existing table (without recreating the schema), use:
<code class="language-sql">INSERT INTO targetTable SELECT * FROM sourceTable;</code>
This method assumes both tables possess identical column structures.
For tables with differing column structures, explicitly specify the columns in both the INSERT INTO
and SELECT
clauses:
<code class="language-sql">INSERT INTO targetTable (columnA, columnB, columnC) SELECT columnX, columnY, columnZ FROM sourceTable;</code>
This approach ensures accurate data insertion into the corresponding columns of the targetTable
. Remember to match column data types for successful data transfer.
The above is the detailed content of How to Efficiently Copy Data Between SQL Server Tables with Identical Schemas?. For more information, please follow other related articles on the PHP Chinese website!