Using “IN” in a WHERE clause where the number of items in the set is very large

前端 未结 10 2381
梦毁少年i
梦毁少年i 2021-02-13 05:30

I have a situation where I need to do an update on a very large set of rows that I can only identify by their ID (since the target records are selected by the user and have noth

10条回答
  •  情歌与酒
    2021-02-13 06:10

    I don't know the type of values in your IN list. If they are most of the values from 1 to 10,000, you might be able to process them to get something like:

    WHERE MyID BETWEEN 1 AND 10000 AND MyID NOT IN (3,7,4656,987)
    

    Or, if the NOT IN list would still be long, processing the list and generating a bunch of BETWEEN statements:

    WHERE MyID BETWEEN 1 AND 343 AND MyID BETWEEN 344 AND 400 ...
    

    And so forth.

    Last of all, you don't have to worry about how Jet will process an IN clause if you use a passthrough query. You can't do that in code, but you could have a saved QueryDef that is defined as a passthrough and alter the WHERE clause in code at runtime to use your IN list. Then it's all passed off to SQL Server, and SQL Server will decide best how to process it.

提交回复
热议问题