Re: How to avoid nulls while writing string for dynamic query

From: "Kumar" <sgnerd(at)yahoo(dot)com(dot)sg>
To: "Tomasz Myrta" <jasiek(at)klaster(dot)net>
Cc: "psql" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: How to avoid nulls while writing string for dynamic query
Date: 2004-02-13 04:53:58
Message-ID: 006701c3f1ed$6610dc30$7502a8c0@hdsc.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I am having problem there. see what happens

sqlstr := 'insert into test(c1, c2) values ('||COALESCE(rec.c1,'NULL')||','
||'\''||rec.c2||'\')';

WARNING: Error occurred while executing PL/pgSQL function test_fn
WARNING: line 8 at assignment
ERROR: pg_atoi: error in "NULL": can't parse "NULL"

Dont forgot that c1 is int.

when i have like this
sqlstr := 'insert into test(c1, c2) values ('||COALESCE(rec.c1,NULL)||','
||'\''||rec.c2||'\')';

NOTICE: <NULL>
WARNING: Error occurred while executing PL/pgSQL function test_fn
WARNING: line 11 at execute statement
ERROR: cannot EXECUTE NULL query

That is the problem i am facing. Please shed some light.

Thanks
Kumar

----- Original Message -----
From: "Tomasz Myrta" <jasiek(at)klaster(dot)net>
To: "Kumar" <sgnerd(at)yahoo(dot)com(dot)sg>
Cc: "psql" <pgsql-sql(at)postgresql(dot)org>
Sent: Thursday, February 12, 2004 6:13 PM
Subject: Re: [SQL] How to avoid nulls while writing string for dynamic query

> Dnia 2004-02-12 13:31, Użytkownik Kumar napisał:
> > The error is because of no value for column c1. If the column c1 is a
> > string I might have replace it with empty string. I don't want to
> > substitute with '0' which could work.
> >
> > sqlstr := 'insert into test(c1, c2) values (' ||ISNULL(rec.c1,'0')||','
> >
> > ||'\''||rec.c2||'\')';
>
> Substitute it with NULL value:
> sqlstr := 'insert into test(c1, c2) values(' || coalesce(rec.c1,'NULL')
> ||...
>
> Regards,
> Tomasz Myrta

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2004-02-13 05:38:22 Re: Index question
Previous Message Tom Lane 2004-02-13 04:49:56 Re: 7.4 - FK constraint performance