Re: variable value in array_to_string

From: "Armand Pirvu (home)" <armand(dot)pirvu(at)gmail(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: variable value in array_to_string
Date: 2016-11-21 23:15:01
Message-ID: 33699A13-AD2D-41C2-A730-543A0B61CBA0@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Played with unnest but not much luck

NOTICE: {item_id,show_id}
NOTICE: item_id

It takes only the first array element in consideration

Ughhhh

On Nov 21, 2016, at 5:02 PM, Armand Pirvu (home) <armand(dot)pirvu(at)gmail(dot)com> wrote:

> My bad on the back tick. No idea why it turned that way
>
> OK got that David.
>
> The idea is that I have the pk columns in an array which I would like to manipulate from the array itself rather than running same query variations multiple times
>
> For example I get in foo
>
> {item_id,show_id}
>
> And from here on I was looking to get say
>
> a.item_id , a.show_id
>
> and all sort of variations
>
> Thanks for help
>
>
>
>
> On Nov 21, 2016, at 4:26 PM, David G. Johnston <david(dot)g(dot)johnston(at)gmail(dot)com> wrote:
>
>> On Mon, Nov 21, 2016 at 3:09 PM, Armand Pirvu (home) <armand(dot)pirvu(at)gmail(dot)com> wrote:
>> Hi
>>
>> Is there anyway I can pass a variable in the array_to_string function ?
>>
>>
>> ​Yes, just like you can pass variables to any other function.​..
>>
>>
>> CREATE OR REPLACE FUNCTION test1 () RETURNS void AS $$
>> ​​DECLARE
>> foo text;
>> foo1 text;
>> begin
>> execute
>> 'select ARRAY( SELECT d.COLUMN_NAME::text from information_schema.constraint_table_usage c, information_schema.key_column_usage d '
>> ||
>> 'where c.constraint_name=d.constraint_name and c.table_name=d.table_name and c.table_schema='
>> ||
>> quote_literal(‘myschema')
>> ||
>> ' and c.table_schema=d.table_schema and c.constraint_name like '
>> ||
>> quote_literal('%_pkey%')
>> ||
>> ') ' into foo;
>> raise notice '%', foo;
>> foo1 :=ARRAY_TO_STRING(foo, ', ');
>> raise notice '%', foo1;
>> END;
>> $$ LANGUAGE plpgsql ;
>>
>>
>> The idea is that I want to slice and dice and not run the query x amount of times
>>
>>
>> I would suggest you learn to use the "format()" function, EXECUTE USING,​ and dollar quoting.
>>
>>
>> So, I copy-pasted your example and placed it into a DO block
>>
>> The character before "myschema" came out as a back-tick instead of a single-quote.
>>
>> array_to_string has the signature (text[], text) but you've defined foo as being just text. Changing that results in a query that executes - though given my system doesn't have conforming data I get no results.
>>
>> David J.
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Fran ... 2016-11-21 23:34:33 Re: Database migration to RDS issues permissions
Previous Message Armand Pirvu (home) 2016-11-21 23:02:46 Re: variable value in array_to_string