473,503 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing array as argument and returning an array in plpgsql

Hi all,
I am using postgresql7.4. How to handle arrays in plpgsql. How can
I pass an array. Is it possible to retrieve values from an array by
indexing it like

argument : '{1,2,3}'
Return value : varchar array

Variables :
---------

a alias for $1
b _varchar

Usage :
-----

b[1] = a[1];
b[2] = a[2];

return b;

Is it possible.

TIA,

--
regards,
Deepa K

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #1
3 22840
hello

It is possible

CREATE OR REPLACE FUNCTION foo(anyarray) RETURNS anyarray AS '
DECLARE b integer[];
BEGIN b := $1; b[1] := b[1] + 1;
RETURN b;
END;
' LANGUAGE plpgsql;

testdb011=> select foo(ARRAY[1,2,3]);
foo
---------
{2,2,3}
(1 øádka)

Regards
Pavel
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #2
I got this when I was searching for something else. I will forward this
to you.
http://archives.postgresql.org/pgsql...1/msg00852.php
CREATE or REPLACE FUNCTION foo(integer[]) RETURNS int AS
'DECLARE
a alias for $1;
index integer := 1;
total integer := 0;
BEGIN
WHILE a[index] > 0
LOOP
total := total + a[index];
index := index + 1;
END LOOP;

RETURN total;
END;
' LANGUAGE 'plpgsql';

test=> select foo('{1,2}');
foo
-----
3
(1 row)
On Tue, 2003-12-16 at 03:25, K. Deepa wrote:
Hi all,
I am using postgresql7.4. How to handle arrays in plpgsql. How can
I pass an array. Is it possible to retrieve values from an array by
indexing it like

argument : '{1,2,3}'
Return value : varchar array

Variables :
---------

a alias for $1
b _varchar

Usage :
-----

b[1] = a[1];
b[2] = a[2];

return b;

Is it possible.

TIA,

--
Jenny Zhang
Open Source Development Lab
12725 SW Millikan Way, Suite 400
Beaverton, OR 97005
(503)626-2455 ext 31

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #3
Jenny Zhang wrote:
Jenny,

although this is old but i find it worth mentioning tom's
comment on it. I hit your comment on facing similar issue.

The cited example is pretty iffy since it assumes that the valid array
entries are all > 0. In recent PG version you can use the array_upper
and array_lower functions instead:

for i in array_lower(a,1) .. array_upper(a,1) loop
-- do something with a[i]
end loop;

regards, tom lane


I got this when I was searching for something else. I will forward this
to you.
http://archives.postgresql.org/pgsql...1/msg00852.php
CREATE or REPLACE FUNCTION foo(integer[]) RETURNS int AS
'DECLARE
a alias for $1;
index integer := 1;
total integer := 0;
BEGIN
WHILE a[index] > 0
LOOP
total := total + a[index];
index := index + 1;
END LOOP;

RETURN total;
END;
' LANGUAGE 'plpgsql';

test=> select foo('{1,2}');
foo
-----
3
(1 row)
On Tue, 2003-12-16 at 03:25, K. Deepa wrote:

Hi all,
I am using postgresql7.4. How to handle arrays in plpgsql. How can
I pass an array. Is it possible to retrieve values from an array by
indexing it like

argument : '{1,2,3}'
Return value : varchar array

Variables :
---------

a alias for $1
b _varchar

Usage :
-----

b[1] = a[1];
b[2] = a[2];

return b;

Is it possible.

TIA,

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
5969
by: Uncle | last post by:
I am an untrained hobbyist. Everything about programming I have learned from the internet. Thank you all for your gracious support. This is what I have: #define CONST_CHAR 0 void some_func(...
3
9336
by: SQLScott | last post by:
I have looked all over and I cannot find an example or information on passing a multi-dimensional array. Well, that is not true. I found a close example in C++ but it didn't work when I...
2
581
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
2
2166
by: sonaliagr | last post by:
I am trying to update a msg array in function by passing the address but it is showing an error. and also, i want the value of msg array to be accessible to the full code that is inside the main...
13
2533
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
6
6913
by: virgincita schmidtmann | last post by:
Good evening, I would like to pass the size of an array from the commandline. int main(int argc, int *argv) { .... max=*argv; int list; ....
0
1346
ntxsoft
by: ntxsoft | last post by:
Hello everybody, I have a small problem while returning array from web service. Firstly I'm new at java web services and I'm using netbeans 6 with glassfish 2. The problem is I have a class, name is...
4
3523
by: drktmplr11 | last post by:
Hi, this is my first post here at the forums, and I am looking for assistance with what looks to be a syntax error within my code. I am attending FIU, and looking to broaden my understanding of...
0
1046
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I'm exporting a COM interface from my C# application. I want to to export a function that will have an array argument of structs. The struct is like: public struct MyStaruct { int A; int...
12
2030
by: lorlarz | last post by:
In the code sample below, how are arguments a legitimate argument to Array.slice? Function.prototype.bind = function(){ var fn = this, args = Array.prototype.slice.call(arguments), object =...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.