473,395 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

plpgsql-fct. fails on NULL in record variables

Hi all,

I'm currently coding some functions in plpgsql for generating
reports out of records in a table. Problem is: NULL values in
records make the complete function fail.
Here is a simple test case (original is more complex with a multi-
table query in the for qres in select... part):

create table test(
id serial,
descr char(4),
data int
);

insert into test (descr, data) values ('set1', 15);
-- record 2 does not have a data value --
insert into test (descr) values ('set2');

create function report(int) returns text as '
declare
qres record;
report text;
begin
for qres in
select descr, data from test where id=$1
loop
report:=qres.descr||'': ''||qres.data;
end loop;
return report;
end;'
language 'plpgsql';

now test the function in psql:
test=> select report(1);
report
----------
set1: 15
(1 row)

test=> select report(2);
report
--------

(1 row)

while what I want it to return in the second case is this:
report
--------
set2:
(1 row)

'set2: NULL' would be ok as well.

How can I achieve this?

Regards,
Daniel

---------------------------(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

Nov 23 '05 #1
1 2245
concatenating a NULL value to a string will return NULL.

SELECT NULL||'Stringggg';
?column?
----------

(1 row)

There's a fonction called coalesce that replaces NULL values with a
specified value it can be usefull:

SELECT coalesce(NULL,'') || ' Stringgggg';
?column?
-------------
Stringgggg
(1 row)

SELECT coalesce('A','') || ' Stringgggg';
?column?
--------------
A Stringgggg
(1 row)

HTH
Najib.


----- Original Message -----
From: "Daniel Martini" <dm******@uni-hohenheim.de>
To: <pg***********@postgresql.org>
Sent: Friday, September 24, 2004 11:33 AM
Subject: [GENERAL] plpgsql-fct. fails on NULL in record variables

Hi all,

I'm currently coding some functions in plpgsql for generating
reports out of records in a table. Problem is: NULL values in
records make the complete function fail.
Here is a simple test case (original is more complex with a multi-
table query in the for qres in select... part):

create table test(
id serial,
descr char(4),
data int
);

insert into test (descr, data) values ('set1', 15);
-- record 2 does not have a data value --
insert into test (descr) values ('set2');

create function report(int) returns text as '
declare
qres record;
report text;
begin
for qres in
select descr, data from test where id=$1
loop
report:=qres.descr||'': ''||qres.data;
end loop;
return report;
end;'
language 'plpgsql';

now test the function in psql:
test=> select report(1);
report
----------
set1: 15
(1 row)

test=> select report(2);
report
--------

(1 row)

while what I want it to return in the second case is this:
report
--------
set2:
(1 row)

'set2: NULL' would be ok as well.

How can I achieve this?

Regards,
Daniel

---------------------------(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


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Mikulec | last post by:
Hi there, I wonder if anyone can shed some light on a very frustrating problem. I'm running a debian linux 3.0 "woody" server, nothing special, with the latest version of postres that apt-get...
1
by: Amin Schoeib | last post by:
Hi, When I Try to write Functions I always become the error That the language plpgsql doesn't exist. I tried it this way: createlang plpgsql template1 createlang -l template1 Procedural...
5
by: Thomas LeBlanc | last post by:
I copied an example from the help: CREATE FUNCTION somefunc() RETURNS integer AS ' DECLARE quantity integer := 30; BEGIN RAISE NOTICE ''Quantity here is %'', quantity; -- Quantity here is 30...
6
by: Martin Marques | last post by:
We are trying to make some things work with plpgsql. The problem is that I built several functions that call one another, and I thought that the way of calling it was just making the assign: ...
1
by: Rajesh Kumar Mallah | last post by:
Hi, profile_row profile_master%ROWTYPE; in a plpgsql function gives the error below tradein_clients=# SELECT general.create_accounts(); WARNING: plpgsql: ERROR during compile of...
0
by: Steve Wampler | last post by:
Hmmm, I've always used plpgsql.so (also formerly known as libplpgsql.so, I think...) as in: CREATE FUNCTION plpgsql_call_handler () RETURNS language_handler AS '/usr/lib/plpgsql.so',...
10
by: lnd | last post by:
After copied pg database from one PC to another -I could not find plpgsql function(s) in the copied database. -had to instal plpgsql language handler again -whilst tables and data moved...
14
by: Karl O. Pinc | last post by:
Hi, Thought perhaps some other eyes than mine can tell if I'm doing something wrong here or if there's a bug somewhere. I've never passed a ROWTYPE varaible to a function but I don't see where...
2
by: Mark Cave-Ayland | last post by:
Hi everyone, I'm trying to write a recursive plpgsql function in PostgreSQL 7.4.2 that given a tree node id (ictid) will return all the nodes below it in the tree, one row per node. When I try...
0
by: sripathy sena | last post by:
Hi, I am trying to install OPenacs with postgres 7.4.3 as the database. The openacs requires plpgsql to be installed. When I try to do this by running "CREATELANG plpgsql template1". I get a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.