我就废话不多说了,大家还是直接看代码吧~
create or replace function aa1(a1 integer[],a2 bigint) returns void AS $$ declare ii integer; declare num integer; begin II:=1; num = 1; FOR ii IN 1..a2 LOOP UPDATE student SET id=a1[num] WHERE cd_id = ii; num = num +1; if (num>6) then num = 1; end if; end loop; end; $$ LANGUAGE plpgsql; select aa1(array[1,4,5,6,7,8],6742)
补充:数据库之postgreSql库的存储过程和循环总结
postgreSql库中存储过程模板
CREATE OR REPLACE FUNCTION p_fx_*** ( OUT v_row INTEGER, OUT v_rote varchar(50), OUT v_log varchar(50)) AS $$ DECLARE BEGIN select count(*) into v_row from *插入表的名字*; v_rote := 'SUCCESS'; v_log := 'SUCCESS'; END $$ LANGUAGE plpgsql VOLATILE
postgreSql库中循环书写的模板,以实际开发中的sql为例
单层循环
do $$ declare ***:=***; begin while *** loop end loop; end $$;
declare --声明变量,如果声明了变量别忘了加分号;
双层循环
do $$
declare ***:=***;
begin
while *循环条件* loop
for i in 1..12 loop
raise notice '%',*变量名*;
end loop;
end loop;
end $$;
raise notice '%',变量名;这是输出语句类似于Java中的print。
将循环放到存储过程中
CREATE OR REPLACE FUNCTION p_fx_*** ( OUT v_row INTEGER, OUT v_rote varchar(50), OUT v_log varchar(50))
AS $$
DECLARE
BEGIN
while *循环条件* loop
for i in 1..12 loop
raise notice '%',*变量名*;
end loop;
end loop;
select count(*) into v_row from *插入表的名字*;
v_rote := 'SUCCESS';
v_log := 'SUCCESS';
END
$$
LANGUAGE plpgsql VOLATILE
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

