简体   繁体   English

查询WHERE IN子句不起作用

[英]Query WHERE IN clause not working

In a SQL query we are using WHERE IN clause to filter the data. 在SQL查询中,我们使用WHERE IN子句来过滤数据。 When I am passing 35000 fields in WHERE IN clause, The ExecuteNonQuery throws 当我在WHERE IN子句中传递35000个字段时, ExecuteNonQuery抛出

Object reference not set to an instance exception 对象引用未设置为实例异常

I have used try catch in where the ExecuteNonQuery excuted, but the exception not catched current method, it catched in parent method (button click) 我在ExecuteNonQuery被执行的地方使用了try catch,但是异常没有捕获到当前方法,而是在父方法中捕获了(单击按钮)

If I reduced the count from 35000 to 25000 the SQL query works fine. 如果我将计数从35000减少到25000,则SQL查询工作正常。 Please help. 请帮忙。

SELECT * FROM COUNTRY WHERE CountryID in ('1','2',......'35000')

I have tried to use Temp Table in SQL also, Same error happened. 我也尝试在SQL中使用临时表,发生了相同的错误。

IF OBJECT_ID('tempdb..#temp)IS NOT NULL DROP #TEMP 
CREATE TABLE #TEMP
( CountryID int NULL)
INSERT INTO #TEMP VALUES ('1')
.
.
.
INSERT INTO #TEMP VALUES('10')

SELECT * FROM COUNTRY WHERE CountryID IN(SELECT CountryID from #temp)

The object null reference error is not the problem, How can i overcome the Where In clause issue in sql query. 对象空引用错误不是问题,我该如何克服sql查询中的Where In子句问题。 What would the possible resolution to avoid the Where in clause in sql query. 避免sql查询中的where in子句的可能解决方案是什么?

Please help. 请帮忙。 Thanks. 谢谢。

使用连接代替,这应该工作

SELECT * FROM COUNTRY c INNER JOIN #temp t on c.CountryId=t.CountryId

为什么不之间使用呢?

SELECT * FROM COUNTRY WHERE CountryID between 1 and 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM