Copying data between tables is a fundamental operation in database administration and development. Think about it: whether you are archiving historical records, creating a backup before a major update, or migrating data into a staging environment for ETL processes, understanding the nuances of how to copy table data in SQL Server is essential. And the platform offers several methods to achieve this, each with distinct performance characteristics, logging behaviors, and use cases. Choosing the right approach depends heavily on whether the target table already exists, the volume of data involved, and whether you need to preserve identity values, constraints, or indexes.
Understanding the Core Scenarios
Before diving into syntax, it is critical to identify which scenario matches your requirement. Generally, there are three distinct patterns:
- Create and Populate: The destination table does not exist. You want to create a new table structure identical to the source and copy the data in a single step.
- Append Data: The destination table exists with a matching schema. You need to insert rows from the source into this existing table.
- Structure Only: You need an empty copy of the table (schema, indexes, constraints) without any data.
Each scenario demands a different T-SQL command or combination of commands Easy to understand, harder to ignore..
Method 1: SELECT INTO — Creating a New Table with Data
The SELECT INTO statement is the fastest and most straightforward way to create a brand-new table populated with data from an existing source. It creates the target table on the fly based on the column definitions and data types of the result set.
Short version: it depends. Long version — keep reading Worth keeping that in mind..
Basic Syntax
SELECT Column1, Column2, Column3
INTO NewTableName
FROM SourceTable
WHERE Condition;
Key Characteristics and Limitations
- Speed: It is minimally logged in the
BULK_LOGGEDorSIMPLErecovery models, making it significantly faster than row-by-row inserts for large datasets. - Schema Replication: It copies column names, data types, and nullability. It also copies
IDENTITYproperties if the column is listed in the select list without an expression. - What It Misses: This is the most critical caveat.
SELECT INTOdoes not copy Primary Keys, Foreign Keys, Indexes, Check Constraints, Default Constraints, or Triggers. The resulting table is a "heap" (a table without a clustered index) unless you explicitly create indexes afterward. - Filegroup Placement: By default, the new table is created on the default filegroup of the database. You cannot specify a different filegroup directly in the statement.
Practical Example
Imagine you need a copy of the Sales.Orders table for the year 2023 for reporting purposes The details matter here. Surprisingly effective..
SELECT OrderID, CustomerID, OrderDate, TotalAmount
INTO Sales.Orders_2023_Archive
FROM Sales.Orders
WHERE YEAR(OrderDate) = 2023;
This instantly creates Sales.Orders_2023_Archive with the data. You would then need to run ALTER TABLE statements to add the Primary Key on OrderID and any necessary Non-Clustered Indexes to support your reporting queries.
Method 2: INSERT INTO SELECT — Appending to an Existing Table
When the destination table already exists—perhaps it is a permanent staging table, a data warehouse fact table, or an archive table with pre-defined indexes and partitioning—you must use INSERT INTO ... SELECT Worth knowing..
Basic Syntax
INSERT INTO DestinationTable (Column1, Column2, Column3)
SELECT Column1, Column2, Column3
FROM SourceTable
WHERE Condition;
Best Practices for Performance
- Explicit Column Lists: Always specify the column list in both the
INSERT INTOandSELECTclauses. Relying on ordinal position (INSERT INTO Table SELECT * FROM Source) is fragile; a schema change in the source table (like a new column added in the middle) will cause the statement to fail or, worse, insert data into the wrong columns. - Minimal Logging with TABLOCK: For large data loads into an empty or truncated table (heap or clustered index with no non-clustered indexes), you can achieve minimal logging by using the
TABLOCKhint.
INSERT INTO DestinationTable WITH (TABLOCK)
SELECT Column1, Column2
FROM SourceTable;
- Order of Insertion: If the destination table has a Clustered Index, inserting data in the order of the clustering key avoids page splits and fragmentation. Use
ORDER BYin theSELECTstatement if the source data isn't already sorted. - Batch Processing: For massive datasets (millions of rows), a single transaction can blow up the transaction log and cause blocking. It is often better to chunk the insert using a loop or
OFFSET/FETCH(orTOPwith aWHEREclause on an indexed column) to process 10,000–50,000 rows per transaction.
Handling Identity Columns
If the destination table has an IDENTITY column and you want to preserve the specific identity values from the source (rather than letting the destination generate new ones), you must enable IDENTITY_INSERT Still holds up..
SET IDENTITY_INSERT DestinationTable ON;
INSERT INTO DestinationTable (IdentityCol, ColA, ColB)
SELECT IdentityCol, ColA, ColB
FROM SourceTable;
SET IDENTITY_INSERT DestinationTable OFF;
Note: You can only have IDENTITY_INSERT set to ON for one table in a session at a time That's the part that actually makes a difference..
Method 3: Copying Schema Only (Structure Without Data)
Often, developers need an empty table structure to apply specific indexing strategies or partitioning schemes before loading data. You've got two clean ways worth knowing here.
Using SELECT INTO with a False Predicate
At its core, a quick, ad-hoc method. The WHERE 1 = 0 clause ensures the engine understands the schema but returns zero rows That alone is useful..
SELECT *
INTO NewEmptyTable
FROM SourceTable
WHERE 1 = 0;
Like the standard SELECT INTO, this copies column definitions and nullability but ignores Keys, Indexes, Constraints, and Triggers Easy to understand, harder to ignore. No workaround needed..
Using Scripting (The Professional Approach)
For production environments, the only reliable way to get an exact replica—including Primary Keys, Foreign Keys, Default Constraints, Check Constraints, Indexes (Clustered and Non-Clustered), and Partition Schemes—is to script the table And it works..
- In SQL Server Management Studio (SSMS), right-click the source table.
- Select Script Table as > CREATE To > New Query Editor Window.
- In the generated script, change the table name in the
CREATE TABLEstatement. - Execute the script.
This guarantees the target table is structurally identical to the source, ready for data loading via INSERT INTO SELECT or BULK INSERT Worth knowing..
Advanced Techniques for Large-Scale Data Movement
When dealing with tables containing tens or hundreds of millions of rows, standard T-SQL inserts can be too slow or log-intensive Simple, but easy to overlook..
1. BULK INSERT / BCP (Bulk Copy Program)
For the absolute highest throughput, exporting data to a flat file (native format preferred) using BCP OUT and importing it via BULK INSERT or BCP IN is the gold standard.
- Advantages: Maximum speed, minimal logging (with
TABLOCK), bypasses the query optimizer overhead. - Workflow:
bcp "SELECT * FROM SourceTable" queryout DataFile.dat -N -T -S ServerNameBULK INSERT DestinationTable FROM 'DataFile.dat' WITH (DATAFILETYPE = 'native', TABLOCK);
2. Partition Switching (Metadata-Only Operation)
If you are using Table Partitioning (
If you are using **Table Partitioning** (
a partitioned table or partition function), you can move data between tables almost instantaneously because it is a metadata-only operation. Instead of physically copying rows, SQL Server simply reassigns a partition from one table to another.
* **Advantages:** Near-instant transfer regardless of row count, zero data movement, minimal logging.
* **Requirements:** The source and destination tables must share an identical structure, the target partition must be empty, and both tables must reside on the same filegroup.
* **Workflow:**
```sql
ALTER TABLE SourceTable
SWITCH PARTITION 1
TO DestinationTable PARTITION 1;
This technique is the backbone of many data-warehousing ETL patterns, where new data is loaded into a staging partition and then switched into the main table in milliseconds Turns out it matters..
3. Enabling Minimal Logging
Regardless of the method, you can dramatically reduce transaction log growth by ensuring the operation is minimally logged. For SELECT INTO, INSERT ... SELECT, and BULK INSERT, the database recovery model matters:
- Under the Simple or Bulk-Logged recovery model, these operations can be minimally logged.
- Using the
TABLOCKhint (orWITH (TABLOCK)) is often required for minimal logging to kick in.
INSERT INTO DestinationTable WITH (TABLOCK)
SELECT * FROM SourceTable;
Choosing the Right Method
There is no single "best" approach—the right choice depends on your constraints:
| Scenario | Recommended Method |
|---|---|
| Quick copy of data into a new table | SELECT INTO |
| Copy data into an existing table | INSERT INTO SELECT |
| Preserve identity values | IDENTITY_INSERT |
| Replicate full structure (keys, indexes) | Scripting via SSMS |
| Maximum throughput for huge datasets | BULK INSERT / BCP |
| Instant transfer of partitioned data | Partition Switching |
Conclusion
Copying tables in SQL Server is a deceptively simple task that hides a surprising amount of nuance. While SELECT INTO and INSERT INTO SELECT cover the vast majority of everyday scenarios, professional-grade work demands an awareness of the details: preserving identity columns, replicating the full schema including constraints and indexes, and optimizing for scale through bulk operations or partition switching.
Bottom line: to match your technique to your goal. Also, reach for SELECT INTO when speed and simplicity matter most, script the table when structural fidelity is non-negotiable, and turn to BULK INSERT or partition switching when performance at scale is the priority. By understanding the tradeoffs of each method—particularly around logging, locking, and metadata—you can move data efficiently while keeping your production systems safe and responsive Easy to understand, harder to ignore. Which is the point..