English 中文(简体)
在T-SQLIN运营商之后,阵列是否有最大规模?
原标题:Is there a maximum size for the array after the T-SQL IN operator?

I am tasked with the following: The customer wants to read a number (possibly thousands) of values from a csv file. I have to use these values in an SQL select statement like this:

SELECT * FROM myTable WHERE myTable.Value IN (cvsValue1, csvValue 2, ..., csvValueN)

The question is: Will this work for an arbitrary number of csv values, and will it perform well for large number of values?

I will need to save the SQL as a string internally in my C# application for later use. (if that makes a difference for alternative solutions)

最佳回答

你真的不想这样做。 最好将这些数值列入一个指数化表格,并作为子顺序使用IN(通常实施() 相对于阵列(通常作为系列操作实施)。

http://msdn.microsoft.com/en-us/library/ms177682.aspx“rel=”_BOL:

Including an extremely large number of values (many thousands) in an IN clause can consume resources and return errors 8623 or 8632. To work around this problem, store the items in the IN list in a table.

Error 8623:

The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partitions. Please simplify the query. If you believe you have received this message in error, contact Customer Support Services for more information.

Error 8632:

Internal error: An expression services limit has been reached. Please look for potentially complex expressions in your query, and try to simplify them.

问题回答

本国运营商的业绩确实很差,而且有许多价值(有时,你的请求没有这么做和坠毁)。

我也这样做。

“......

SELECT cast(Items as int) as id INTO #table_temp
FROM dbo.split(@str_ids_comma, , )

SELECT * 
FROM myTable 
WHERE EXISTS(
SELECT *
FROM  #table_temp
WHERE id = myTable.Value
)

“...... you can find a definition of split here : http://www.sqlservercentral.com/articles/Tally+Table/72993/

I guess that max allowed number of elements in the list for IN operator depends on database. I read somewhere I think, because I had similar problem, that OracleXE has a limitation of 1000 list elements. You should look into this for your specific database.
As for the alternative solution, you could split this query into several batches maybe, where list contains smaller number of elements.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...