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

前端 未结 10 2387
梦毁少年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:11

    I would use a table-variable / temp-table; insert the values into this, and join to it. Then you can use the same set multiple times. This works especially well if you are (for example) passing down a CSV of IDs as varchar. As a SQL Server example:

    DECLARE @ids TABLE (id int NOT NULL)
    
    INSERT @ids
    SELECT value
    FROM dbo.SplitCsv(@arg) // need to define separately
    
    UPDATE t
    SET    t. // etc
    FROM   [TABLE] t
    INNER JOIN @ids #i ON #i.id = t.id
    

提交回复
热议问题