pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Rowell <richard@bowmansystems.com>
To: pgsql-sql@postgresql.org
Subject: Recursive SETOF function
Date: Mon, 22 Nov 2004 09:18:13 -0600
Message-ID: <1101136693.18148.66.camel@richard> (raw)

I'm trying to port some TSQL to PLPGSQL.  The DB has a table with a
recursive foreign key that represents a tree hierarchy.  I'm trying to
re-create a TSQL function that pulls out all the ancestors of a given
node in the hierarchy.

I'm rather new to PLSQL and I have several questions.

1.  In TSQL, I can assign a scalar to the result of query like so:
  SET @var1 = (SELECT foo FROM bar WHERE bar.uid=@var2)

How would I do this in PLSQL?

2.  In TSQL the "result table" can be inserted into manually.  IE:

CREATE FUNCTION foo () RETURNS @ttable TABLE( uid INTEGER) AS BEGIN 
	INSERT @ttable VALUES (1)
	RETURN
END

Is there a way to manually insert rows into the result table in PLSQL?


What follows is my TSQL function if that helps give context.  

CREATE FUNCTION svp_getparentproviderids (@child_provider INTEGER)
RETURNS @provider_ids TABLE ( uid INTEGER )
AS
BEGIN
    DECLARE @cid AS INTEGER
    IF (SELECT count(*) FROM providers WHERE uid =@child_provider) > 0
    BEGIN
	SET @cid = @child_provider
	WHILE @cid IS NOT NULL
	BEGIN
	    INSERT @provider_ids VALUES (@cid)
	    SET @cid = (SELECT parent_id FROM providers WHERE uid=@cid)
	END
    END
    RETURN
END

-- 




view thread (7+ messages)  latest in thread

Message-ID: <1101136693.18148.66.camel@richard>
Permalink:  ../1101136693.18148.66.camel@richard/
Also on:    postgresql.org/message-id/1101136693.18148.66.camel@richard

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: richard@bowmansystems.com
  Subject: Re: Recursive SETOF function
  In-Reply-To: <1101136693.18148.66.camel@richard>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox