我在plpgsql中创建了一个函数。该函数有一个名为CURSOR_的游标,当我获取游标的值时,在循环内执行查询。我的查询使用where条件和光标数据。问题是,当函数执行查询时,变量 name_var 总是为空,尽管它随着从游标检索的值而改变。我做错了什么? 这是我的代码:
CREATE OR REPLACE FUNCTION fn_return_company_centers() RETURNS
TABLE(company text, centers text) AS
$BODY$
DECLARE CURSOR_ CURSOR FOR SELECT C.NAME, M.LOCATION || '__' || M.STORAGEKEY AS CENTER
FROM COMPANY C INNER JOIN MONITOR M ON C.ID = M.COMPANY_ID
ORDER BY C.NAME;
v_parent_rec RECORD;
name_var text :='';
_CENTROS text := '';
POS int := 0;
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS TMP_COMPANY_CENTER (company text, centers text);
DELETE FROM TMP_COMPANY_CENTER;
FOR v_parent_rec IN CURSOR_ LOOP
IF name_var = '' THEN
name_var := v_parent_rec.name;
SELECT A.CENTROS INTO _CENTROS
FROM SCHEDULER_REPORT A
WHERE A.EMPRESA = name_var ;
POS := position(v_parent_rec.center in _CENTROS);
RAISE NOTICE 'Es %', POS;
END LOOP;
RETURN QUERY SELECT * FROM TMP_COMPANY_CENTER ORDER BY COMPANY;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
事先提前,
丹尼尔。