Quantcast
Channel: Planet PostgreSQL
Viewing all articles
Browse latest Browse all 9968

Andrew Dunstan: quote_literal vs NULL

$
0
0
I had a client caught by this today, and I've been caught by it a few times myself. If you construct dynamic SQL for use in PLPGSQL then you will usually want to use quote_literal() to quote the value properly in the SQL. But quote_literal() is a STRICT function, so if its argument is NULL then so is its result. This can make your whole dynamic SQL string NULL, resulting in your getting errors like:
ERROR:  query string argument of EXECUTE is null

The way I get around the problem is to wrap the call to quote_literal() in a call to coalesce() if the value being quoted might be NULL, like this:
coalesce(quote_literal(your_variable),'NULL')



Viewing all articles
Browse latest Browse all 9968

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>