Ordering a record returned from a stored procedure

This is a multi-part message in MIME format.

------=_NextPart_000_0010_01C4B509.E0558290
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

I am pulling a report from the database using a stored procedure but cannot
get the information to return in a specific order unless I hardcode the
order by clause.

CREATE OR REPLACE FUNCTION submissionreport(INTEGER, DATE, TEXT) RETURNS
setof submissionrec AS '
DECLARE
result submissionrec%rowtype;
hmhmkey ALIAS for $1;
submissiondate ALIAS for $2;
sort ALIAS for $3;

BEGIN
RAISE NOTICE ''The sort order should be: %.'', sort;
FOR result IN
SELECT
(..... select all necessary fields ...)
FROM
(.... tables ...)
WHERE
(... contraints)

ORDER BY sort
LOOP
RETURN next result;
END LOOP;

RETURN result;


END;
' LANGUAGE plpgsql;

What am I missing? The returned data is ordered if the "Order By" clause has
the values hard coded but doesn't seem to read the "sort" variable.

Any help would be appreciated.


Kent Anderson

------=_NextPart_000_0010_01C4B509.E0558290
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1400" name=3DGENERATOR></HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D250102715-18102004>I am pull=
ing a
report from the database using a stored procedure but cannot get the inform=
ation
to return in a specific order unless I hardcode the order by clause.
</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN
class=3D250102715-18102004></SPAN></FONT> </DIV>
<DIV><FONT size=3D2><SPAN class=3D250102715-18102004>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D250102715-18102004>CREATE OR=
REPLACE
FUNCTION submissionreport(INTEGER, DATE, TEXT) RETURNS setof submissionrec =
AS
'<BR>DECLARE<BR>          =
;     
result
submissionrec%rowtype;<BR>        &=
nbsp;      
hmhmkey ALIAS for
$1;<BR>           &n=
bsp;   
submissiondate ALIAS for
$2;<BR>           &n=
bsp;   
sort ALIAS for $3;</SPAN></FONT></DIV>
<DIV><FONT face=3DArial></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN
class=3D250102715-18102004>BEGIN</SPAN></FONT></DIV>
<DIV><FONT size=3D+0><SPAN class=3D250102715-18102004><FONT face=3DArial si=
ze=3D2>RAISE
NOTICE ''The sort order should be: %.'', sort;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>FOR result IN<BR> SELECT</FONT></DIV>
<DIV></SPAN></FONT><SPAN class=3D250102715-18102004><FONT
face=3DArial>    (..... select all necessary fields
....)</FONT></SPAN></DIV>
<DIV><SPAN class=3D250102715-18102004><FONT
face=3DArial>FROM       </FONT></SPAN></DIV>
<DIV><SPAN class=3D250102715-18102004><FONT face=3DArial>   =
(....
tables ...)</FONT></SPAN></DIV>
<DIV><SPAN class=3D250102715-18102004><FONT
face=3DArial>WHERE   </FONT></SPAN></DIV>
<DIV><SPAN class=3D250102715-18102004><FONT face=3DArial>   =
(...
contraints)</FONT></SPAN></DIV>
<DIV><SPAN class=3D250102715-18102004><FONT face=3DArial></FONT></SPAN>&nbs=
p;</DIV>
<DIV><SPAN class=3D250102715-18102004><FONT face=3DArial>ORDER BY
sort<BR>LOOP<BR>        RETURN next
result;<BR>END LOOP;</FONT></SPAN></DIV>
<DIV><FONT face=3DArial></FONT> </DIV>
<DIV><SPAN class=3D250102715-18102004><FONT face=3DArial> RETURN
result;</FONT></SPAN></DIV>
<DIV><FONT face=3DArial></FONT> </DIV><SPAN class=3D250102715-18102004>
<DIV><BR><FONT face=3DArial>END;<BR>' LANGUAGE plpgsql;</FONT></DIV>
<DIV><FONT face=3DArial></FONT> </DIV>
<DIV><SPAN class=3D250102715-18102004><FONT face=3DArial>What am I missing?=
The
returned data is ordered if the "Order By" clause has the values =
hard
coded but doesn't seem to read the "sort" variable.</FONT></SPAN></DIV>
<DIV><SPAN class=3D250102715-18102004><FONT face=3DArial></FONT></SPAN>&nbs=
p;</DIV>
<DIV><SPAN class=3D250102715-18102004><FONT face=3DArial>Any help would be=

appreciated.</FONT></SPAN></DIV></SPAN></SPAN></FONT></DIV><SPAN
class=3D250102715-18102004>
<DIV><FONT face=3DArial
size=3D2>           =
            &nb=
sp;
</FONT></SPAN></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN
class=3D250102715-18102004></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Kent Anderson</FONT></DIV></BODY></HTML>

------=_NextPart_000_0010_01C4B509.E0558290--
kenta [ Mo, 18 Oktober 2004 17:59 ] [ ID #453827 ]

Re: Ordering a record returned from a stored procedure

On Mon, 18 Oct 2004, Kent Anderson wrote:

> I am pulling a report from the database using a stored procedure but cannot
> get the information to return in a specific order unless I hardcode the
> order by clause.
>
> CREATE OR REPLACE FUNCTION submissionreport(INTEGER, DATE, TEXT) RETURNS
> setof submissionrec AS '
> DECLARE
> result submissionrec%rowtype;
> hmhmkey ALIAS for $1;
> submissiondate ALIAS for $2;
> sort ALIAS for $3;
>
> BEGIN
> RAISE NOTICE ''The sort order should be: %.'', sort;
> FOR result IN
> SELECT
> (..... select all necessary fields ...)
> FROM
> (.... tables ...)
> WHERE
> (... contraints)
>
> ORDER BY sort
> LOOP
> RETURN next result;
> END LOOP;
>
> RETURN result;
>
>
> END;
> ' LANGUAGE plpgsql;
>
> What am I missing? The returned data is ordered if the "Order By" clause has
> the values hard coded but doesn't seem to read the "sort" variable.

You're telling it to order by the value of the third argument, not the
value of the column with the name of the third argument. I think right
now you'd need to use EXECUTE to put it in as if it were the expression to
sort on.


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match
sszabo [ Mo, 18 Oktober 2004 18:25 ] [ ID #453828 ]

Re: Ordering a record returned from a stored procedure - date issue

I have the code working except for the date part of the where clause. Can
anyone point out how
yield_date = ''''10/18/2004''''

can be translated so the 10/18/2004 is coming from a variable?
ie yield_date = '' ... variable with date

Thanks


This works but the date is hardcoded.
FOR result IN
EXECUTE ''SELECT
(..... select all necessary fields ...)
FROM
(.... tables ...)
WHERE hm_key= '' || hmhmkey || ''
AND yield_date = ''''10/18/2004''''
ORDER BY '' || sort
LOOP
RETURN next result;
END LOOP;

RETURN result;


This doesn't work and I am sure its due to all the '''' getting out of hand
when I try to have a date variable used by the string. (sorry for the mess
of apostraphes)
I have tried several variations but keep getting no records returned or an
error.
yield_date = '' || '''' || submissiondate || '''' || '' (returns 0 rows but
no error - the date variable does have a valid date in it)
yield_date = '''' || submissiondate || '''' (returns ERROR: invalid input
syntax for type date: " || submissiondate || ")
yield_date = '' || '''' submissiondate '''' || '' (returns ERROR: syntax
error at or near "$2" at character 982)
yield_date = '' || '' || submissiondate || '' || '' (returns ERROR: column
"submissiondate" does not exist)

FOR result IN
EXECUTE ''SELECT
(..... select all necessary fields ...)
FROM
(.... tables ...)
WHERE hm_key= '' || hmhmkey || ''
AND yield_date = '' || '''' || submissiondate || '''' || ''
ORDER BY '' || sort
LOOP
RETURN next result;
END LOOP;

RETURN result;


-----Original Message-----
From: pgsql-sql-owner [at] postgresql.org
[mailto:pgsql-sql-owner [at] postgresql.org]On Behalf Of Stephan Szabo
Sent: Monday, October 18, 2004 11:25 AM
To: Kent Anderson
Cc: Pgsql-Sql [at] Postgresql. Org
Subject: Re: [SQL] Ordering a record returned from a stored procedure



On Mon, 18 Oct 2004, Kent Anderson wrote:

> I am pulling a report from the database using a stored procedure but
cannot
> get the information to return in a specific order unless I hardcode the
> order by clause.
>
> CREATE OR REPLACE FUNCTION submissionreport(INTEGER, DATE, TEXT) RETURNS
> setof submissionrec AS '
> DECLARE
> result submissionrec%rowtype;
> hmhmkey ALIAS for $1;
> submissiondate ALIAS for $2;
> sort ALIAS for $3;
>
> BEGIN
> RAISE NOTICE ''The sort order should be: %.'', sort;
> FOR result IN
> SELECT
> (..... select all necessary fields ...)
> FROM
> (.... tables ...)
> WHERE
> (... contraints)
>
> ORDER BY sort
> LOOP
> RETURN next result;
> END LOOP;
>
> RETURN result;
>
>
> END;
> ' LANGUAGE plpgsql;
>
> What am I missing? The returned data is ordered if the "Order By" clause
has
> the values hard coded but doesn't seem to read the "sort" variable.

You're telling it to order by the value of the third argument, not the
value of the column with the name of the third argument. I think right
now you'd need to use EXECUTE to put it in as if it were the expression to
sort on.


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match




---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo [at] postgresql.org
kenta [ Mo, 18 Oktober 2004 20:32 ] [ ID #453832 ]

Re: Ordering a record returned from a stored procedure - date issue

Never mind, it requires '''''''' on each side of the variable.

Thanks

-----Original Message-----
From: pgsql-sql-owner [at] postgresql.org
[mailto:pgsql-sql-owner [at] postgresql.org]On Behalf Of Kent Anderson
Sent: Monday, October 18, 2004 1:32 PM
To: Pgsql-Sql [at] Postgresql. Org
Subject: Re: [SQL] Ordering a record returned from a stored procedure -
date issue


I have the code working except for the date part of the where clause. Can
anyone point out how
yield_date = ''''10/18/2004''''

can be translated so the 10/18/2004 is coming from a variable?
ie yield_date = '' ... variable with date

Thanks


This works but the date is hardcoded.
FOR result IN
EXECUTE ''SELECT
(..... select all necessary fields ...)
FROM
(.... tables ...)
WHERE hm_key= '' || hmhmkey || ''
AND yield_date = ''''10/18/2004''''
ORDER BY '' || sort
LOOP
RETURN next result;
END LOOP;

RETURN result;


This doesn't work and I am sure its due to all the '''' getting out of hand
when I try to have a date variable used by the string. (sorry for the mess
of apostraphes)
I have tried several variations but keep getting no records returned or an
error.
yield_date = '' || '''' || submissiondate || '''' || '' (returns 0 rows but
no error - the date variable does have a valid date in it)
yield_date = '''' || submissiondate || '''' (returns ERROR: invalid input
syntax for type date: " || submissiondate || ")
yield_date = '' || '''' submissiondate '''' || '' (returns ERROR: syntax
error at or near "$2" at character 982)
yield_date = '' || '' || submissiondate || '' || '' (returns ERROR: column
"submissiondate" does not exist)

FOR result IN
EXECUTE ''SELECT
(..... select all necessary fields ...)
FROM
(.... tables ...)
WHERE hm_key= '' || hmhmkey || ''
AND yield_date = '' || '''' || submissiondate || '''' || ''
ORDER BY '' || sort
LOOP
RETURN next result;
END LOOP;

RETURN result;


-----Original Message-----
From: pgsql-sql-owner [at] postgresql.org
[mailto:pgsql-sql-owner [at] postgresql.org]On Behalf Of Stephan Szabo
Sent: Monday, October 18, 2004 11:25 AM
To: Kent Anderson
Cc: Pgsql-Sql [at] Postgresql. Org
Subject: Re: [SQL] Ordering a record returned from a stored procedure



On Mon, 18 Oct 2004, Kent Anderson wrote:

> I am pulling a report from the database using a stored procedure but
cannot
> get the information to return in a specific order unless I hardcode the
> order by clause.
>
> CREATE OR REPLACE FUNCTION submissionreport(INTEGER, DATE, TEXT) RETURNS
> setof submissionrec AS '
> DECLARE
> result submissionrec%rowtype;
> hmhmkey ALIAS for $1;
> submissiondate ALIAS for $2;
> sort ALIAS for $3;
>
> BEGIN
> RAISE NOTICE ''The sort order should be: %.'', sort;
> FOR result IN
> SELECT
> (..... select all necessary fields ...)
> FROM
> (.... tables ...)
> WHERE
> (... contraints)
>
> ORDER BY sort
> LOOP
> RETURN next result;
> END LOOP;
>
> RETURN result;
>
>
> END;
> ' LANGUAGE plpgsql;
>
> What am I missing? The returned data is ordered if the "Order By" clause
has
> the values hard coded but doesn't seem to read the "sort" variable.

You're telling it to order by the value of the third argument, not the
value of the column with the name of the third argument. I think right
now you'd need to use EXECUTE to put it in as if it were the expression to
sort on.


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match




---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo [at] postgresql.org




---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo [at] postgresql.org
kenta [ Mo, 18 Oktober 2004 21:17 ] [ ID #453833 ]

Re: Ordering a record returned from a stored procedure - date

Kent Anderson wrote:
> Never mind, it requires '''''''' on each side of the variable.

You will be delighted to learn that "dollar quoting" is in 8.0, which
allows you to do things like:

CREATE FUNCTION .... AS $$
...function body here without needing doubling of '
$$
LANGUAGE plpgsql;

I believe you can nest them too so long as you change the included
string: $UNIQUESYMBOL1$

> -----Original Message-----
> From: pgsql-sql-owner [at] postgresql.org
> [mailto:pgsql-sql-owner [at] postgresql.org]On Behalf Of Kent Anderson
> Sent: Monday, October 18, 2004 1:32 PM
> To: Pgsql-Sql [at] Postgresql. Org
> Subject: Re: [SQL] Ordering a record returned from a stored procedure -
> date issue
>
>
> I have the code working except for the date part of the where clause. Can
> anyone point out how
> yield_date = ''''10/18/2004''''
>
> can be translated so the 10/18/2004 is coming from a variable?
> ie yield_date = '' ... variable with date

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo [at] postgresql.org so that your
message can get through to the mailing list cleanly
dev [ Di, 19 Oktober 2004 10:21 ] [ ID #453837 ]
Datenbanken » comp.databases.postgresql.sql » Ordering a record returned from a stored procedure

Vorheriges Thema: problems using phpPgAmin
Nächstes Thema: Database backup