views:

56

answers:

1
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  A: 

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.

egrunin
Pinalkumar Dave is authority for me, and he claims in other way:http://blog.sqlauthority.com/2009/03/11/sql-server-difference-between-union-vs-union-all-optimal-performance-comparison/
Bulk insert is not permitted
Are you permitted touse XML? You could pass all the codes as an XML document into SQL Server and then use the XML data type and its methods to insert it a table. Still much less efficient than BULK INSERT though.
Daniel Renshaw
unfortunately I can't...but idea is nice :)
My comment about `UNION ALL` meant: you don't need **any** kind of `UNION` if you do simple sequential `INSERT` s. Why don't you try it?
egrunin