Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Question:

I have a script with around 45 thousand insert from select statements. When I try and run it, I get an error message stating that I have run out of memory. How can I get this script to run?

Context:

  1. Added some new data fields to make an app play nice with another app the client uses.
  2. Got a spreadsheet of data from the client full of data that mapped current data items to values for these new fields.
  3. Converted spreadsheet to insert statements.
  4. If I only run some of the statements it works but the entire script does not.
  5. No. There are no typos.

If there is a different way I should be loading this data feel free to chastise me and let me know.

share|improve this question
    
Similar question on SO: (stackoverflow.com/questions/222442/…) Not sure if the answer helps –  jumpdart Apr 19 '12 at 14:24
add comment

3 Answers

up vote 10 down vote accepted

The maximum batch size for SQL Server 2005 is 65,536 * Network Packet Size (NPS), where NPS is usually 4KB. That works out to 256 MB. That would mean that your insert statements would average 5.8 KB each. That doesn't seem right, but maybe there are extraneous spaces or something unusual in there.

My first suggestion would be to put a "GO" statement after every INSERT statement. This will break your single batch of 45,000 INSERT statements into 45,000 separate batches. This should be easier to digest. Be careful, if one of those inserts fails you may have a hard time finding the culprit. You might want to protect yourself with a transaction. You can add those statements quickly if your editor has a good search-and-replace (that will let you search on and replace return characters like \r\n) or a macro facility.

The second suggestion is to use a Wizard to import the data straight from Excel. The wizard builds a little SSIS package for you, behind the scenes, and then runs that. It won't have this problem.

share|improve this answer
    
A GO after every statement? Well, I guess if you're generating them using another script that's OK. Otherwise, I'd just put one after every 1000 INSERTs. With regards to making the transaction atomic and minimizing the size of the transaction, why not load all the rows into a temp table or table variable and then load them in one shot from there to the target table? –  Nick Chammas Apr 19 '12 at 15:14
    
A 1000 is just as good as 1, but harder to count. To be honest, he might get away with just one GO statement, at the halfway mark, near statement 21,500. I like the GO fix because it doesn't require complicated editing of the current script, or counting INSERT statements (which might not map directly to line numbers). –  darin strait Apr 19 '12 at 16:00
1  
Surely, even a bad approximation of 1000 statements is good enough. :) –  Nick Chammas Apr 19 '12 at 16:13
    
Adding the GOs was a quick and easy fix.. 25mb script runs in a little less than 9min with no problems. Wanted to have it as a script to keep it within our standard patch deployment process for when it goes out. –  spaghetticowboy Apr 19 '12 at 17:58
add comment

BULK INSERT or bcp seem more appropriate options than 45,000 insert statements.

If you need to stick with the insert statements, I would consider a few options:

A: Use transactions and wrap batches of 100 or 500 or 1000 statements in each one to minimize the impact on the log and the batch. e.g.

BEGIN TRANSACTION;
INSERT dbo.table(a, ...) SELECT 1, ...
INSERT dbo.table(a, ...) SELECT 2, ...
...
INSERT dbo.table(a, ...) SELECT 500, ...
COMMIT TRANSACTION;
GO

BEGIN TRANSACTION;
INSERT dbo.table(a, ...) SELECT 1, ...
INSERT dbo.table(a, ...) SELECT 2, ...
...
INSERT dbo.table(a, ...) SELECT 500, ...
COMMIT TRANSACTION;
GO

B: Instead of individual insert statements, use UNION ALL for 100 or 500 statements at a time, e.g.

INSERT dbo.table(a, ...)
SELECT 1, ...
UNION ALL SELECT 2, ...
...
UNION ALL SELECT 500, ...
GO

INSERT dbo.table(a, ...)
SELECT 501, ...
UNION ALL SELECT 502, ...
...
UNION ALL SELECT 1000, ...
GO

I've left error handling out for brevity, but the point is that I would never try to send a single batch of 45,000 individual statements to SQL Server.

share|improve this answer
1  
Too bad the OP can't use table-value constructors, a 2008+ feature. He'd still have to batch the inserts into groups of 1000 rows, which is the maximum you can group together with a TVC. –  Nick Chammas Apr 19 '12 at 15:06
    
That was going to be my first suggestion until I saw the version tag. –  Aaron Bertrand Apr 19 '12 at 15:42
1  
@NickChammas - The performance of those degrades non linearly with the number of values clauses BTW. I submitted a connect item with a repro of inserting 1000 rows with 10 VARCHAR(800) columns on 2008 with a compile time of 12.5 minutes on my 2008 dev instance as it does a lot of unnecessary work comparing values rather than just getting on with inserting them (performs much quicker when parameterised and no values to look at). Though much improved in 2012 the non linear pattern still exists & should be fixed in version after. –  Martin Smith Apr 21 '12 at 9:38
add comment

I am not sure why you are getting the out of memory error, but there is an easier approach.

If you can export the data from the spreadsheet into a delimited format (e.g. csv) you can use the data import wizard in SSMS to insert the data for you:

SSMS import data task.

share|improve this answer
    
thats is helpful but i dont have access to the clients databases. I have to prepare patches and dataloads in scripts –  spaghetticowboy Dec 27 '12 at 21:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.