help using arrays in a function

From: "Jennifer Lee" <jlee(at)scri(dot)sari(dot)ac(dot)uk>
To: <pgsql-general(at)postgresql(dot)org>
Subject: help using arrays in a function
Date: 2004-02-26 10:35:26
Message-ID: BE57D4C018CC2642AF256E2FDDA9095736945A@sims.scri.sari.ac.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

I have a rather simple function I've been using in 7.3.4

version
------------------------------------------------------------------------
-----------------
PostgreSQL 7.3.4 on i686-pc-cygwin, compiled by GCC gcc (GCC) 3.2
20020927 (prerelease)
(1 row)

This compares an integer to all elements in an integer array and looks
for a match

CREATE OR REPLACE FUNCTION public.array_element_compare(_int4, int4)
RETURNS text AS
'
DECLARE
a ALIAS FOR $1;
b ALIAS FOR $2;
i integer;
status text;
BEGIN
i := 1;
status := ''false'';
WHILE a[i] LOOP
IF a[i] = b THEN
status := ''true'';
END IF;
i := i+1;
END LOOP;
RETURN status;
END;'
LANGUAGE 'plpgsql';

It works find

germinate1.5 =# select array_element_compare('{100,92,82}', 82);
array_element_compare
-----------------------
true
(1 row)

germinate1.5 =# select array_element_compare('{100,92}', 82);
array_element_compare
-----------------------
false
(1 row)

I'm now testing our database on 7.4 and get an error with this function
and I can't figure out how to fix it.

version
------------------------------------------------------------------------
-------------
PostgreSQL 7.4 on i686-pc-cygwin, compiled by GCC gcc (GCC) 3.3.1
(cygming special)
(1 row)

germinate1.5 =# select array_element_compare('{100, 92, 82}', 82);
ERROR: invalid input syntax for type boolean: "100"
CONTEXT: PL/pgSQL function "array_element_compare" line 9 at while

Can anyone point me in the right direction?

Thanks much for any help,
Jennifer

*****************************************************************
DISCLAIMER:

This email is from the Scottish Crop Research Institute, but the views expressed by the sender are not necessarily the views of SCRI and its subsidiaries. This email and any files transmitted with it are confidential to the intended recipient at the e-mail address to which it has been addressed. It may not be disclosed or used by any other than that addressee.
If you are not the intended recipient you are requested to preserve this confidentiality and you must not use, disclose, copy, print or rely on this e-mail in any way. Please notify mail(at)scri(dot)sari(dot)ac(dot)uk quoting the name of the sender and delete the email from your system.

Although SCRI has taken reasonable precautions to ensure no viruses are present in this email, neither the Institute nor the sender accepts any responsibility for any viruses, and it is your responsibility to scan the email and the attachments (if any).

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Peter Eisentraut 2004-02-26 13:12:16 Re: postgreSQL licenseing
Previous Message cnliou 2004-02-26 09:37:22 Re: Simplyfying many equals in a join