Re: EXECUTE INSERT BUGS?

From: "Talha Khan" <talha(dot)amjad(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: EXECUTE INSERT BUGS?
Date: 2006-11-07 01:12:37
Message-ID: f80885fc0611061712l72d4a34bg71040fc5812d42f1@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi Mathew,

whats happening here in your case is that when you use the concatenation
operator || and an element in your command is NULL the whole concat chain
ends up being NULL so the execute command runs as

EXECUTE NULL

what you can do is to write your command as such:

cmd := 'INSERT INTO test (
col
) values ( '
||coalesce( quote_literal(v_value),'NULL') || ');';

Now your command will look like

EXECUTE 'insert into test (col) values( NULL);

making NULL a string instead of a value.

Regards
Talha Khan

On 11/7/06, Matthew Peter <survivedsushi(at)yahoo(dot)com> wrote:
>
>
> create table test (col text);
> create or replace function tester() RETURNS void AS $$
> DECLARE
> cmd text;
> v_value text := null;
> -- ^^^ right here, NULL makes the querystring fail by
> setting cmd =
> null
> BEGIN
> cmd := 'INSERT INTO test (
> col
> ) values ( '
> || quote_literal(v_value) || ');';
> EXECUTE cmd;
>
> END;
> $$ LANGUAGE plpgsql;
>
>
> test=# \i /tmp/test
> CREATE TABLE
> CREATE FUNCTION
> test=# select * from tester();
> ERROR: cannot EXECUTE a null querystring
> CONTEXT: PL/pgSQL function "tester" line 12 at execute statement
>
>
> Also, if v_value is set to boolean then quote_literal(v_value) throws
> error
>
>
>
>
>
> ____________________________________________________________________________________
> Cheap talk?
> Check out Yahoo! Messenger's low PC-to-Phone call rates.
> http://voice.yahoo.com
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: explain analyze is your friend
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Premsun Choltanwanich 2006-11-07 01:40:54 Re: Header meaning for pg_dump
Previous Message Jeff Davis 2006-11-07 00:58:47 Re: EXECUTE INSERT BUGS?