UFN_NUMBERTABLE

Create a number table

Return

Return Type
table

Parameters

Parameter Parameter Type Mode Description
@MIN int IN
@MAX int IN

Definition

Copy


create function dbo.UFN_NUMBERTABLE (@MIN integer, @MAX integer)
returns @NUMBERTABLE table (NUMBER integer NOT NULL PRIMARY KEY)
as
begin
  insert @NUMBERTABLE values (@MIN)
  while @@rowcount > 0
    begin
      insert @NUMBERTABLE 
      select 
        t.NUMBER + (x.MAXNUMBER - @MIN + 1)
      from @NUMBERTABLE t
        CROSS JOIN (select MAXNUMBER = MAX(NUMBER) FROM @NUMBERTABLE) x 
      where
        --shift the increment to the right side to take advantage of index

        t.NUMBER <= @MAX - (x.MAXNUMBER - @MIN + 1)
    end
  return
end