首页 > 解决方案 > 我必须在 Postgres plpgsql 过程的每次迭代中将变量重置为 null 吗?

问题描述

我有这个简单的 plpgsql 语言的 postgres 过程(示例)。在调用 select into 之前我必须执行“_current_id = null”吗?or _current_id always will be null when select not found any matching row?. 我知道我可以测试,但我不想将来因为不好的做法而受苦。但也不想添加不必要的额外代码行

BEGIN
    DECLARE
     _current_id integer;
     _row record;
    BEGIN
     FOR _row IN select * from my_table LOOP
        select id into _current_id from my_second_table where name=row.name;
         if (_current_id is null) then
           --my code here
         end if;
     END LOOP;
END;
    

标签: postgresqlplpgsql

解决方案


推荐阅读