alter function [dbo].[GetCounterTable]
(
@V int
) returns @t table (N int) as begin
set @v = @v - 1
if @v > 30 begin
;with MyData as (
select
N = a.N + b.N * 10 + c.N * 100 + d.N * 1000
from
dbo.GetCounterTable(10) a
,dbo.GetCounterTable(10) b
,dbo.GetCounterTable(10) c
,dbo.GetCounterTable(10) d
)
insert into @t (N)
select N
from MyData
where N <= @v
order by N
end
else if @v > 0
insert into @t (N)
select (@v)
union select * from dbo.GetCounterTable(@v)
else if @v = 0
insert into @t values (0)
return
end