0

I need to alter a table in my DB. What I want is to ad a column with type integer[] and add values to this column directly from another query which result is 4 id numbers.

What I got currently:

ALTER TABLE resourcen add rs_insurance integer[] 
update resourcen set rs_insurance = (select li_id from li_versicherungsart) 

The second command returns following error :

ERROR: column "rs_insurance" is of type integer[] but expression is of type integer
SQL state: 42804
Hint: You will need to rewrite or cast the expression.
Character: 37

How can I cast all IDs found to fit into int[] array?

4
  • select array_agg(li_id) ... but you most definitely need some kind of co-related subquery. But it's hard to tell without more detailed information on those two tables and how they relate to each other Commented Nov 23, 2016 at 9:44
  • This worked! Posted it as an answer so I can accept it! Commented Nov 23, 2016 at 9:47
  • Yes that is exaclty what I want. But the li_versicherungsart is different on different installations. Commented Nov 23, 2016 at 9:53
  • Because the values might be changed later on by the user. Commented Nov 23, 2016 at 10:04

1 Answer 1

4

You can use postgres array aggregation function:

update resourcen set rs_insurance = (select array_agg(li_id) from li_versicherungsart) 

Reference: https://www.postgresql.org/docs/current/static/functions-aggregate.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.