在plpgsql的循环中递增数字 [英] Incrementing a number in a loop in plpgsql
问题描述
我无法从示例中立即找到它.我想在一个函数的循环中增加一个变量.
I couldn't find this immediately from the examples. I want to increment a variable in a loop, in a function.
例如:
DECLARE
iterator float4;
BEGIN
iterator = 1;
while iterator < 999
.....
iterator ++;
END;
这将如何完成?
我正在查看有关流量控制的这份文件:
http://www.postgresql.org/docs/8.4/static/plpgsql-control-structures.html
I was looking at this document about flow control:
http://www.postgresql.org/docs/8.4/static/plpgsql-control-structures.html
它们似乎与我无关,除非这些绝对是模拟增加变量的唯一方法.
And none of them seem to be relevant for me, unless these are absolutely the only ways to simulate incrementing a variable.
推荐答案
在plpgsql中递增变量:
To increment a variable in plpgsql:
iterator := iterator + 1;
没有++
运算符.
关于plpgsql中的赋值运算符:
About the assignment operator in plpgsql:
您的代码片段将像这样工作:
Your code fragment would work like this:
DECLARE
iterator float4 := 1; -- we can init at declaration time
BEGIN
WHILE iterator < 999
LOOP
iterator := iterator + 1;
-- do stuff
END LOOP;
END;
使用 FOR
循环:
FOR i in 1 .. 999 -- i is integer automatically, not float4
LOOP
-- do stuff
END LOOP;
变量
name
被自动定义为类型integer
,并且仅在循环内部存在(变量名称的任何现有定义在循环内都会被忽略).
The variable
name
is automatically defined as typeinteger
and exists only inside the loop (any existing definition of the variable name is ignored within the loop).
这篇关于在plpgsql的循环中递增数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!