Unix Technical Forum

select sum within transaction problem

This is a discussion on select sum within transaction problem within the pgsql Sql forums, part of the PostgreSQL category; --> Dear all I have a function like below (simplified). Everything works the way I want it to except for ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 05:53 PM
Bart Degryse
 
Posts: n/a
Default select sum within transaction problem

Dear all
I have a function like below (simplified). Everything works the way I want it to except for one thing.
After the select statement sum_revenues is NULL instead of the real sum.
Has this something to do with the earlier deletes and inserts not being committed yet?
I assumed they would have been committed at the END marked with (1), but apparently they don't.
How can I fix this?
Thanks to anyone who can enlighten me!

CREATE FUNCTION test(companies IN text[]) returns void AS
$body$
DECLARE
company text;
sum_revenues revenues.revenue%TYPE;
BEGIN
perform general_log('test', 'start');
for idx in array_lower(companies, 1) .. array_upper(companies, 1)
loop
BEGIN
BEGIN
company := lower(companies[idx]);
delete from revenues where ...;
insert into revenues select ..., ..., ... from billing where ....condition1...;
insert into revenues select ..., ..., ... from billing where ....condition2...;
insert into revenues select ..., ..., ... from billing where ....condition3...;
insert into revenues select ..., ..., ... from billing where ....condition4...;
END; --(1)
select sum(revenue) into sum_revenues from revenues;
EXCEPTION
WHEN others
perform general_errlog('test', SQLSTATE, SQLERRM);
END;
end loop;
perform general_log('test', 'end');
EXCEPTION
WHEN others
perform general_errlog('test', SQLSTATE, SQLERRM);
END;
$body$
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 05:54 PM
Tom Lane
 
Posts: n/a
Default Re: select sum within transaction problem

"Bart Degryse" <Bart.Degryse@indicator.be> writes:
> I have a function like below (simplified). Everything works the way I want =
> it to except for one thing.
> After the select statement sum_revenues is NULL instead of the real sum.
> Has this something to do with the earlier deletes and inserts not being =
> committed yet?


No. I suspect the problem is hidden somewhere in the code you didn't
show us. In particular you should check whether the insert/selects
are actually finding anything to insert ...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:47 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com