我在 pl/sql 函数中有一个 for 循环,如下所示:
FOR i IN min..max LOOP
变量 i, min, max 声明为
NUMERIC
在我的情况下 min 和 max 非常大,但范围本身很小,即:
min = 3232236033
max = 3232236286
如您所见,范围约为 256,但使用此值 oracle 会引发数字溢出错误,我一直在思考如何使其工作。
我应该如何迭代这些值?
编辑
好的,我有一个有效的答案,使用最大/最小差异循环,但是真的不可能在 oracle 中循环遍历大值吗?
编辑 我检索到的错误是:
SQL Error: ORA-01426: nadmiar numeryczny
ORA-06512: przy "PS.DHCP", linia 88
01426. 00000 - "numeric overflow"
*Cause: Evaluation of an value expression causes an overflow/underflow.
*Action: Reduce the operands.
代码第 88 行是:
FOR client_ip IN min_host..max_host
min_host, max_host, client_ip 是
inet_aton
的结果(IP的数字表示)
最佳答案
似乎问题来自我被转换的数字太小(这似乎是 pl/sql 的错误),
您可以更改循环类型:
While 循环工作正常
set serveroutput on
/
declare
min_val number;
max_val number ;
iterator number ;
begin
min_val := 3232236033 ;
max_val := 3232236286 ;
iterator := min_val;
while iterator<=max_val loop
dbms_output.put_line(iterator);
iterator := iterator + 1;
end loop ;
end;
/
从这里:
http://download.oracle.com/docs/cd/E11882_01/appdev.112/e17126/controlstatements.htm#BABEFFDC
FOR LOOP Index
The index of a FOR LOOP statement is implicitly declared as a variable of type INTEGER that is local to the loop. The statements in the loop can read the value of the index, but cannot change it. Statements outside the loop cannot reference the index. After the FOR LOOP statement runs, the index is undefined. (A loop index is sometimes called a loop counter.)
In Example 4-17, the FOR LOOP statement tries to change the value of its index, causing an error.
接下来:
http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28370/loop_statement.htm
index_name
An undeclared identifier that names the loop index (sometimes called a loop counter). Its scope is the loop itself; you cannot reference the index outside the loop.
The implicit declaration of index_name overrides any other declaration outside the loop. To refer to another variable with the same name, use a label. See Example 4-22, "Referencing Global Variable with Same Name as Loop Counter".
Inside a loop, the index is treated like a constant: it can appear in expressions, but cannot be assigned a value.
因此,即使您在声明中声明了“索引”,它也不会在循环中使用,而是使用隐式创建的 INDEX(它的精度似乎太小,无法满足您的需要)
关于sql - Oracle11g for 循环中的数字溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5044451/