postgresql 函数之游标、更新数据
根据人员信息更新另一个表中的部门信息
CREATE OR REPLACE FUNCTION "public"."updatedeptcode"()
RETURNS "pg_catalog"."varchar" AS $BODY$
DECLARE
unbound_refcursor refcursor; --游标
deptCode VARCHAR(100);
deptName VARCHAR(100);
persPin VARCHAR(100);
deptId VARCHAR(100);
BEGIN
open unbound_refcursor for execute 'select a.pin, b.code, b.name, b.id from 表明 a LEFT JOIN 表名 b on a.auth_dept_id = b.id'; --打开游标 并注入要搜索的字段的记录
loop --开始循环
fetch unbound_refcursor into persPin, deptCode, deptName, deptId; --将游标指定的值赋值给变量
if deptCode is not null then --任意的逻辑
update 表名 set auth_dept_id= deptId, auth_dept_code= deptCode, auth_dept_name= deptName where pers_person_pin= persPin;
else
exit;
end if;
end loop; --结束循环
close unbound_refcursor; --关闭游标
raise notice 'the end of msg...'; --打印消息
RETURN 'true';
END$BODY$
LANGUAGE plpgsql VOLATILE
COST 100