PostgreSQL cursor example (create a cursor and traverse it in the function)

Source: Internet
Author: User
-- Drop function top100cur (refcursor); Create Function top100cur (refcursor) returns refcursor as $ beginopen $1 for select * From person limit 100; return $1; end $ language plpgsql; ---------- test cursor ----------- select top100cur ('abc'); -- fetch all from ABC; -- drop function from2cur (refcursor, Int, INT ); -- This is a function that returns records within a certain range in the cursor -- create function from2cur (refcursor, Int, INT) returns setof text as $ declare -- declare some subscript variables PNAM text; PNO text; index int; lower int; upper int; beginindex: = 1; lower: = $2; upper: = $3; fetch $1 into PNAM, PNO; -- one item must be fetch first; otherwise, the found is falsewhile found loop -- only in [lower, upper] interval records are returned -- if lower <= index and upper> = index thenreturn next PNAM | PNO; end if; fetch $1 into PNAM, PNO; index: = index + 1; -- If index> upper then return; end if; end loop; end $ language plpgsql; select top100cur ('abc '); -- create a cursor named ABC -- fetch all in ABC; -- test the cursor select * From from2cur ('abc', 2, 5 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.