Обсуждение: How to execute cursor in PostgreSQL?

Поиск
Список
Период
Сортировка

How to execute cursor in PostgreSQL?

От
Vinaya Torne
Дата:
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';

Thanks&Regards,
Vinaya Torne
 +91-9403150377

Re: How to execute cursor in PostgreSQL?

От
David G Johnston
Дата:
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.