I seriously recommend you look at upgrading to SQL 2005 and use partitioning
to handle this. With partitioning you would not have to copy a single row
under the right conditions. But currently if you have an identity column you
could create a clustered index on it and do the inserts in smaller bacthes
of say 10K at a time.
SET NOCOUNT ON
DECLARE @Total INT, @Counter INT, @Loops INT
DECLARE @Begin INT, @End INT
SET @Begin = 1
SET @End = 10000
SET @Counter = 1
SELECT @Total = MAX(R_ID)
FROM Test3
SET @Total = 150000 -- For testing purposes
-- See how many loops we need to make by dividing the total rows by the
-- amount we will work on in each pass
SET @Loops = (@Total / 10000)
WHILE @Counter <= @Loops
BEGIN
SET @StartTime = GETDATE()
INSERT INTO [dbo].[TestInserts]
([Sub_ID],[LName],[MDate])
SELECT [Sub_ID],[LName],[MDate]
FROM TEST3 WITH (NOLOCK) -- or TABLOCK **
WHERE R_ID BETWEEN @Begin AND @End
SET @Begin = @Begin + 10000
SET @End = @End + 10000
SET @Counter = @Counter + 1
-- WAITFOR DELAY '00:00:01'
END
--
Andrew J. Kelly SQL MVP
[quoted text, click to view] "Usenet User" < @ . > wrote in message
news:46ll13hmveh4b3q3anhifk54il5qccvrd1@4ax.com...
> On 9 Apr 2007 10:32:46 -0700, "SQL Menace" <denis.gobo@gmail.com>
> wrote:
>
>>On Apr 9, 1:01 pm, Usenet User < @ . > wrote:
>>> Environment: SQL 2000 Standard SP4
>>> Database recovery mode: Simple
>>>
>>> I have a stored proc that once a day transfers a large amount of data
>>> (2+ million rows usually) from one table to another as follows:
>>>
>>> INSERT INTO table1
>>> SELECT * FROM table2
>>>
>>> DROP table2
>>>
>>> This doesn't scale well obviously, and quite often the above operation
>>> results in "could not obtain lock" error.
>>>
>>> Could someone suggest, please, some less brutal way of copying rows,
>>> like in batches, so example? None of the tables have primary keys or
>>> identity columns, by the way.
>>>
>>> TIA!
>>
>>What about BCP OUT and then BCP IN or BULK INSERT (this is what is
>>called a minimally logged operation)
>>
>>>>None of the tables have primary keys or
>>> identity columns, by the way.
>>Can't wait to hear the Celko answer on this one ;-)
>>How are you going to do a batch if you don't have a unique identifier?
>
> Ok, I agree. Now, suppose I have an identity column included in
> table2. How would I go about doing the above task in batches?
>
> TIA!