Im looking for the fastest way to copy a table and its contents on my sql server just simple copy of the table with the source and destination on the same server/database.
Currently with a stored procedure select * into sql statement it takes 6.75 minutes to copy over 4.7 million records. This is too slow.
CREATE PROCEDURE [dbo].[CopyTable1]
AS
BEGIN
DECLARE @mainTable VARCHAR(255),
        @backupTable VARCHAR(255),
        @sql VARCHAR(255),
        @qry nvarchar(max);
SET NOCOUNT ON;
Set @mainTable='Table1'
Set @backupTable=@mainTable + '_Previous'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(@backupTable) AND type in (N'U'))
BEGIN
    SET @Sql = 'if exists (select * from sysobjects '
    SET @Sql = @Sql + 'where id = object_id(N''[' + @backupTable + ']'') and '
    SET @Sql = @Sql + 'OBJECTPROPERTY(id, N''IsUserTable'') = 1) ' + CHAR(13)
    SET @Sql = @Sql + 'DROP TABLE [' + @backupTable + ']'
    EXEC (@Sql)
END
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(@mainTable) AND type in (N'U'))
    SET @Sql = 'SELECT * INTO dbo.[' + @backupTable + '] FROM dbo.[' + @mainTable + ']'
    EXEC (@Sql)
END
                Using a SQL multi row fetch is the fastest way to copy data from one table to another (I was surprised how much faster it is when compared to CPYF). Using VARCHAR is slower than just a straight CHAR (not surprised).
In Object Explorer right-click the table you want to copy and select Design. Select the columns in the existing table and, from the Edit menu, select Copy. Switch back to the new table and select the first row. From the Edit menu, select Paste.
Quite quick too. Right-click on the database and choose Tasks/Export Data. A wizard will take you through the steps but you choosing your SQL server client as the data source and target will allow you to select the database and table(s) you wish to transfer.
If you are concerned about speed, it seems you have two alternatives; copying by block or the BCP/Bulk insert method.
DECLARE 
    @CurrentRow bigint, @RowCount bigint, @CurrentBlock bigint
SET 
    @CurrentRow = 1
SELECT 
    @RowCount = Count(*)
FROM 
    oldtable 
WITH (NOLOCK)
WHILE @CurrentRow < @RowCount
BEGIN
    SET 
        @CurrentBlock = @CurrentRow + 1000000
INSERT INTO 
    newtable 
(FIELDS,GO,HERE)
SELECT 
    FIELDS,GO,HERE
FROM (
    SELECT 
        FIELDS,GO,HERE, ROW_NUMBER() OVER (ORDER BY SomeColumn) AS RowNum 
    FROM 
        oldtable
    WITH (NOLOCK)
    ) AS MyDerivedTable
WHERE 
    MyDerivedTable.RowNum BETWEEN @startRow AND @endRow
SET 
    @CurrentRow = @CurrentBlock + 1
end
How to copy a huge table data into another table in SQL Server
SELECT 
    * 
INTO 
    NewTable 
FROM 
    OldTable 
WHERE 
    1=2
BULK INSERT 
    NewTable 
FROM 
    'c:\temp\OldTable.txt' 
WITH (DATAFILETYPE  = 'native')
What is the fastest way to copy data from one table to another
http://www.databasejournal.com/features/mssql/article.php/3507171/Transferring-Data-from-One-Table-to-Another.htm
You seem to want to copy a table that is a heap and has no indexes. That is the easiest case to get right. Just do a
insert into Target with (tablock) select * from Source
Make sure, that minimal logging for bulk operations is enabled (search for that term). Switch to the simple recovery model.
This will take up almost no log space because only allocations are logged with minimal logging.
This just scans the source in allocation order and append new bulk-allocated pages to the target.
Again, you have asked about the easiest case. Things get more complicated when indexes come into play.
Why not insert in batches? It's not necessary. Log space is not an issue. And because the target is not sorted (it is a heap) we don't need sort buffers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With