One option is to remove this from the request and do something like:
declare @Numrows int; select @Numrows = (case @Group when 6500 then 10 when 5450 then 5 when 2010 then 3 when 2000 then 1 else 0 end); select top(@NumRows) * from Table1;
You can also do it like this:
with const as ( select (case @Group when 6500 then 10 when 5450 then 5 when 2010 then 3 when 2000 then 1 else 0 end) as Numrows ) select t.* from (select t.*, ROW_NUMBER() over () as seqnum from table1 t ) t cross join const where seqnum <= NumRows;
In this case, you need to specify the columns to avoid getting seqnum in the list.
By the way, usually when using top you should also have order by . Otherwise, the results will be uncertain.
Gordon linoff
source share