CREATE OR REPLACE FUNCTION _ccgc_mutil_table(refcursor, refcursor)
RETURNS SETOF refcursor AS
$BODY$
DECLARE
r record;
ids text :='';
sql text;
BEGIN
OPEN $1 FOR SELECT * FROM "Warning";
RETURN NEXT $1;
FOR r in (select * from "Warning") loop
ids := ids || ',' || quote_literal(r."MonitorID");
END loop;
ids := substr(ids,2,length(ids)-1);
sql := 'SELECT * FROM "MonitorPoint" where "MonitorID" IN ('||ids||')';
--OPEN $2 FOR SELECT * FROM "MonitorPoint" where "MonitorID" IN (ids);
OPEN $2 FOR execute sql;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
问题一:如何遍历游标,即遍历$1 中的record, 上面使用的是: FOR r in (select * from "Warning") loop;
问题二:如下代码为什么无法执行,而此句 OPEN $2 FOR execute sql; 却执行正常
--OPEN $2 FOR SELECT * FROM "MonitorPoint" where "MonitorID" IN (ids);
2 个解决方案
#1
似乎此版块搞 postgreSQL 的很少呀,顶下
#2
纠结了几天的问题还是没搞定,暂时使用 "for r in sql loop end loop;" 解决
#1
似乎此版块搞 postgreSQL 的很少呀,顶下
#2
纠结了几天的问题还是没搞定,暂时使用 "for r in sql loop end loop;" 解决