There is insufficient system memory to run this query when creating temporary table

徘徊边缘 提交于 2019-12-12 03:09:57

问题


StringBuilder query = new StringBuilder();
                    query.Append("CREATE TABLE #Codes (Code nvarchar(100) collate database_default ) ");
                    query.Append("Insert into #Codes (Code) ");
                    int lengthOfCodesArray = targetCodes.Length;
                    for (int index = 0; index < lengthOfCodesArray; index++)
                    {
                        string targetCode = targetCodes[index];
                        query.Append("Select N'" + targetCode + "' ");
                        if (index != lengthOfCodesArray - 1)
                        {
                            query.Append("Union All ");
                        }
                    }
  query.Append("drop table #Codes ");

on: cmd.ExecuteReader() I get

There is insufficient system memory to run this query when creating temporary table

But weird thing is that, when I have 25k codes is ok, when 5k I get this error. 

Initial size is 262 MB.

Lengt of each code is average 15.


回答1:


This produces one giant statement, and of course it fails eventually.

You should do your INSERT one at a time (no UNION ALL), at least until it's time to optimize.

I have a feeling that your ultimate answer is going to involve BULK INSERT, but I don't know enough about your application to be sure.



来源:https://stackoverflow.com/questions/2725353/there-is-insufficient-system-memory-to-run-this-query-when-creating-temporary-ta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!