域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過(guò)
這篇文章主要介紹了postgresql 循環(huán)函數(shù)的簡(jiǎn)單實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧。
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
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)
補(bǔ)充:數(shù)據(jù)庫(kù)之postgreSql庫(kù)的存儲(chǔ)過(guò)程和循環(huán)總結(jié)
postgreSql庫(kù)中存儲(chǔ)過(guò)程模板
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庫(kù)中循環(huán)書寫的模板,以實(shí)際開發(fā)中的sql為例
單層循環(huán)
do $$
declare ***:=***;
begin
while *** loop
end loop;
end $$;
declare --聲明變量,如果聲明了變量別忘了加分號(hào);
雙層循環(huán)
do $$
declare ***:=***;
begin
while *循環(huán)條件* loop
for i in 1..12 loop
raise notice '%',*變量名*;
end loop;
end loop;
end $$;
將循環(huán)放到存儲(chǔ)過(guò)程中
CREATE OR REPLACE FUNCTION p_fx_*** ( OUT v_row INTEGER, OUT v_rote varchar(50), OUT v_log varchar(50))
AS $$
DECLARE
BEGIN
while *循環(huán)條件* 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
文章來(lái)源:腳本之家
來(lái)源地址:https://www.jb51.net/article/204227.htm
申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!