Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 4C8511337B69 for ; Thu, 27 Jan 2011 10:16:04 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 02318-06 for ; Thu, 27 Jan 2011 14:15:56 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from compulab.co.il (compulab.co.il [67.18.134.219]) by mail.postgresql.org (Postfix) with ESMTP id 5F0231337990 for ; Thu, 27 Jan 2011 10:15:55 -0400 (AST) Received: from [62.90.235.247] (helo=zimbra-mta.compulab.co.il) by compulab.site5.com with esmtp (Exim 4.69) (envelope-from ) id 1PiSde-0004H4-AW for pgsql-general@postgresql.org; Thu, 27 Jan 2011 08:15:55 -0600 Received: from localhost (localhost.localdomain [127.0.0.1]) by zimbra-mta.compulab.co.il (Postfix) with ESMTP id 1A3F17E91EA for ; Thu, 27 Jan 2011 16:15:51 +0200 (IST) X-Virus-Scanned: amavisd-new at compulab.co.il Received: from zimbra-mta.compulab.co.il ([127.0.0.1]) by localhost (zimbra-mta.compulab.co.il [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id vDx-R9qRGqqT for ; Thu, 27 Jan 2011 16:15:49 +0200 (IST) Received: from [10.1.1.27] (sim-pc.compulab.local [10.1.1.27]) by zimbra-mta.compulab.co.il (Postfix) with ESMTP id AD8097E8E4D for ; Thu, 27 Jan 2011 16:15:49 +0200 (IST) Message-ID: <4D417E09.4080201@compulab.co.il> Date: Thu, 27 Jan 2011 16:15:37 +0200 From: Sim Zacks User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.14pre) Gecko/20110118 Lightning/1.0b2 Shredder/3.1.8pre MIME-Version: 1.0 To: PostgreSQL general Subject: Re: Queyring for columns which are exist in table. References: <4D414F41.1020804@compulab.co.il> In-Reply-To: Content-Type: multipart/alternative; boundary="------------060304060405090704060309" X-ACL-Warn: { X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - compulab.site5.com X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - compulab.co.il X-Source: X-Source-Args: X-Source-Dir: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-5 required=5 tests=BAYES_00=-1.9, HTML_MESSAGE=0.001, T_FILL_THIS_FORM_SHORT=0.01, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201101/778 X-Sequence-Number: 172137 This is a multi-part message in MIME format. --------------060304060405090704060309 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit What you will need to do in your function is ... sql = 'select ' || checkColumn('tbl','c2','0') || '::text as fld from tbl'; for row in execute sql loop return next row.fld; end loop; return; ... where the function returns a set of text (or int or whatever the datatype will always be. Sim On 01/27/2011 01:35 PM, Santosh Bhujbal (sabhujba) wrote: > Hi Sim, > > Thank you for the response. > > My question is why is it not working? > > What changes needs to be done in function or in calling SQL to make it > work as per requirement? > > Thanks, > > Santosh. > > *From:*pgsql-general-owner@postgresql.org > [mailto:pgsql-general-owner@postgresql.org] *On Behalf Of *Sim Zacks > *Sent:* Thursday, January 27, 2011 4:26 PM > *To:* pgsql-general@postgresql.org > *Subject:* Re: [GENERAL] Queyring for columns which are exist in table. > > On 01/27/2011 09:52 AM, Santosh Bhujbal (sabhujba) wrote: > > CREATE OR REPLACE FUNCTION ColumnAlreadyExists(name, name) RETURNS > INTEGER AS E' > > DECLARE columnCount INTEGER; > > BEGIN > > SELECT COUNT (pg_attribute.attname) into columnCount FROM > pg_attribute,pg_class, pg_type WHERE > ((pg_attribute.attrelid=pg_class.oid) AND > (pg_attribute.atttypid=pg_type.oid) AND (pg_class.relname = $1) > AND (pg_attribute.attname = $2)); > > IF columnCount = 0 THEN > > RETURN 0; > > END IF; > > RETURN 1; > > END; > > ' LANGUAGE 'plpgsql'; > > DROP FUNCTION checkColumn(name,name,name); > > CREATE OR REPLACE FUNCTION checkColumn(name, name, name) RETURNS > name AS E' > > DECLARE isColumnExist INTEGER; > > BEGIN > > SELECT ColumnAlreadyExists ($1,$2) into isColumnExist; > > IF isColumnExist = 0 THEN > > RETURN name($3); > > ELSE > > RETURN name($2); > > END IF; > > END; > > ' LANGUAGE 'plpgsql'; > > Function checkColumn should return proper column name (second > parameter) if column exist and third parameter if column not exist. > > NOW when I try to execute following command it returns improper > result. > > I expect proper column values as a output of query. > > SELECT(checkColumn('tbl','c2','0'))::name FROM tbl; > > mydb=# SELECT (checkColumn('tbl','c2','0'))::name FROM tbl; > > checkcolumn > > ------------- > > c2 > > c2 > > c2 > > c2 > > c2 > > c2 > > c2 > > c2 > > (8 rows) > > mydb=# > > Above query should return actual values present for c2 column in tbl. > > But it's not working as desired. > > Please help me in this. > > Thanks in advance, > > Santosh. > > the name datatype won't cast it into a selectable column. > Forgetting about your function, if you try select 'c2'::name from tbl > it will also return c2 as a name datatype. > What you want to do is a dynamic query where you use execute or for > row in execute in a plpgsql function. > > Also to see if the column exists, I would recommend using > information_schema.columns > --------------060304060405090704060309 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit

What you will need to do in your function is


...

sql = 'select ' || checkColumn('tbl','c2','0') || '::text as fld from tbl';

for row in execute sql loop

    return next row.fld;

end loop;

return;

...


where the function returns a set of text (or int or whatever the datatype will always be.


Sim


On 01/27/2011 01:35 PM, Santosh Bhujbal (sabhujba) wrote:

Hi Sim,

 

Thank you for the response.

 

My question is why is it not working?

What changes needs to be done in function or in calling SQL to make it work as per requirement?

 

Thanks,

Santosh.

 

From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of Sim Zacks
Sent: Thursday, January 27, 2011 4:26 PM
To: pgsql-general@postgresql.org
Subject: Re: [GENERAL] Queyring for columns which are exist in table.

 

On 01/27/2011 09:52 AM, Santosh Bhujbal (sabhujba) wrote:

CREATE OR REPLACE FUNCTION ColumnAlreadyExists(name, name) RETURNS INTEGER AS E'

DECLARE columnCount INTEGER;

BEGIN

 

SELECT COUNT (pg_attribute.attname) into columnCount FROM pg_attribute,pg_class, pg_type WHERE ((pg_attribute.attrelid=pg_class.oid) AND (pg_attribute.atttypid=pg_type.oid) AND (pg_class.relname = $1) AND (pg_attribute.attname = $2));

 

IF columnCount = 0 THEN

RETURN 0;

END IF;

RETURN 1;

END;

' LANGUAGE 'plpgsql';

 

 

DROP FUNCTION checkColumn(name,name,name);

CREATE OR REPLACE FUNCTION checkColumn(name, name, name) RETURNS name AS E'

DECLARE isColumnExist INTEGER;

 

BEGIN

SELECT ColumnAlreadyExists ($1,$2) into isColumnExist;

 

IF isColumnExist = 0 THEN

RETURN name($3);

ELSE

RETURN name($2);

END IF;

END;

' LANGUAGE 'plpgsql';

 

 

 

Function checkColumn should return proper column name (second parameter) if column exist and third parameter if column not exist.

 

NOW when I try to execute following command it returns improper result.

I expect proper column values as a output of query.

 

SELECT(checkColumn('tbl','c2','0'))::name FROM tbl;

 

mydb=# SELECT (checkColumn('tbl','c2','0'))::name FROM tbl;

 checkcolumn

-------------

 c2

 c2

 c2

 c2

 c2

 c2

 c2

 c2

(8 rows)

 

mydb=#

 

 

Above query should return actual values present for c2 column in tbl.

But it’s not working as desired.

 

Please help me in this.

 

Thanks in advance,

Santosh.

the name datatype won't cast it into a selectable column.
Forgetting about your function, if you try select 'c2'::name from tbl it will also return c2 as a name datatype.
What you want to do is a dynamic query where you use execute or for row in execute in a plpgsql function.

Also to see if the column exists, I would recommend using information_schema.columns


--------------060304060405090704060309--