473,654 Members | 3,289 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 22852
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
5981
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( char* arg, int len ) { // stuff }
3
9342
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 "converted it" to VB.Net. My receiving method looks like this: Public DoIt(ByRef Array( , ) as string) as boolean but what I am not clear on is what the parameter looks like in my calling routine:
2
581
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
2
2178
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 function...I hope i am making sense...Please look at the code and help me in pointing out the error.. #include<stdio.h> #include<stdlib.h> #include<time.h>
13
2545
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 data from a query into an array. The intent is that the following code:
6
6926
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
1355
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 "Experience" and I want to return Experience array from service. In web service I implement like that @WebMethod(operationName = "getExperience") public Experience getExperience( @WebParam(name = "user") User user, ...
4
3533
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 programming with engineering applications. The class is an introduction to C++ with engineering emphasis. The bubble_sort function was provided by the professor. The other code was authored by myself. My question refers to calling a two dimensional...
0
1050
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 B;
12
2053
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 = args.shift(); return function(){ return fn.apply(object, args.concat(Array.prototype.slice.call(arguments)));
0
8376
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8594
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.