Re: plpgsql and arrays

From: Richard Huxton <dev(at)archonet(dot)com>
To: Artis Caune <Artis(dot)Caune(at)latnet(dot)lv>
Cc: pgsql-general(at)PostgreSQL(dot)org
Subject: Re: plpgsql and arrays
Date: 2007-01-12 10:08:20
Message-ID: 45A75E14.8080403@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Artis Caune wrote:
> I'm trying to write function which accept array and insert each element
> in table:
>
> =# SELECT * FROM add_user('{user1(at)domain, user2(at)domain}');
> ERROR: column "email" is of type character varying but expression is
> of type record
> HINT: You will need to rewrite or cast the expression.

> function is like this:

> FOR v_rec IN SELECT $1[i] FROM generate_series(array_lower($1,1),
> array_upper($1,1)) s(i)
...
> INSERT INTO users2 VALUES ((SELECT currval('users2_uid_seq')),
> v_rec);

Well, v_rec is a RECORD, just like the error says. You want the first
attribute. Try this:

FOR v_rec IN SELECT $1[i] AS username FROM ...
INSERT INTO users2 VALUES (..., v_rec.username)

However, I'd also just have a normal loop counting over the array
upper/lower bounds. Then you could just use:
INSERT INTO users2 VALUES (..., $1[i])

HTH
--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Gellert, Andre 2007-01-12 10:25:59 Need setup help for Postgresql 8.1.3 on Solaris 10
Previous Message Zeugswetter Andreas ADI SD 2007-01-12 09:49:53 Re: [HACKERS] Checkpoint request failed on version 8.2.1.