niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Databases » PostgreSQL
Pgsql dynamic statements and null values
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
Guy Rouillier
*nix forums addict


Joined: 02 Mar 2005
Posts: 73

PostPosted: Wed Mar 09, 2005 6:13 pm    Post subject: Re: Pgsql dynamic statements and null values Reply with quote

Ragnar Hafstaš wrote:
Quote:
On Tue, 2005-03-08 at 16:30 -0600, Guy Rouillier wrote:
We use a dynamic statement in a pgsql stored function to insert rows
into a table determined at run time. After much debugging, I've
discovered that a null incoming argument will cause the dynamic
statement to evaluate to null. The error message emitted is "unable
to execute null statement."

can't you use COALESCE() ?

Thanks Ragnar and Martijn for the replies. NULLIF doesn't seem applicable here as I already have a null value coming in, so I'm not comparing it to anything. I had tried COALESCE before my original post and it produced the same result: cannot execute null statement. However, your prompting motivated me to try a couple more alternatives. Of the many I tried, here is one that works:

coalesce(quote_literal(inval), 'NULL')

Nice to know, but given the verbosity, I think I'll stick with my check_null(inval). A worthwhile exercise, though, since I can now reduce that function to this one line.

--
Guy Rouillier


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend
Back to top
Martijn van Oosterhout
*nix forums Guru


Joined: 02 Mar 2005
Posts: 674

PostPosted: Wed Mar 09, 2005 9:50 am    Post subject: Re: Pgsql dynamic statements and null values Reply with quote

On Tue, Mar 08, 2005 at 04:30:54PM -0600, Guy Rouillier wrote:
Quote:
This is such a common usage pattern that I'm pretty sure I'm missing
something basic. Pgsql provides quote_literal to aid with inserting a
literal string into a dynamically prepared statement. My opinion is
that quote_literal should handle nulls as well, but if quote_literal
can't be changed for historical reasons, then providing another function
like check_null below would be very useful. Basically, such a function
should supply the value NULL if the incoming value is null, or the
incoming value otherwise.

Lookup the COALESCE and NULLIF functions.

Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
Quote:
Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.
Back to top
Ragnar Hafstaš
*nix forums beginner


Joined: 01 Mar 2005
Posts: 37

PostPosted: Wed Mar 09, 2005 7:31 am    Post subject: Re: Pgsql dynamic statements and null values Reply with quote

On Tue, 2005-03-08 at 16:30 -0600, Guy Rouillier wrote:
Quote:
We use a dynamic statement in a pgsql stored function to insert rows
into a table determined at run time. After much debugging, I've
discovered that a null incoming argument will cause the dynamic
statement to evaluate to null. The error message emitted is "unable to
execute null statement."

can't you use COALESCE() ?

gnari



---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
Back to top
Guy Rouillier
*nix forums addict


Joined: 02 Mar 2005
Posts: 73

PostPosted: Tue Mar 08, 2005 9:30 pm    Post subject: Pgsql dynamic statements and null values Reply with quote

We use a dynamic statement in a pgsql stored function to insert rows
into a table determined at run time. After much debugging, I've
discovered that a null incoming argument will cause the dynamic
statement to evaluate to null. The error message emitted is "unable to
execute null statement." I provide a full example at the end of this
message. To see the problem, remove the check_null function from the
insert_t1 stored proc, and execute the proc like this: select
insert_t1(null);

This is such a common usage pattern that I'm pretty sure I'm missing
something basic. Pgsql provides quote_literal to aid with inserting a
literal string into a dynamically prepared statement. My opinion is
that quote_literal should handle nulls as well, but if quote_literal
can't be changed for historical reasons, then providing another function
like check_null below would be very useful. Basically, such a function
should supply the value NULL if the incoming value is null, or the
incoming value otherwise.

CREATE TABLE T1
(
f1 smallint
);

CREATE OR REPLACE FUNCTION check_null
(
inval anyelement
) RETURNS varchar AS $$

DECLARE

retval varchar := 'NULL';

BEGIN

if inval is not null then
retval := quote_literal(inval);
end if;
return retval;

END;
$$ LANGUAGE plpgsql;



CREATE OR REPLACE FUNCTION insert_t1
(
inval integer
) RETURNS VOID AS $$

DECLARE
sqlstmt varchar;

BEGIN

sqlstmt := ' INSERT INTO T1 ' ||
' ( ' ||
' F1 ' ||
' ) ' ||
' VALUES ' ||
' ( ' ||
check_null(inval) ||
' ) ' ;

execute sqlstmt;
return;
END;
$$ LANGUAGE plpgsql;

--
Guy Rouillier


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 7:59 am | All times are GMT
navigation Forum index » Databases » PostgreSQL
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Dynamic IP Issues, when Sever is on Fixed. spode Postfix 2 Tue Aug 14, 2007 2:10 pm
No new posts does the default constructor initialize values? NewToCPP C++ 12 Thu Jul 20, 2006 3:37 pm
No new posts converting array values to monomaniac21 PHP 11 Thu Jul 20, 2006 10:17 am
No new posts "CHARACTER SET COLLATE NULL" error with mySQL 4.0.27 thomas Armstrong MySQL 1 Thu Jul 20, 2006 9:11 am
No new posts Messages to pgsql-general list not being posted Kevin Murphy PostgreSQL 4 Wed Jul 19, 2006 1:50 pm

Personal Loans | Myspace Comments | Loans | Credit Cards | Credit Cards
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2519s ][ Queries: 20 (0.1367s) ][ GZIP on - Debug on ]