SQL Async Multiple Queries Combine Results — Maximum Results (Top XYZ)

后端 未结 4 1548
忘了有多久
忘了有多久 2021-01-26 08:47

I have a search function set up, where I run multiple queries simultaneously. The top 1000 results of each query are written to a table. (These run async--I am just leaving out

4条回答
  •  时光说笑
    2021-01-26 09:18

    If you setup your result table with an identity column you can achieve your goal using this query (let 'counter' be that column. don't forget to put an index on it)

    declare @remaining int
    select @remaining = 1000 - (max(counter) - min(counter) + 1) from result
    if @result>0
      insert into Result (Text) select top (@remaining) Text from MyTable
    

    Also, if you have a list of table names, you can use a while loop and exit if @result is 0.

提交回复
热议问题