The Problem
I have been having problems importing data into SQL Server from Excel file for nearly a year. I would perform an OpenDataSource command in SQL to open an Excel 97-2003 file and import the data into a temporary table. The problem is that it would work once and would then return the following error on every attempt after that, until the SQL Server service was restarted.
The OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)" reported an error. The provider did not give any information about the error. Cannot initialize the data source object of OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)".
You can also get the following error when performing queries.
Msg 10314, Level 16, State 11, Line 1
An error occurred in the Microsoft .NET Framework while trying to load assembly id 66669. The server may be running out of resources, or the assembly may not be trusted with PERMISSION_SET = EXTERNAL_ACCESS or UNSAFE. Run the query again, or check documentation to see how to solve the assembly trust issues. For more information about this error:
System.IO.FileLoadException: Could not load file or assembly 'nexgen database, Version=1.0.3945.15002, Culture=neutral, PublicKeyToken=a80b074634e01a5e' or one of its dependencies. An error relating to security occurred. (Exception from HRESULT: 0x8013150A)
System.IO.FileLoadException:
at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)
I submitted several forum posts over this time trying to elicite some sort of information on how to fix this problem. Here are a couple of the forum posts I made to try to get this problem fixed.
- http://social.msdn.microsoft.com/Forums/en-US/sqldatabaseengine/thread/aef8303d-06c0-4c52-b06a-9f1bf3705664
- http://social.msdn.microsoft.com/Forums/en/sqlsetupandupgrade/thread/f613520f-4ba0-4597-8b93-8f3e1774a1fd
Attempts to Fix It
However, all of my attempts at getting this resolved always resulted in either a ton of people replying that they’re also experiencing this issue or links to the same old articles. Here are some of the things that MS KB Articles and other resources around the Internet recommended along with some things I tried in order to resolve this problem. There is a mess of other things I tried as well, but I don’t want to have 3 pages of things that don’t work listed here.
- Make sure the user that the SQL Server service is running as has read and write access to the temp and tmp environment variable folders.
- Try importing a different file.
- Try a file that’s local to the SQL Server.
- Try setting up a Linked Server for the file.
- Run ASP.NET, SQL Server, memory, file system, registry, process and service traces to try to track down the problem.
- Install SQL Server 2005 SP3, which according to this article is supposed to fix the excessive open temp tables problem.
Recently I received a message from someone named Jim Cheong. He posted a comment through my first blog post on this site asking whether I had solved the problem in this MSDN forum post. After talking with him he said that he was experiencing the same issues and had possibly come across a fix for it, which he was currently trying out.
The Root of the Problem
He said that the MemToLeave area might be to blame and pointed me towards this SQL Server Central article. I ran this script they have on their site to determine the amount of total available memory and largest free size. I found that I only had 18MB of available memory and the largest free block was 11MB.
With VAS_Summary As (
Select Size = VAS_Dump.Size,
Reserved = SUM( Case(Convert(int, VAS_Dump.Base) ^ 0)
When 0
Then 1
Else 0
End),
Free = Sum( Case(Convert(int, VAS_Dump.Base) ^ 0)
When 0
Then 1
Else 0
End)
From (Select Convert(varbinary, Sum(region_size_in_bytes)) [Size],
region_allocation_base_address [Base]
From sys.dm_os_virtual_address_dump
Where region_allocation_base_address <> 0x0
Group By region_allocation_base_address
Union
Select CONVERT(varbinary, region_size_in_bytes) [Size],
region_allocation_base_address [Base]
From sys.dm_os_virtual_address_dump
Where region_allocation_base_address = 0x0) As VAS_Dump
Group By Size)
Select SUM(convert(bigint, Size)*Free)/1024 As [Memory: Total Avail (KB)],
CAST(max(size) as bigint)/1024 As [Memory: Max Free (KB)]
From VAS_Summary
Where Free <> 0
According to testing by Jim, he found that the OpenDataSource and OpenRowSet commands need approximately 71MB of free memory in order to run. This means that I was far below the necessary amount in order to run. So, the imports would fail until I restarted the SQL Server service, thus freeing up this memory and allowing me to do an import (maybe 2) before it would start failing again.
The Real Fix
As the SQL Server Central article states though, allocating more memory to the MemToLeave pool isn’t as easy as just setting the SQL startup switch. Whatever additional memory you allocate to the MemToLeave pool is taken from the Buffer Pool (BPool), which is SQL’s main operating memory. For this reason, reducing the amount of memory in this pool can have some very undesirable side effects. So, set this responsibly, do testing out of production first and get ready to set it back if things don’t work out the way you hoped, but here is how you allocate more memory to the MemToLeave pool.
- Open SQL Server Configuration Manager.
- Select the SQL Server Services folder in the left pane.
- Right-click the SQL Server (MSSQLSERVER) service in the right pane.
- Click Properties.
- Click the Advanced tab in the properties dialog that pops up.
- Add “-g512;” to the front of the value for parameter “Startup Parameters”.
- Click OK.
Summary
So, Jim and I setup processes to track how much total available and free memory we had over time. At the end of a week we compared notes. Both of us had well over the necessary amount, even after a week and neither of us had experienced the problem again. For me, it’s been more than 2 weeks now and I have not had to touch the SQL Server service at all. This has been a major life saver and I am eternally greatful to Jim for spotting this solution!
You post is very useful, which solved our problem.
We were trying to import Excel/CSV files using OPENROWSET with Microsoft.ACE.OLEDB.12.0, maximum file size tested was 1.74MB. Occasionally, a query could hang forever, which caused subsequent queries to hang as well. The server Activity Monitor would say Wait Type: OLEDB, Wait Resource: External. Killing those session caused them go into KILL/ROLLBACK state, and the sessions stayed there until the server was restarted.
By adding the -g512 to the startup parameter for the SQL Server service, the problem had gone away.
Thanks again for the solution!
No problem! As embarassing as it is, I tracked down problems and possible fixes for this issue for almost a year before I ran into Jim and we came across the solution. I can’t believe nothing was posted about this in relation to the OpenRowSet and OpenDataSource problems. So, I figured I had to write a blog post about it to save others the headache. Thanks for the comment!
Thanks a lot. You saved my life ! I spent hours trying to fix this problem. -g256 was enough for my server. Perhaps you could suggest to Microsoft an error message more explicit for this memory problem.
Thanks again.
Patrick
Good, I’m glad I could help! I know what you mean. I spent so much time trying to track this down. Me and a colleague were both shaking our heads that such a common issue could have such an undocumented solution. That’s why I wrote the article; to hopefully help people like yourself finally find a fix for this.
If I get some time, I’ll see about talking to MS about creating some sort of documentation for this problem.
When I applied this change, SQL Services would not start. Did you run across anything like that.
Never mind. Got it to start. Still getting NULL message.
Sam, what option size did you use (e.g. -g512, -g256, etc.)? Please also check the following.
These are just some things to check. Let me know if you get it resolved.
I was able to get the problem resolved. I think I had the syntax incorrect.
My original problem was that I didn’t put a ; between the last entry and the -g512.
Your article really helped put us on the right track to getting our issue resolved. Thanks…
This looks very relavant to the issue and I am please that somebody figured this out. My problem is a little different though. I used the jet 4.0 drivers for openrowset with few issues. Now I’d like to start using the ACE OLEDB 12.0 drivers to view xlsx files. I can, through many reboots and reinstalls, get them to work but the Jet 4.0 drivers stop working. However, once i reboot the server, the Jet drivers work and the ACE drivers stop working. Any clues what i am doing wrong?
This seems to be a little different issue than what I was experiencing… in symptom. However, the source of the problem could be the same if the ACE driver also uses the MemToLeave memory space. I would run the script that I have in this article and view how much free space you have before, during and after imports with both drivers. Often once these imports happen the memory is not freed up for quite some time and even when it is, not all of it is returned to the pool. What I did was create a job that inserted these values into a table every 10 minutes and ran it for about a week. Then I was able to see what was happening with this memory space over time and see where I was dropping below an acceptable memory level, which was causing future imports to fail. I’m glad this article was of use to you. Let me know if you have further questions!
Thanks a lot. your solution can fix our problem . thanks again
You’re welcome! I’m very glad it solved the problem. It makes me happy to know that I’m saving people all the trouble I had to go through until I found the solution. Have a great day!
I had this issue with an Access database as well. While this worked I was also able to resolve the problem by replacing the Jet Driver reference with:
Microsoft.ACE.OLEDB.12.0
One mention, this doesn’t work on 64 bit. Microsoft doesn’t have a 64 bit compatible driver and no plans to.
I wish I could say it has solved my problem but it hasn’t. I’m trying to open an Excel spreadsheet using a query along the lines:
SELECT * FROM OPENROWSET( ‘Microsoft.Jet.OLEDB.4.0′,’Excel 8.0;Database=C:\Myfolder\MyFile.xls’,'SELECT * FROM [Sheet1$]‘)
and it fails with the error (SQL2008 R2 Express under Windows 7):
Cannot initialize the data source object of OLE DB provider “Microsoft.Jet.OLEDB.4.0″ for linked server “(null)”
The same code works fine under SQL 2005 Express and Windows XP. It’s driving me potty! The destination file exists. I tried installing Office 2010 in case absence of Excel was an issue but that made no difference.
2008 r2 is 64 bit. the jet 4.0 drivers are 32 bit and will not work. Install the new ace 12.0 drivers and change your query to this:
SELECT * FROM OPENROWSET(‘Microsoft.ACE.OLEDB.12.0′,’Excel 12.0;Database=c:\MyFolder\MyFile.xls;’, ‘SELECT * FROM [Sheet1$]‘);
That should take care of it.
Thanks.
Actually I have just managed to solve it by changing the SQL Server start account to my own personal account.
Sorry I didn’t get to your comments before now. Yea, SQL uses the account that the SQL Server service is running under to access anything from the file system. So, you must ensure that the account has permissions to the file system where you’re going to open files. This gets especially tricky when opening files from shares on another computer, but it can be solved. I’m glad you got this up and working!
Steve, I work on SQL 2008 and Windows 2008 both 32 bit. I’ve been struggling all morning with the Jet driver issue. Changing to the Ace driver saved my day.
When I run the sql statement it gives me
57 MB & 15 MB in first & 2nd column.
As you have mentioned that OpenDataSource and OpenRowSet commands need approximately 71MB of free memory in order to run. It means my test system can never run this. Can you please explain what does “-g512″ parameter will do?
Thanks
Shafiq
Hi Shafiq! The “-g512″ parameter takes 512MB of memory from SQL’s primary memory space (BPool or Buffer Pool) and allocates it to the MemToLeave memory space. This memory area is used for things like OpenRowSet, OpenDataSource, SQL CLR, etc. The default is 256MB, which is hardly ever adequate. Please note though (as mentioned in the article) that allocating more memory to the MemToLeave area removes this memory from the BPool area. Depending on the amount of memory your server has, this can cause performance problems and other issues. So, please test this before going live in a production environment. I hope that helps!
- Nathon
Thanks for the post. It worked for me. I chacked the available memory which was about 56MB. I then simply restarted the service. This was done in our dev environment. I was on the hook because I had promised the open row set was a quick and easy solution. I will adjust the -g512 after analysis of other memory requirements.
Thanks again to you and your partner…
Joseph
No problem Joseph. I understand being under pressure to get things fixed immediately. I hope it takes care of the issue for you more permanently.
Thanks Nathon, your solotuion resolves my problem.
Great Tatiane! I’m very glad it helped you! Before I wrote this article nothing existed on this problem. It took me over a year to come across the solution. So I’m very glad it is helping so many people not have to go through what I did!
Thank you so much Nathon, for sharing not only the solution but also the explanation.
I struggled with this for a long time. I’m using SQL Server 2008 R2(32bit) developer edition on Windows 7(32bit).
Maria,
You’re quite welcome! I’m very glad to help so many people with this. It makes me feel good when I get these comments about having saved people the trouble I went through. It sort of makes up for it having taken me a year to figure out (as embarassing as that is). If you love the article make sure to vote for it!
Have a great day!
Nathon
I have been battling with this problem too – I am importing a range of XLS files, the largest of which is around 50MB. The testing (on smaller test files!) worked perfectly until I ran the job this morning against all the files and was side-swiped by the dreaded non-specific error. My confusion was exacerbated by the the fact that no further imports would run until I restarted in desperation! I was initially disappointed that your “-g512;” solution did NOT work, but I persisted along this line and eventually threw “-g1024;” at it: Bingo, it works! So it seems that this subsystem is quite memory hungry…
Henri,
Yea, it all depends on your individual configuration, the amount of data being imported and how much other stuff is utilizing those resources.
As I mentioned in the article though you have to be careful because SQL will “rob Peter to pay Paul”. Whatever you assign to the MemToLeave area above 256MB is taken from the BPool. Since the buffer pool is SQLs primary memory space, you can do real performance damage. So, the best plan is to have a test environment or try it during off hours first.
Anyway, I’m glad it helped!
Sir, you are a hero! I’ve spent hours on this – Iwas at the point of desperation when I found your site, and sceptically gave it a go. And it works – beautiful. Thank you.
I’m so glad that it helped you as well! I know exactly what you were going through. If you read the other comments you’ll see that I battled this issue on and off for the better part of a year before nailing down the exact cause. It still remains the most popular article on my blog, so it must be helping others. That’s why I posted it, so others don’t have to go through what I did. Cheers!
i getting same error message, but my case i am using sql2000.
Server: Msg 7399, Level 16, State 1, Line 1
OLE DB provider ‘Microsoft.Jet.OLEDB.4.0′ reported an error. The provider did not give any information about the error.
OLE DB error trace [OLE/DB Provider 'Microsoft.Jet.OLEDB.4.0' IDBInitialize::Initialize returned 0x80004005: The provider did not give any information about the error.].
it was working well, only since today it is giving error. my other server with same configuration working well.
can u help on it.
mahbub, sorry for the late reply. I’m starting a new job and trying to move across the state. Regarding the issue you’re having, I would first try normal troubleshooting steps. The error I describe above is recurring. I would check things like ensuring the Tab name in Excel (or whatever you’re importing from) is correct, make sure the file is not open while importing, ensure you have permissions to read the file, reboot the server, etc. Let me know if you still have issues.
Like others, this solved my issue.
Unfortunately, it looks like the 512 might have caused corruption of my master table as I no longer have my login and the error log shows an issue with
After this started working, I took it down to 128 and then I took it out completely (each time restarting the service.) It still is working without the -g parameter in place. Very odd.
Here’s hoping a reboot somehow clears up the master issue.
Paul, I’m sorry to hear that it caused issues for you. I have never heard of it causing master database corruption, removal of accounts, etc. If I were to guess, I’d say something else was at work with those issues. However, I do hope they are resolved when you reboot.
Nathon:
Yes, fortunately, there were other issues at play that had created the master table problems. So that’s good as it would have seemed strange (although I thought maybe pulling too much ram had caused corruption.)
But unfortunately, the -g parameter did not fix my problem.
And I’ve opened up every temp folder I can find. Very odd.
However, I did get this to work finally with a linked server by using the Access 12.0 driver going to an Excel file. Very weird, makes some sense in a hand waving sort of way. I did find that it needed the file to be local (as others have pointed out. Even mapping a drive wouldn’t cut it.)
Here is what worked for me:
EXEC master.dbo.sp_addlinkedserver @server = N’ROADINV_XLS’, @srvproduct=N’Excel’,
@provider=N’Microsoft.ACE.OLEDB.12.0′,
@datasrc=N’C:\Temp\test.xls’,
@provstr=N’Excel 8.0′
Strange stuff and really, very dissapointing that an issue that so many folks have had problems with is still out there. I wonder if 2008 corrected this.
Hi Nathan
Thanks for this post. Helped me out loads.
Steve
Steve,
Thanks for the comment! I appreciate hearing the feedback! I’m glad this helped!
I have been having a issue with my SQL
EXEC master.dbo.sp_addlinkedserver @server = N’ROADINV_XLS’, @srvproduct=N’Excel’,
@provider=N’Microsoft.ACE.OLEDB.12.0′,
@datasrc=N’C:\Temp\test.xls’,
@provstr=N’Excel 8.0′
This worked for me aswell, so thanks Nathan
SolarSun,
Another happy customer! I’m very happy that it helped you as well. This is consistently the single most popular article on my blog, so you know there’s a lot of folks out there experiencing this issue!
[Censored] M$OFT!!!! YOU SAVED MY DAY!!! LOV YOU NATHON!
ok seriously, btw, i’m using SQL 2005, and ima constantly backup all my tables since there are reports saying bla bla bla tables might get corrupted….
thanks again pal, you saved my day
Adam,
Ha, ha… now you had to know I’d have to edit that post!
Anyway, I’m very glad that helped you out! That’s exactly why I published the article. It took me almost a year (on & off) to finally come across the answer to this incredibly frustrating issue! I agree that Microsoft should at least have an article similar to this on how to fix this issue along with the side effects you’d see. Oh, well. It just means that I get a very popular article! Thanks for the comment and have a great day!
[...] SQL Memory and External Data by Nathon Dalton [...]
i changed to -g512 but still its not working…
Did you restart the service and make sure you have a semicolon between it and the other settings.
Awesome, awesome, awesome.
Thanks so much!
Walter,
Ha, ha, ha… I love it when I get these comments where I’ve made someone’s day! It just brightens my whole day! Thanks Walter!
Genius, thanks so much
Ken, I’m glad I could help!