Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

My code is

select column_name from information.schema.columns where table_name='aean'

It returns column names of table aean.
Now I have declared an array:

DECLARE colnames text[]

How can I store select's output in colnames array.
Is there any need to initialize colnames?

share|improve this question
+1, I reached here with exact same error message- ERROR: could not find array type for data type information_schema.sql_identifier. Was trying with array_agg(column_name, ',') – jerrymouse Aug 21 '12 at 6:54
add comment (requires an account with 50 reputation)

1 Answer

There are two ways. One is to aggregate:

select array_agg(column_name::text)
from information.schema.columns
where table_name='aean'

The other is to use the array() operator:

select array(
select column_name
from information.schema.columns
where table_name='aean'
)

I'm presuming this is for plpgsql. In that case you can assign it like this:

colnames := array(
select column_name
from information.schema.columns
where table_name='aean'
);
share|improve this answer
can you give me the code that works on postgres because this is not working on postgres ERROR: could not find array type for data type information_schema.sql_identifier – mitesh Jun 19 '11 at 12:15
Sorry 'bout that. Mindless copying and pasting got array_agg() included in all three calls. I've also type-casted the initial one to make your PG version happy. – Denis Jun 19 '11 at 13:41
+1 Thanks, was searching for exact same thing. – jerrymouse Aug 21 '12 at 6:52
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.