postgresql 中的 while 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6094566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:02:09  来源:igfitidea点击:

while function in postgresql

postgresqlplpgsql

提问by nike

I want to insert records to a table by function.

我想按函数向表中插入记录。

CREATE OR REPLACE FUNCTION insert_wilda()
    RETURNS integer AS
$BODY$
DECLARE
    countKab integer;
    i integer;
BEGIN
    i := 1;
    SELECT count(*)+34 into countKab from m_kab
    WHILE (i <= countKab)
    loop

        INSERT INTO "t_historyWilda"("kdHistory","kdProp","kdKab","kdKec","nmWilda","noUrut",bulan,tahun,"isActive")
        SELECT 'i',"kdProp","kdKab",'000',"namaKab",'1', EXTRACT(MONTH FROM TIMESTAMP 'now()'), EXTRACT(YEAR FROM TIMESTAMP 'now()'),'1' FROM m_kab
    end loop;
    RETURN i;
END;
$BODY$
    LANGUAGE plpgsql VOLATILE
    COST 100;

But I got error like this:

但我得到了这样的错误:

ERROR:  syntax error at or near "<="
LINE 10:  WHILE (i <= countKab)
                   ^

********** Error **********

ERROR: syntax error at or near "<="

回答by Adrian Serafin

You missed ';' after this line

你错过了 ';' 在这一行之后

SELECT count(*)+34 into countKab from m_kab