Unix Technical Forum

LOOping clobbering variable

This is a discussion on LOOping clobbering variable within the pgsql Novice forums, part of the PostgreSQL category; --> Hi folks, The enclosed function returns a proper result only when the LOOP and its enclosed functionality is removed ...


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

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 08:18 PM
K Anderson
 
Posts: n/a
Default LOOping clobbering variable

Hi folks,

The enclosed function returns a proper result only when the LOOP and its enclosed functionality is removed from the function. Notice near the return I have xml=xml||'</transaction>';

That should at the least return what ever is in xml with </transaction> concatinated, or so I would hope. But it's empty.

I am using postgresql 7.4.6 on FreeBSD.

Any assistance in correcting this would be greatly appreciated.

TIA

CREATE OR REPLACE FUNCTION public.fetch_order(int8)
RETURNS text AS
'Declare
transcode ALIAS for $1;
data RECORD;
tmp_data RECORD;
tmp_data2 RECORD;
xml TEXT;
tmp_xml TEXT;
tmp_item_name TEXT;
Begin
xml=\'<transaction>\';
select * into data from orders where id=transcode::int8;
IF data.type = \'so\' THEN
select * into tmp_data from customers where id = data.cus_ven_id;
xml=xml||\'<customer>\';
xml=xml||\'<name>\'||tmp_data.customer||\'</name>\';
xml=xml||\'<address>\'||tmp_data.address||\'</address>\';
xml=xml||\'<phone_number>\'||tmp_data.phone_number ||\'</phone_number>\';
xml=xml||\'<fax_number>\'||tmp_data.fax_number||\' </fax_number>\';
xml=xml||\'<e_mail>\'||tmp_data.e_mail||\'</e_mail>\';
xml=xml||\'</customer>\';
END IF;
FOR tmp_data IN SELECT * FROM order_line_item where order_number=transcode::int8 LOOP
select * into tmp_data2 from items where id=tmp_data.item_id;
xml=xml||\'<item>\'||tmp_data2.item;
xml=xml||\'<decription>\'||tmp_data2.description|| \'</description>\';
xml=xml||\'<qty>\'||tmp_data.quantity||\'</qty>\';
xml=xml||\'<cost>\'||tmp_data.cost||\'</coast>\';
xml=xml||\'</item>\';
END LOOP;
xml=xml||\'</transaction>\';
return xml;
End;'
LANGUAGE 'plpgsql' VOLATILE;

--
_______________________________________________
Get your free email from http://www.kittymail.com

Powered by Outblaze

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 08:18 PM
Michael Fuhr
 
Posts: n/a
Default Re: LOOping clobbering variable

On Sat, Jan 01, 2005 at 07:38:16PM -0800, K Anderson wrote:

> The enclosed function returns a proper result only when the LOOP
> and its enclosed functionality is removed from the function. Notice
> near the return I have xml=xml||'</transaction>';
>
> That should at the least return what ever is in xml with
> </transaction> concatinated, or so I would hope. But it's empty.


I'd guess that one of the values you're concatenating is NULL.
Concatenating NULL with anything results in NULL, so if you assign
that back to xml then all subsequent concatenations will also result
in NULL.

You can use COALESCE to convert NULLs to empty strings.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 08:18 PM
Stephan Szabo
 
Posts: n/a
Default Re: LOOping clobbering variable

On Sat, 1 Jan 2005, K Anderson wrote:

> The enclosed function returns a proper result only when the LOOP and its
> enclosed functionality is removed from the function. Notice near the
> return I have xml=xml||'</transaction>';
>
> That should at the least return what ever is in xml with </transaction>
> concatinated, or so I would hope. But it's empty.


Is it possible that you're ending up with a NULL after the loop? NULL
concatenated with anything is still NULL. If any of the various fields
you're concatenating in could be NULL you'll need to guard against that.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

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 11:11 AM.


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