如何在Firebird select語句中創建1到100的範圍?如何在Firebird的select語句中創建從1到100的範圍?
我已經找到了將字符串轉換爲列表的方式,但它不適用於我,因爲我需要生成更大的範圍How to input an array parameter of values to Firebird Stored Procedure?。
是否有可能在不創建存儲過程的情況下執行此類操作?
如何在Firebird select語句中創建1到100的範圍?如何在Firebird的select語句中創建從1到100的範圍?
我已經找到了將字符串轉換爲列表的方式,但它不適用於我,因爲我需要生成更大的範圍How to input an array parameter of values to Firebird Stored Procedure?。
是否有可能在不創建存儲過程的情況下執行此類操作?
一種方法是遞歸CTE:
with recursive n as (
select 1 as n
from rdb$database
union all
select n.n + 1
from n
where n < 100
)
select n.n
from n;
除了戈登的建議,你也可以使用一個可選的存儲過程來實現這一點:
create procedure generate_range(startvalue integer, endvalue integer)
returns (outputvalue integer)
as
begin
outputvalue = startvalue;
suspend;
while (outputvalue < endvalue) do
begin
outputvalue = outputvalue + 1;
suspend;
end
end
然後,您可以使用它作爲:
select outputvalue from generate_range(1, 100);
戈登所顯示的CTE的好處是,它允許更大的範圍,如Firebird CTE的遞歸限制爲1024.
也更具可讀性 –
請注意,它適用於Firebird 2.1,但限於1024個元素;請參閱https://firebirdsql.org/refdocs/langrefupd21-select.html#langrefupd21-select-cte如果超出範圍,則會出現此錯誤:'對同一請求執行的併發執行太多。 –