Re: How to return an array of values from a function?

From: Sim Zacks <sim(at)compulab(dot)co(dot)il>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: How to return an array of values from a function?
Date: 2011-09-15 07:34:48
Message-ID: 4E71AA98.1060206@compulab.co.il
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html style="direction: ltr;">
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<style>body p { margin-bottom: 0cm; margin-top: 0pt; } </style>
</head>
<body style="direction: ltr;"
bidimailui-detected-decoding-type="latin-charset" bgcolor="#ffffff"
text="#000000">
On 09/15/2011 09:13 AM, Siva Palanisamy wrote:
<blockquote
cite="mid:90F0F47595235141A4380FCF01B0185B224141CCB4(at)CHN-HCLT-EVS07(dot)HCLT(dot)CORP(dot)HCL(dot)IN"
type="cite">
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<meta name="Generator" content="Microsoft Word 12 (filtered
medium)">
<style>
<!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;}
@page Section1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
{page:Section1;}
-->
</style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
<div class="Section1">
<p class="MsoNormal">Hi All,<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">I wish to return an array of values.
Assume, I have a table called contacts. I want to display all
the names available from a column in the UI. Hence I need to
return that column as an array of values. Please correct me if
there is any other solution exist.<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Here is the code, I&#8217;m trying to execute:<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">CREATE OR REPLACE FUNCTION get_all_names()<o:p></o:p></p>
<p class="MsoNormal">RETURNS TEXT[] AS '<o:p></o:p></p>
<p class="MsoNormal">DECLARE<o:p></o:p></p>
<p class="MsoNormal">group_data TEXT[];<o:p></o:p></p>
<p class="MsoNormal">BEGIN<o:p></o:p></p>
</div>
</blockquote>
Try<br>
<blockquote
cite="mid:90F0F47595235141A4380FCF01B0185B224141CCB4(at)CHN-HCLT-EVS07(dot)HCLT(dot)CORP(dot)HCL(dot)IN"
type="cite">
<div class="Section1">
<p class="MsoNormal">--SELECT display_name INTO group_data FROM
contacts;<o:p></o:p></p>
</div>
</blockquote>
select array(select display_name from contacts) into group_data;<br>
<blockquote
cite="mid:90F0F47595235141A4380FCF01B0185B224141CCB4(at)CHN-HCLT-EVS07(dot)HCLT(dot)CORP(dot)HCL(dot)IN"
type="cite">
<div class="Section1">
<p class="MsoNormal">RETURN group_data;<o:p></o:p></p>
<p class="MsoNormal">END;<o:p></o:p></p>
<p class="MsoNormal">'LANGUAGE 'plpgsql';<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">During compilation, I got the below error:<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">ERROR:&nbsp; array value must start with "{" or
dimension information<o:p></o:p></p>
<p class="MsoNormal">CONTEXT:&nbsp; PL/pgSQL function "get_all_ names
" line 4 at SQL statement<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Regards,<o:p></o:p></p>
<p class="MsoNormal">Siva.<o:p></o:p></p>
</div>
<br>
</blockquote>
<br>
</body>
</html>

Attachment Content-Type Size
unknown_filename text/html 4.0 KB

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Yang Zhang 2011-09-15 07:53:59 Why is this query running slowly?
Previous Message Thomas Kellerer 2011-09-15 07:29:54 Re: Problem with the 9.1 one-click installer Windows7 64bit