plpgsql function Syntax

From: derrick(at)grifflink(dot)com
To: <pgsql-novice(at)postgresql(dot)org>
Subject: plpgsql function Syntax
Date: 2004-09-02 01:16:11
Message-ID: 00dd01c4908a$6f1dff60$0200a8c0@main
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

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

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Stephan Szabo 2004-09-02 02:32:37 Re: plpgsql function Syntax
Previous Message Steve Crawford 2004-09-01 21:42:09 Re: Copy of DB behaves different from original