potgresql-WITH RECURSIVE与refcursor

//potgresql7.8以上支持的递归 WITH RECURSIVE t(n) AS ( SELECT 1 UNION ALL SELECT n+1 FROM t ) SELECT n FROM t LIMIT 100; //参考:http://www.postgresql.org/docs/8.4/static/queries-with.html //创建plpgsql过程语言 create LANGUAGE 'plpgsql' //操作多条数据集,游标(refcursor) CREATE OR REPLACE FUNCTION ref_test(ref1 refcursor, i integer) RETURNS SETOF refcursor AS $BODY$ begin open $1 for WITH RECURSIVE t(n) AS ( SELECT i UNION ALL SELECT n+i FROM t ) SELECT n FROM t LIMIT 100; return next $1; end;$BODY$ LANGUAGE 'plpgsql' VOLATILE //或者不用返回多个结果集 CREATE OR REPLACE FUNCTION "ref_Test"( i integer) RETURNS refcursor AS $BODY$ DECLARE ref refcursor; BEGIN OPEN ref FOR WITH RECURSIVE t(n) AS ( SELECT i UNION ALL SELECT n+i FROM t ) SELECT n FROM t LIMIT 100; RETURN ref; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE //调用 select ref_test('a',1);FETCH ALL FROM a; 

你可能感兴趣的:(potgresql-WITH RECURSIVE与refcursor)