dbTalk Databases Forums  

plpgsql function Syntax

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss plpgsql function Syntax in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
derrick@grifflink.com
 
Posts: n/a

Default plpgsql function Syntax - 09-01-2004 , 08:16 PM






Is it possible to get a select statement to work like this with a for loop?

CREATE FUNCTION public.search(int4)
RETURNS SETOF search1 AS
'
DECLARE
category alias for $1;
newrows search1%rowtype;
rec RECORD;
SQLCommand varchar;

BEGIN
IF (category = 0) THEN
SQLCommand := 'SELECT * FROM table';
ELSE
SQLCommand := 'SELECT idnumber FROM table';
END IF;

FOR rec IN SQLCommand
LOOP
...blah...
...blah...
END LOOP;

Basically I want to create the SELECT statement dynamically and then use
that select statement in subsequent querries later in the function. Will
this syntax work or should I try to use a VIEW?

Thanks,
Derrick


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org


Reply With Quote
  #2  
Old   
Stephan Szabo
 
Posts: n/a

Default Re: plpgsql function Syntax - 09-01-2004 , 09:32 PM







On Wed, 1 Sep 2004 derrick (AT) grifflink (DOT) com wrote:

Quote:
Is it possible to get a select statement to work like this with a for loop?

CREATE FUNCTION public.search(int4)
RETURNS SETOF search1 AS
'
DECLARE
category alias for $1;
newrows search1%rowtype;
rec RECORD;
SQLCommand varchar;

BEGIN
IF (category = 0) THEN
SQLCommand := 'SELECT * FROM table';
ELSE
SQLCommand := 'SELECT idnumber FROM table';
END IF;

FOR rec IN SQLCommand
LOOP
...blah...
...blah...
END LOOP;

Basically I want to create the SELECT statement dynamically and then use
that select statement in subsequent querries later in the function. Will
this syntax work or should I try to use a VIEW?
You should be able to use FOR rec IN EXECUTE SQLCommand LOOP to execute
the string and loop over the results I think.


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
  #3  
Old   
derrick@grifflink.com
 
Posts: n/a

Default Re: plpgsql function Syntax - 09-01-2004 , 10:20 PM



Excellent! Thank you.

Derrick
----- Original Message -----
From: "Stephan Szabo" <sszabo (AT) megazone (DOT) bigpanda.com>
To: <derrick (AT) grifflink (DOT) com>
Cc: <pgsql-novice (AT) postgresql (DOT) org>
Sent: Wednesday, September 01, 2004 8:32 PM
Subject: Re: [NOVICE] plpgsql function Syntax


Quote:
On Wed, 1 Sep 2004 derrick (AT) grifflink (DOT) com wrote:

Is it possible to get a select statement to work like this with a for
loop?

CREATE FUNCTION public.search(int4)
RETURNS SETOF search1 AS
'
DECLARE
category alias for $1;
newrows search1%rowtype;
rec RECORD;
SQLCommand varchar;

BEGIN
IF (category = 0) THEN
SQLCommand := 'SELECT * FROM table';
ELSE
SQLCommand := 'SELECT idnumber FROM table';
END IF;

FOR rec IN SQLCommand
LOOP
...blah...
...blah...
END LOOP;

Basically I want to create the SELECT statement dynamically and then use
that select statement in subsequent querries later in the function.
Will
this syntax work or should I try to use a VIEW?

You should be able to use FOR rec IN EXECUTE SQLCommand LOOP to execute
the string and loop over the results I think.



---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.