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
insert into #tt2
SELECT TOP 1000 [sID]
FROM [docSVsys]
where (select count(*) from #tt2) < 1000
But I would still use .NET and TPL.
This will still process all tables but it should process 0 rows once it gets to 1000
The following SQL limits the number of rows to 4 from any subsequent query
SET ROWCOUNT 4
SET @Rows = @@ROWCOUNT
Get the number of rows form the preceeding select
so something like
SELECT TOP etc...
SET @Rows = @@ROWCOUNT
SET ROWCOUNT 1000 - @Rows
Will probably error if @row goes below zero
but you an trap this with IF and goto PROC_LABLE
The following will likely give you a plan that achieves your desired result of not processing any rows after the 1,000th one has been found.
WITH CTE
AS (SELECT Text
FROM A
WHERE CONTAINS(Text, '"searchString"')
UNION ALL
SELECT Text
FROM B
WHERE CONTAINS(Text, '"searchString"')
UNION ALL
SELECT Text
FROM C
WHERE CONTAINS(Text, '"searchString"'))
INSERT INTO Results
SELECT TOP 1000 Text
FROM CTE
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.