Connecting Tech Pros Worldwide Forums | Help | Site Map

Writing a user defined function

Newbie
 
Join Date: May 2007
Posts: 3
#1: Jul 18 '08
Hello,
I am trying to code a simple udf in postgres. How do I write sql commands into pl/sql ? The foll. code doesnt work.

CREATE OR REPLACE FUNCTION udf()
RETURNS integer AS $$
BEGIN
for i in 1..2000 loop
for j in 1...10000 loop
end loop;
begin work;
declare cust scroll cursor for select * from tpcd.customer;
FETCH FORWARD 5 FROM cust;
end loop;
CLOSE cust;
COMMIT work;
return 1;
end;
$$ LANGUAGE plpgsql;

select udf();

Moderator
 
Join Date: Dec 2006
Location: Europe
Posts: 293
#2: Jul 19 '08

re: Writing a user defined function


I do not use latest posgres release but postgres 8.1 and earlier do not support nested transactions. Running a function postgres creates a transaction so you can't write 'begin work' and 'commit work' in function body (cos doing it you try to write nested transaction). is that clear?
Reply