生成数字范围的最快方法

时间:2016-09-28 15:23:18

标签: firebird firebird-3.0

在两个值之间生成数字的最快方法是什么。

例如:
第一价值:6,000,000
第二价值:7,500,000

我必须创建1,500,000行,如下所示

 6,000,001
 6,000,002
 .
 .
 7,500,000

2 个答案:

答案 0 :(得分:2)

这对我有用:

create or alter procedure GET_INTEGER_RANGE (
INICIO integer,
FIN integer)
returns (
    ACTUAL integer)
AS
begin
  actual = inicio;
  while (actual<=fin) do
  begin
       suspend;
       actual = actual +1;
  end
end

SELECT * FROM GET_INTEGER_RANGE(6000000,7500000);

答案 1 :(得分:1)

不确定这是否是最快的,但这是我能想到的唯一方法:

with recursive numbers (nr) as (
   select 6000000 
   from rdb$database
   union all
   select nr + 1
   from numbers
   where nr < 7500000
)
select * 
from numbers;

更新:正如franbenz在评论中所指出的,Firebird仅限于1024的递归深度,显然无法更改。因此,虽然基本语法是正确的,但在尝试生成超过1024行时,上述操作将无效。