Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6976637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:31:22+00:00 2026-05-27T17:31:22+00:00

I am trying to learn basic cursors using PostgreSQL. This is my script: DECLARE

  • 0

I am trying to learn basic cursors using PostgreSQL. This is my script:

DECLARE cur_employees CURSOR FOR
  SELECT *
  FROM   employee

CLOSE cur_employees 

I want to traverse the list and output those which are active. Where should I start?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:31:22+00:00Added an answer on May 27, 2026 at 5:31 pm

    You very rarely want to explicitly use cursors at all in PostgreSQL, even when processing query results in plpgsql. This is a marked contrast from many other SQL databases where they are used almost all the time.

    In plpgsql you could simply write something like:

    DECLARE
      emp employee%rowtype;
    BEGIN
      FOR emp IN SELECT * FROM employee LOOP
        IF emp.active THEN
          RAISE INFO 'Active: %', emp.employee_id
        END IF;
      END LOOP;
    END
    

    In the above, the plpgsql language handler deals with opening, binding, fetching and closing itself (more on declarations, and control structures).

    With PostgreSQL from 9.0, you can simply drop into executing plpgsql using a “DO” block. For prior versions, you need to create a function and select it. If you’re looking for the PostgreSQL equivalent of, for example, iterating through a result with a cursor on SQL Server, that’s what it is. Note that iteration etc. are not part of the SQL dialect, only part of plpgsql (or any of the other emebedded languages).

    The “DECLARE CURSOR xxx” syntax at the SQL level can be used like this:

    DECLARE cur_employees CURSOR FOR SELECT * FROM employee;
    FETCH NEXT FROM cur_employees;
    // etc..
    CLOSE cur_employees;
    

    This can be used to carefully get just part of the query result set. However, it’s unusual to use these, since usually your client driver will provide some sort of functionality to do this (e.g. scrollable result sets in JDBC). You can also return cursors from functions similar to Oracle, although again that’s a comparatively rare use case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn how to do XPath queries from Python using this sample
I'm trying to learn this basic thing about processors that should be taught in
I know this is a basic PHP question, and I'm trying to learn the
Sorry if this is basic, I am trying to learn as much as I
I am trying to learn the basic of 3d for Mac using Xcode, so
I am trying to learn to make a basic game using pygame. I want
I am trying to learn the kernel density estimation from the basic. Anyone have
I'm trying to learn some basic ajax using Django. My simple project is an
Im trying to learn javascript and, having some basic programming experience already (from C++),
I am trying to learn some of the basic and advanced features of visual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.