[SQL] Join issue?

2004-12-15 Thread Marian POPESCU
Hi,
I have a problem with this join query:

SELECT
CASE WHEN (UR.id_rights IS NULL) THEN '0' ELSE UR.id_rights END as
id_rights,
CASE WHEN (UR.r_category IS NULL) THEN 'CMP' ELSE UR.r_category END as
category,
U.id as id_user,
U.username
FROM mgw_users U LEFT OUTER JOIN mgw_cnt_users_rights UR ON (U.id =
UR.r_id_user)
WHERE (U."level" = 9)
AND (
  ((UR.r_id_object = 5) OR (UR.r_id_object IS NULL))
   AND
  ((UR.r_category = 'CMP') OR (UR.r_category IS NULL))
)
ORDER BY U.username;

I get this result and I expect something else:

0;"CMP";1;"admin"
0;"CMP";4;"user2"

I would like to obtain

0;"CMP";1;"admin"
0;"CMP";2;"user0"
0;"CMP";3;"user1"
0;"CMP";4;"user2"

What am I doing wrong?
Tables structure is:
CREATE TABLE companies
(
  id_company int8 NOT NULL DEFAULT nextval('mgw__seq_cnt_companies'::text),
  cmp_node int8[] NOT NULL DEFAULT '{0}'::bigint[],
  cmp_name varchar(150) NOT NULL DEFAULT '???'::character varying,
  cmp_created timestamp NOT NULL DEFAULT now(),
  cmp_created_by int8 NOT NULL DEFAULT 0,
  CONSTRAINT mgw_cnt_companies_pkey PRIMARY KEY (id_company, cmp_node)
)
WITH OIDS;
ALTER TABLE mgw_cnt_companies OWNER TO postgres;
CREATE TABLE users_rights
(
  id_rights int8 NOT NULL DEFAULT
nextval('mgw__seq_cnt_users_rights'::text),
  r_category varchar(3) NOT NULL DEFAULT 'CMP'::character varying,
  r_id_object int8 NOT NULL DEFAULT 0,
  r_id_user int8 NOT NULL DEFAULT 0,
  r_created timestamp NOT NULL DEFAULT now(),
  r_created_by int8 NOT NULL DEFAULT 0,
  r_expires timestamp NOT NULL DEFAULT (now() + '365 days'::interval),
  r_suspended int2 NOT NULL DEFAULT 0,
  r_rights varchar(3) NOT NULL DEFAULT 'RWD'::character varying,
  CONSTRAINT mgw_cnt_users_rights_pkey PRIMARY KEY (id_rights)
)
WITH OIDS;
ALTER TABLE mgw_cnt_users_rights OWNER TO postgres;
CREATE TABLE mgw_users
(
  id int4 NOT NULL,
  username varchar(100) NOT NULL,
  "level" int4,
  CONSTRAINT mgw_users_pkey PRIMARY KEY (id)
)
WITH OIDS;
ALTER TABLE mgw_users OWNER TO postgres;
Table data is:
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (1, '{0}', 'A Company', '2004-12-13
18:04:11.288622', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (2, '{0,0}', 'A SubCompany',
'2004-12-13 18:04:31.612607', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (3, '{0,0,0}', 'A Sub Sub Company',
'2004-12-13 18:04:49.207465', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (5, '{1}', 'Netscape', '2004-12-13
18:31:12.783856', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (6, '{1,0}', 'Netscape Division',
'2004-12-13 18:31:23.243747', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (7, '{1,0,0}', 'Netscape Navigator',
'2004-12-13 18:31:57.840392', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (8, '{1,1,0}', 'Mozilla',
'2004-12-13 18:32:17.618974', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (9, '{1,2,0}', 'AOL', '2004-12-14
14:56:45.938362', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (10, '{1,1,0,0}', 'Mozilla.org',
'2004-12-14 14:57:07.246855', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (11, '{1,1,0,1}', 'Mozillazine',
'2004-12-14 14:57:22.314781', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (4, '{0,0,1}', 'A Sub Sub Company
2', '2004-12-13 18:26:02.966243', 0);
INSERT INTO mgw_cnt_users_rights (id_rights, r_category, r_id_object,
r_id_user, r_created, r_created_by, r_expires, r_suspended, r_rights)
VALUES (1, 'CMP', 8, 2, '2004-12-14 18:05:30.946643', 0, '2005-12-14
18:05:30.946643', 0, 'RWD');
INSERT INTO mgw_cnt_users_rights (id_rights, r_category, r_id_object,
r_id_user, r_created, r_created_by, r_expires, r_suspended, r_rights)
VALUES (2, 'CMP', 8, 3, '2004-12-14 18:25:13.277141', 0, '2005-12-14
18:25:13.277141', 0, 'RWD');
INSERT INTO mgw_cnt_users_rights (id_rights, r_category, r_id_object,
r_id_user, r_created, r_created_by, r_expires, r_suspended, r_rights)
VALUES (3, 'CMP', 7, 2, '2004-12-15 11:11:35.916306', 0, '2005-12-15
11:11:35.916306', 0, 'RWD');
INSERT INTO mgw_cnt_users_rights (id_rights, r_category, r_id_object,
r_id_user, r_created, r_created_by, r_expires, r_suspended, r_rights)
VALUES (4, 'CNT', 8, 2, '2004-12-15 11:49:48.007345', 0, '2005-12-15
11:49:48.007345', 0, 'RWD');
INSERT INTO mgw_users (id, username, "level") VALUES (1, 'admin', 9);
INSERT INTO mgw_users (id, username, "level") VALUES (2, 'user0', 9);
INSERT INTO mgw_users (id, username, "level") VALUES (3, 'user1', 9);
INSE

[SQL] Join issue?

2004-12-15 Thread Marian POPESCU
Hi,

I have a problem with this join query:


SELECT
CASE WHEN (UR.id_rights IS NULL) THEN '0' ELSE UR.id_rights END as
id_rights,
CASE WHEN (UR.r_category IS NULL) THEN 'CMP' ELSE UR.r_category END as
category,
U.id as id_user,
U.username
FROM mgw_users U LEFT OUTER JOIN mgw_cnt_users_rights UR ON (U.id =
UR.r_id_user)
WHERE (U."level" = 9)
AND (
  ((UR.r_id_object = 5) OR (UR.r_id_object IS NULL))
   AND
  ((UR.r_category = 'CMP') OR (UR.r_category IS NULL))
)
ORDER BY U.username;


I get this result and I expect something else:

0;"CMP";1;"admin"
0;"CMP";4;"user2"


I would like to obtain

0;"CMP";1;"admin"
0;"CMP";2;"user0"
0;"CMP";3;"user1"
0;"CMP";4;"user2"


What am I doing wrong?

Tables structure is:

CREATE TABLE companies
(
  id_company int8 NOT NULL DEFAULT nextval('mgw__seq_cnt_companies'::text),
  cmp_node int8[] NOT NULL DEFAULT '{0}'::bigint[],
  cmp_name varchar(150) NOT NULL DEFAULT '???'::character varying,
  cmp_created timestamp NOT NULL DEFAULT now(),
  cmp_created_by int8 NOT NULL DEFAULT 0,
  CONSTRAINT mgw_cnt_companies_pkey PRIMARY KEY (id_company, cmp_node)
)
WITH OIDS;
ALTER TABLE mgw_cnt_companies OWNER TO postgres;

CREATE TABLE users_rights
(
  id_rights int8 NOT NULL DEFAULT
nextval('mgw__seq_cnt_users_rights'::text),
  r_category varchar(3) NOT NULL DEFAULT 'CMP'::character varying,
  r_id_object int8 NOT NULL DEFAULT 0,
  r_id_user int8 NOT NULL DEFAULT 0,
  r_created timestamp NOT NULL DEFAULT now(),
  r_created_by int8 NOT NULL DEFAULT 0,
  r_expires timestamp NOT NULL DEFAULT (now() + '365 days'::interval),
  r_suspended int2 NOT NULL DEFAULT 0,
  r_rights varchar(3) NOT NULL DEFAULT 'RWD'::character varying,
  CONSTRAINT mgw_cnt_users_rights_pkey PRIMARY KEY (id_rights)
)
WITH OIDS;
ALTER TABLE mgw_cnt_users_rights OWNER TO postgres;

CREATE TABLE mgw_users
(
  id int4 NOT NULL,
  username varchar(100) NOT NULL,
  "level" int4,
  CONSTRAINT mgw_users_pkey PRIMARY KEY (id)
)
WITH OIDS;
ALTER TABLE mgw_users OWNER TO postgres;

Table data is:

INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (1, '{0}', 'A Company', '2004-12-13
18:04:11.288622', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (2, '{0,0}', 'A SubCompany',
'2004-12-13 18:04:31.612607', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (3, '{0,0,0}', 'A Sub Sub Company',
'2004-12-13 18:04:49.207465', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (5, '{1}', 'Netscape', '2004-12-13
18:31:12.783856', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (6, '{1,0}', 'Netscape Division',
'2004-12-13 18:31:23.243747', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (7, '{1,0,0}', 'Netscape Navigator',
'2004-12-13 18:31:57.840392', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (8, '{1,1,0}', 'Mozilla',
'2004-12-13 18:32:17.618974', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (9, '{1,2,0}', 'AOL', '2004-12-14
14:56:45.938362', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (10, '{1,1,0,0}', 'Mozilla.org',
'2004-12-14 14:57:07.246855', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (11, '{1,1,0,1}', 'Mozillazine',
'2004-12-14 14:57:22.314781', 0);
INSERT INTO mgw_cnt_companies (id_company, cmp_node, cmp_name,
cmp_created, cmp_created_by) VALUES (4, '{0,0,1}', 'A Sub Sub Company
2', '2004-12-13 18:26:02.966243', 0);


INSERT INTO mgw_cnt_users_rights (id_rights, r_category, r_id_object,
r_id_user, r_created, r_created_by, r_expires, r_suspended, r_rights)
VALUES (1, 'CMP', 8, 2, '2004-12-14 18:05:30.946643', 0, '2005-12-14
18:05:30.946643', 0, 'RWD');
INSERT INTO mgw_cnt_users_rights (id_rights, r_category, r_id_object,
r_id_user, r_created, r_created_by, r_expires, r_suspended, r_rights)
VALUES (2, 'CMP', 8, 3, '2004-12-14 18:25:13.277141', 0, '2005-12-14
18:25:13.277141', 0, 'RWD');
INSERT INTO mgw_cnt_users_rights (id_rights, r_category, r_id_object,
r_id_user, r_created, r_created_by, r_expires, r_suspended, r_rights)
VALUES (3, 'CMP', 7, 2, '2004-12-15 11:11:35.916306', 0, '2005-12-15
11:11:35.916306', 0, 'RWD');
INSERT INTO mgw_cnt_users_rights (id_rights, r_category, r_id_object,
r_id_user, r_created, r_created_by, r_expires, r_suspended, r_rights)
VALUES (4, 'CNT', 8, 2, '2004-12-15 11:49:48.007345', 0, '2005-12-15
11:49:48.007345', 0, 'RWD');


INSERT INTO mgw_users (id, username, "level") VALUES (1, 'admin', 9);
INSERT INTO mgw_users (id, username, "level") VALUES (2, 'user0', 9);
INSERT INTO mgw_users (id, username, "level") VALUES (3, 'user1', 9);
INSER

[SQL] [Fwd: Majordomo results: unsubscribe]

2004-12-15 Thread Jodi Kanter




I am going to be on leave from work for a few months and am trying to
remove myself from the admin and sql lists until I return. I received
the following message below. Can anyone assist? I am not sure why it's
not working.
My email address is [EMAIL PROTECTED] and my alias for this account is
[EMAIL PROTECTED]. Both may be listed because I frequently get
emails in duplicate.
Any help would be greatly appreciated.
Jodi Kanter

 Original Message 

  

  Subject: 
  Majordomo results: unsubscribe


  Date: 
  Wed, 15 Dec 2004 15:55:43 +


  From: 
  [EMAIL PROTECTED]


  Reply-To: 
  [EMAIL PROTECTED]


  To: 
  Jodi Kanter <[EMAIL PROTECTED]>

  



 approve GCW9AF unsubscribe pgsql-general [EMAIL PROTECTED]
 The unsubscribe command did not succeed.
 
 The password is invalid.  Some common reasons for this error are:
 
 The password was mistyped.
 
 The address [EMAIL PROTECTED] has not been registered.
 (You may be registered under a different e-mail address.)
 
 The password is not a valid administrative password for the
 pgsql-general mailing list.  
  
 The password was a temporary password, and has expired.
 
 See "help password" and "help admin_passwords" for more details.
 
 -- 
Stopping at signature separator.  No more commands will be processed.


Valid commands processed: 1
0 succeeded, 0 stalled, and 1 failed.


Use the following command: 
  sessioninfo 9a89a68dacc14bc87d2253d7c07230a6189a4e70
to see technical information about this session.



-- 


___


___
Jodi L Kanter
BioInformatics Database Administrator
University of Virginia
(434) 924-2846
[EMAIL PROTECTED]
 






[SQL] Breadth first traversal in PLSQL (How to implement Queue?)

2004-12-15 Thread Richard Rowell
I have a table with a unary (recursive) relationship that represents a
hierarchy.  With the gracious help of Mike Rylander I was able to port a
TSQL function that would traverse "up" the hierarchy to PL/SQL.  Now I
need help porting the "down" the hierarchy function.  

As implemented in TSQL I utilized a simple breadth first tree traversal.
I'm not sure how to replicate this in PL/SQL as I haven't figured out
how to implement the queue required for the breadth first algorithm.  My
queue is declared "queue SETOF INTEGER" and I'm able to "SELECT INTO"
this variable.  However when I try to delete the "current" value, I get
a syntax error.  If I comment the delete out, I also get an error when I
try to fetch the "next" value from the front of the queue.

Below is the function, followed by the psql output:

CREATE OR REPLACE FUNCTION svp_getchildproviderids (INTEGER)
  RETURNS SETOF INTEGER
  AS '
DECLARE
 parent_provider ALIAS FOR $1;
 cid INTEGER;
 queue SETOF INTEGER;
BEGIN
   SELECT INTO cid count(*) FROM providers WHERE uid =parent_provider;
   IF cid = 0 THEN
   RAISE EXCEPTION ''Inexistent ID --> %'', parent_provider;
   RETURN;
   END IF;
   cid := parent_provider;
   LOOP
   EXIT WHEN cid IS NULL;
   RETURN NEXT cid;
   SELECT INTO queue uid FROM providers WHERE parent_id = cid;
   DELETE FROM queue WHERE queue.queue = cid;
   SELECT INTO cid * FROM queue LIMIT 1;
   END LOOP;
   RETURN;
END;' LANGUAGE 'plpgsql';

sp_demo_505=# select * from svp_getchildproviderids(1);
ERROR:  syntax error at or near "$1" at character 14
CONTEXT:  PL/pgSQL function "svp_getchildproviderids" line 16 at SQL
statement


-- 


---(end of broadcast)---
TIP 6: Have you searched our list archives?

   http://archives.postgresql.org


Re: [SQL] Breadth first traversal in PLSQL (How to implement Queue?)

2004-12-15 Thread Mike Rylander
On Wed, 15 Dec 2004 12:54:44 -0600, Richard Rowell
<[EMAIL PROTECTED]> wrote:
> I have a table with a unary (recursive) relationship that represents a
> hierarchy.  With the gracious help of Mike Rylander I was able to port a
> TSQL function that would traverse "up" the hierarchy to PL/SQL.  Now I
> need help porting the "down" the hierarchy function.

Glad I could help!

> 
> As implemented in TSQL I utilized a simple breadth first tree traversal.
> I'm not sure how to replicate this in PL/SQL as I haven't figured out
> how to implement the queue required for the breadth first algorithm.  My
> queue is declared "queue SETOF INTEGER" and I'm able to "SELECT INTO"
> this variable.  However when I try to delete the "current" value, I get
> a syntax error.  If I comment the delete out, I also get an error when I
> try to fetch the "next" value from the front of the queue.
> 

You probably want to use a temp table to hold the queue.  Edits inline below.

> Below is the function, followed by the psql output:
> 
> CREATE OR REPLACE FUNCTION svp_getchildproviderids (INTEGER)
>   RETURNS SETOF INTEGER
>   AS '
> DECLARE
>  parent_provider ALIAS FOR $1;
>  cid INTEGER;

 -- Comment out the next line...
 -- queue SETOF INTEGER;

> BEGIN

 -- We need to use execute to create the queue, otherwise
 -- the OID will be cached and the next invocation will cause
 -- an exception.
 EXECUTE ''CREATE TEMP TABLE cid_queue
   (id SERIAL, cid INTEGER ) WITHOUT OIDS;'';

>SELECT INTO cid count(*) FROM providers WHERE uid =parent_provider;
>IF cid = 0 THEN
>RAISE EXCEPTION ''Inexistent ID --> %'', parent_provider;
>RETURN;
>END IF;
>cid := parent_provider;
>LOOP
>EXIT WHEN cid IS NULL;
>RETURN NEXT cid;

  -- Put the CID into the queue
  EXECUTE ''INSERT INTO cid_queue VALUES
   ((SELECT uid FROM providers WHERE
parent_id = '' ||
 quote_literal( cid ) || ''));'';

  -- We'll use EXECUTE to delete the current cid from the queue
  EXECUTE ''DELETE FROM cid_queue WHERE cid = '' ||
 quote_literal( cid ) || '';'';

  -- And a short loop to grab the next one
   FOR cid IN EXECUTE ''SELECT cid FROM cid_queue ORDER BY id LIMIT 1;''
   END LOOP;

>END LOOP;
>RETURN;
> END;' LANGUAGE 'plpgsql';

Let me know if that works.  As before, it's untested, so YMMV... :)

-- 
Mike Rylander
[EMAIL PROTECTED]
GPLS -- PINES Development
Database Developer
http://open-ils.org

---(end of broadcast)---
TIP 8: explain analyze is your friend


Re: [SQL] Join issue?

2004-12-15 Thread Stephan Szabo

On Wed, 15 Dec 2004, Marian POPESCU wrote:

> Hi,
>
> I have a problem with this join query:
>
> 
> SELECT
>   CASE WHEN (UR.id_rights IS NULL) THEN '0' ELSE UR.id_rights END as
> id_rights,
>   CASE WHEN (UR.r_category IS NULL) THEN 'CMP' ELSE UR.r_category END as
> category,
>   U.id as id_user,
>   U.username
> FROM mgw_users U LEFT OUTER JOIN mgw_cnt_users_rights UR ON (U.id =
> UR.r_id_user)
> WHERE (U."level" = 9)
> AND (
>((UR.r_id_object = 5) OR (UR.r_id_object IS NULL))
> AND
>((UR.r_category = 'CMP') OR (UR.r_category IS NULL))
> )
> ORDER BY U.username;
> 
>
> I get this result and I expect something else:
> 
> 0;"CMP";1;"admin"
> 0;"CMP";4;"user2"
> 

Which appears to me to be correct for the above on the data you gave.
The outer join results in a set like:
 id | r_id_object | r_category
+-+
  1 | |
  2 |   8 | CMP
  2 |   7 | CMP
  2 |   8 | CNT
  3 |   8 | CMP
  4 | |
Which then is filtered by the where clause.  All the id=2 and id=3 rows
fail the filter. Outer joins do not provide a NULL extended row if the
join condition succeeds on some rows.

> I would like to obtain
> 
> 0;"CMP";1;"admin"
> 0;"CMP";2;"user0"
> 0;"CMP";3;"user1"
> 0;"CMP";4;"user2"
> 

I'm not sure exactly what you want actually.  The case when on r_category
seems redundant since you're asking for only rows that have 'CMP' or NULL
and are making the latter into the former.

In general, I think you need to consider moving some of your conditions on
UR into the ON clause like ON (U.id = UR.r_id_user and ur.r_id_object=5
...) in which case rows in UR that fail the extra conditions don't prevent
a NULL extending row from being produced.

---(end of broadcast)---
TIP 5: Have you checked our extensive FAQ?

   http://www.postgresql.org/docs/faqs/FAQ.html


Re: [SQL] Breadth first traversal in PLSQL (How to implement Queue?)

2004-12-15 Thread Mike Rylander
Arg! One more change below

On Wed, 15 Dec 2004 21:48:57 -0500, Mike Rylander <[EMAIL PROTECTED]> wrote:
> On Wed, 15 Dec 2004 12:54:44 -0600, Richard Rowell
> <[EMAIL PROTECTED]> wrote:
> > I have a table with a unary (recursive) relationship that represents a
> > hierarchy.  With the gracious help of Mike Rylander I was able to port a
> > TSQL function that would traverse "up" the hierarchy to PL/SQL.  Now I
> > need help porting the "down" the hierarchy function.
> 
> Glad I could help!
> 
> >
> > As implemented in TSQL I utilized a simple breadth first tree traversal.
> > I'm not sure how to replicate this in PL/SQL as I haven't figured out
> > how to implement the queue required for the breadth first algorithm.  My
> > queue is declared "queue SETOF INTEGER" and I'm able to "SELECT INTO"
> > this variable.  However when I try to delete the "current" value, I get
> > a syntax error.  If I comment the delete out, I also get an error when I
> > try to fetch the "next" value from the front of the queue.
> >
> 
> You probably want to use a temp table to hold the queue.  Edits inline below.
> 
> > Below is the function, followed by the psql output:
> >
> > CREATE OR REPLACE FUNCTION svp_getchildproviderids (INTEGER)
> >   RETURNS SETOF INTEGER
> >   AS '
> > DECLARE
> >  parent_provider ALIAS FOR $1;
> >  cid INTEGER;
> 
>  -- Comment out the next line...
>  -- queue SETOF INTEGER;
> 
> > BEGIN
> 
>  -- We need to use execute to create the queue, otherwise
>  -- the OID will be cached and the next invocation will cause
>  -- an exception.
>  EXECUTE ''CREATE TEMP TABLE cid_queue
>(id SERIAL, cid INTEGER ) WITHOUT OIDS;'';
> 
> >SELECT INTO cid count(*) FROM providers WHERE uid =parent_provider;
> >IF cid = 0 THEN
> >RAISE EXCEPTION ''Inexistent ID --> %'', parent_provider;
> >RETURN;
> >END IF;
> >cid := parent_provider;
> >LOOP
> >EXIT WHEN cid IS NULL;
> >RETURN NEXT cid;
> 
>   -- Put the CID into the queue
>   EXECUTE ''INSERT INTO cid_queue VALUES
>((SELECT uid FROM providers WHERE
> parent_id = '' ||
>  quote_literal( cid ) || ''));'';
> 
>   -- We'll use EXECUTE to delete the current cid from the queue
>   EXECUTE ''DELETE FROM cid_queue WHERE cid = '' ||
>  quote_literal( cid ) || '';'';
> 
>   -- And a short loop to grab the next one
>FOR cid IN EXECUTE ''SELECT cid FROM cid_queue ORDER BY id LIMIT 
> 1;''
>END LOOP;
> 
> >END LOOP;

-- We need to drop the temp table, since this will probably be called
-- more than once in a transaction.
EXECUTE ''DROP TABLE cid_queue;'';

> >RETURN;
> > END;' LANGUAGE 'plpgsql';
> 
> Let me know if that works.  As before, it's untested, so YMMV... :)
> 
> --
> Mike Rylander
> [EMAIL PROTECTED]
> GPLS -- PINES Development
> Database Developer
> http://open-ils.org
> 


-- 
Mike Rylander
[EMAIL PROTECTED]
GPLS -- PINES Development
Database Developer
http://open-ils.org

---(end of broadcast)---
TIP 6: Have you searched our list archives?

   http://archives.postgresql.org


Re: [SQL] Breadth first traversal in PLSQL (How to implement Queue?)

2004-12-15 Thread Andrew - Supernews
On 2004-12-15, Richard Rowell <[EMAIL PROTECTED]> wrote:
> I have a table with a unary (recursive) relationship that represents a
> hierarchy.  With the gracious help of Mike Rylander I was able to port a
> TSQL function that would traverse "up" the hierarchy to PL/SQL.  Now I
> need help porting the "down" the hierarchy function.  

Have you looked at contrib/tablefunc's connectby() function?

-- 
Andrew, Supernews
http://www.supernews.com - individual and corporate NNTP services

---(end of broadcast)---
TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]


Re: [SQL] [Fwd: Majordomo results: unsubscribe]

2004-12-15 Thread Richard Huxton
Jodi Kanter wrote:
I am going to be on leave from work for a few months and am trying to 
remove myself from the admin and sql lists until I return. I received 
the following message below. Can anyone assist? I am not sure why it's 
not working.
My email address is [EMAIL PROTECTED] and my alias for this account is 
[EMAIL PROTECTED] Both may be listed because I frequently get emails 
in duplicate.
Any help would be greatly appreciated.
Try the web interface from http://www.postgresql.org/lists.html - you 
can unsubscribe/get a password reminder from there.

--
  Richard Huxton
  Archonet Ltd
---(end of broadcast)---
TIP 5: Have you checked our extensive FAQ?
  http://www.postgresql.org/docs/faqs/FAQ.html