X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id CF574D1DA5F for ; Thu, 12 Feb 2004 12:30:49 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 74822-08 for ; Thu, 12 Feb 2004 08:30:50 -0400 (AST) Received: from smtp105.mail.sc5.yahoo.com (smtp105.mail.sc5.yahoo.com [66.163.169.225]) by svr1.postgresql.org (Postfix) with SMTP id F1E2CD1DA57 for ; Thu, 12 Feb 2004 08:30:46 -0400 (AST) Received: from unknown (HELO IVESIA67) (sgnerd@203.196.190.138 with login) by smtp105.mail.sc5.yahoo.com with SMTP; 12 Feb 2004 12:30:48 -0000 Message-ID: <00c001c3f164$1db30140$7502a8c0@hdsc.com> Reply-To: "Kumar" From: "Kumar" To: "psql" Subject: How to avoid nulls while writing string for dynamic query Date: Thu, 12 Feb 2004 18:01:12 +0530 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_00BD_01C3F192.337AC930" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/111 X-Sequence-Number: 16802 This is a multi-part message in MIME format. ------=_NextPart_000_00BD_01C3F192.337AC930 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Dear Friends, Postgres 7.3.4 on RH Linux 7.2. I wanted to write a dynamic query for insert statement. create table test(c1 int, c2 varchar) insert into test(c1, c2) values (1,'Hai1'); insert into test(c1, c2) values (NULL,'Hai2'); so I wrote a function called test_fn() DECLARE sqlstr VARCHAR(100); rec RECORD; BEGIN FOR rec IN SELECT * FROM test LOOP sqlstr :=3D 'insert into test(c1, c2) values (' ||rec.c1||',' ||'\''|= |rec.c2||'\')'; RAISE NOTICE '%',sqlstr; execute sqlstr; END LOOP; RETURN 'DONE'; END; NOTICE: insert into test(c1, c2) values (1,'Hai1') NOTICE: So i have created a null function. sqlstr :=3D 'insert into test(c1, c2) values (' ||ISNULL(rec.c1,'')||',' ||'\''|= |rec.c2||'\')'; Now I got results as NOTICE: insert into test(c1, c2) values (1,'Hai1') NOTICE: insert into test(c1, c2) values (,'Hai2') WARNING: Error occurred while executing PL/pgSQL function test_fn WARNING: line 11 at execute statement ERROR: parser: parse error at or near "," at character 34 The error is because of no value for column c1. If the column c1 is a strin= g I might have replace it with empty string. I don't want to substitute wit= h '0' which could work. sqlstr :=3D 'insert into test(c1, c2) values (' ||ISNULL(rec.c1,'0')||',' ||'\''|= |rec.c2||'\')'; NOTICE: insert into test(c1, c2) values (1,'Hai1') NOTICE: insert into test(c1, c2) values (0,'Hai2') Total query runtime: 47 ms. Data retrieval runtime: 0 ms. 1 rows retrieved. How can I do that. Please advise me. Thanks Kumar ------=_NextPart_000_00BD_01C3F192.337AC930 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Dear Friends,
 
Postgres 7.3.4 on RH Linux 7.2.
 
I wanted to write a dynamic query for insert=20 statement.
 
create table test(c1 int, c2 varchar)
 
insert into test(c1, c2) values (1,'Hai1');
insert i= nto=20 test(c1, c2) values (NULL,'Hai2');
 
so I wrote a function called test_fn()
 
DECLARE
   =20 sqlstr  VARCHAR(100);
    =20 rec  RECORD;
BEGIN
     FOR rec=20 IN SELECT * FROM test
   =20  LOOP
          sqlstr = ;:=3D=20 'insert into test(c1, c2) values=20 (' ||rec.c1||','
        &n= bsp;  =20                = =20                = =20                = =20        =20 ||'\''||rec.c2||'\')';
    RAISE NOTICE=20 '%',sqlstr;
     execute=20 sqlstr;
    END LOOP;
RETURN 'DONE';
END;
 
NOTICE:  insert into test(c1, c2) values=20 (1,'Hai1')
NOTICE:  <NULL>
 
So i have created a null function.
 
  sqlstr :=3D 'insert into test(c1, c2) = values=20 (' ||ISNULL(rec.c1,'')||','
      &nb= sp;    =20                = =20                = =20                = =20         ||'\''||rec.c2||'\')';

Now I got results as
NOTICE:  insert into test(c1, c2) values=20 (1,'Hai1')
NOTICE:  insert into test(c1, c2) values=20 (,'Hai2')
WARNING:  Error occurred while executing PL/pgSQL functio= n=20 test_fn
WARNING:  line 11 at execute statement
 
ERROR:  parser: parse error at or near "," at char= acter=20 34
 
The error is because of no value for column c1. If the = column=20 c1 is a string I might have replace it with empty string. I don't want to= =20 substitute with '0' which could work.
 
sqlstr :=3D 'insert into test(c1, c2) values=20 (' ||ISNULL(rec.c1,'0')||','
      &n= bsp;    =20                = =20                = =20                = =20         ||'\''||rec.c2||'\')';
 
NOTICE:  insert into test(c1, c2) values=20 (1,'Hai1')
NOTICE:  insert into test(c1, c2) values=20 (0,'Hai2')
 
Total query runtime: 47 ms.
Data retrieval runtime: = 0=20 ms.
1 rows retrieved.
 
How can I do that. Please advise me.
 
Thanks
Kumar
 
 

------=_NextPart_000_00BD_01C3F192.337AC930--