I was recently doing some testing that required a fairly large table. I created the test table, and set it up to insert about 30 million rows. I got an error that read:
The instance of the SQL Server Database Engine cannot obtain a LOCK resource at this time. Rerun your statement when there are fewer active users. Ask the database administrator to check the lock and memory configuration for this instance, or to check for long-running transactions.
Naturally, having not encountered that before, I started by Googling the error and looking up Error 1204 and Microsoft’s Support Article. The support article helpfully suggested setting locks to 0 using sp_configure. But that is the default value and mine was already set that way.
So, I created a simplified setup that would reliably produce the error based on Jeff Moden’s Tally Table Article.
if OBJECT_ID('BigTallyTable', 'U') is not NULL drop table dbo.BigTallyTable create table BigTallyTable (n BigInt primary key) insert into dbo.BigTallyTable(n) select top (30000000) row_number() over (order by s1.name) as n from master.dbo.syscolumns s1, master.dbo.syscolumns s2
And I started playing with it. Since this all tied to locks, one thing I did was break it up into batches:
declare @maxValue int, @maxperround int, @last int set @maxValue = 30000000 --30 Million set @maxPerRound = 500000 set @last = 0 while @last < @maxValue begin if @maxPerRound > @maxValue - @last set @maxPerRound = @maxValue - @last insert into dbo.BigTallyTable(n) select top (@maxPerRound) row_number() over (order by s1.name) + @last as n from master.dbo.syscolumns s1, master.dbo.syscolumns s2 Print 'Finished a round with @last = ' + cast(@last as varchar) + ' and @maxPerround = ' + cast(@maxPerRound as varchar) set @last = @last + @maxPerRound end
This avoided the error and generated the results I was looking for. On a multi-user system (I was doing this on my test instance of SQL which is on my personal machine) it would have also allowed other queries that were waiting for locks to be able to execute more freely, though whether that is necessarily good or not depends on the circumstances.
I also got it to lock by directing SQL Server to take a table lock right from the beginning.
insert into dbo.BigTallyTable with (TABLOCK) (n) select top (30000000) row_number() over (order by s1.name) as n from master.dbo.syscolumns s1, master.dbo.syscolumns s2
This ran substantially faster than splitting it into batches did and is clearly a simpler query. It does take a more aggressive table lock which, as the article on lock escalation points out, can cause more problems with blocking other queries if that is a concern.