--drop function top100cur(refcursor);create function top100cur(refcursor) returns refcursor as $$beginopen $1 for select * from person limit 100;return $1;end$$language plpgsql;----------測試遊標----------- SELECT top100cur('abc');-- fetch all from abc;-- drop function from2cur(refcursor,int,int);--這是一個返回遊標中在一定範圍內記錄的函數--create function from2cur(refcursor,int,int)returns setof text as $$declare--聲明一些下標變數pnam text;pno text;index int;lower int;upper int;beginindex:=1;lower:=$2;upper:=$3;fetch $1 into pnam,pno;--必須先fetch一條,否則found為falsewhile found loop--只在[lower,upper]區間的記錄才返回--if lower<=index and upper>=index thenreturn next pnam||pno;end if;fetch $1 into pnam,pno;index:=index+1;--超過upper後,函數返回--if index>upper then return;end if;end loop;end$$language plpgsql;select top100cur('abc');--建立一個名字為abc的遊標-- fetch all in abc;--測試遊標select * from from2cur('abc',2,5);