I have the following query where I would like to return two result sets. One being the results as a table and the second just returning the number of potential results, or MaxResults.
The last line errors with Invalid object name ResultsTemp
. It does not work until I comment out the second last line. It appears I can only use the ResultsTemp once.
DECLARE @StartRow int;
DECLARE @MaxRows int;
set @StartRow = 0;
set @MaxRows = 5;
WITH ResultsTemp AS
(
SELECT ROW_NUMBER() OVER (ORDER BY FTS.RANK DESC) AS RowId,
Id, Name FROM tNews
INNER JOIN CONTAINSTABLE(tNews, *, 'FORMSOF(INFLECTIONAL, hello)')
AS FTS ON tNews.Id = FTS.[KEY]
)
SELECT Id, Name, RowId FROM ResultsTemp
Group By Id, Name, RowId
Having RowId between @StartRow and (@StartRow + @MaxRows);
select COUNT(*) from ResultsTemp;
thank you
This can't be done (as far as I'm aware). There's three workarounds, one which allows you to keep as two distinct result sets, and the other two which require merging the results into the same result set (as either an extra row or an extra column).
UNION ALL
and give the count row suitable values for ID, Name and RowID so you can extract it from the rest of the dataSELECT COUNT(*)
into the primary result set as an extra column with aCROSS JOIN
or similar.For this latter, you can do it by changing your primary query to:
I can't vouch for the performance - you'd need to profile.