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.