Re: How to execute cursor in PostgreSQL? - Mailing list pgsql-admin

From David G Johnston
Subject Re: How to execute cursor in PostgreSQL?
Date
Msg-id 1425012028153-5839672.post@n5.nabble.com
Whole thread Raw
In response to How to execute cursor in PostgreSQL?  (Vinaya Torne <v.b.torne@gmail.com>)
List pgsql-admin
Vinaya Torne wrote
> Hi Admin
>
> Below  is my program for cursor, but I am not getting how to execute the
> cursor on a database.
>
> Please suggest on this
>
> emp=# CREATE or replace function cursor_demo() returns integer as $$
> declare
> emp_rec employee%rowtype;
> emp cursor for select * from employee;
> comm numeric(6,2);
> begin
> loop
> fetch emp into emp_rec;
> if emp_rec.deptno = 5 then
> comm:=emp_rec.salary*0.2;
> else if emp_rec.deptno=8 then
> comm:= emp_rec.salary*0.5;
> else if emp_rec.deptno = 10 then
> comm:=emp_rec.salary*0.3;
> end if;
> end if;
> end if;
> raise notice 'emp_rec.ename||emp_rec.deptno||emp_rec.salary||comm.';
> exit when not found;
> end loop;
> close emp;
> end;
> $$ language'plpgsql';

I don't understand the question/problem.

I'm not seeing how this is an -admin concern; its better directed to
-general.

I'll assume the above is meant for learning because that is really bad form
if you intend it to be a solution to a problem.

pl/pgsql allows you to loop without making use of actual cursors.

I've never really made use of cursors...

You execute a function by doing: "SELECT cursor_demo();" > which will toss
an error because you have no "RETURN int" statement...there may be other
syntax issues with what you have written as well.

You should read much of the documentation; but as relates to this question:

http://www.postgresql.org/docs/9.4/interactive/plpgsql.html
and specifically
http://www.postgresql.org/docs/9.4/interactive/plpgsql-cursors.html

if you do end up convinced that a pl/pgsql cursor is a proper solution to
your, unstated, problem.

David J.




--
View this message in context: http://postgresql.nabble.com/How-to-execute-cursor-in-PostgreSQL-tp5839670p5839672.html
Sent from the PostgreSQL - admin mailing list archive at Nabble.com.


pgsql-admin by date:

Previous
From: Vinaya Torne
Date:
Subject: How to execute cursor in PostgreSQL?
Next
From: Albe Laurenz
Date:
Subject: Re: Security with V9.3.3 standby servers