Recursion in SQL

From: "Zeb Fropiaz" <aik__1(at)hotmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Recursion in SQL
Date: 2002-08-02 04:56:20
Message-ID: Uho29.3878$wh1.2446@news01.bloor.is.net.cable.rogers.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I want to find if X is a friend of Y. But the frienship could be through a
chain of other friends as well. So I need to use recursion. I wrote the
following code similar to something I found at IBM's site
(ftp://ftp.software.ibm.com/ps/products/db2/info/vr6/htm/db2s0/db2s0503.htm#
Header_1031)

void find_friend() {

EXEC SQL BEGIN DECLARE SECTION;
short Xsid;
short Ysid;
short count;
EXEC SQL END DECLARE SECTION;

printf("\nEnter first ID: ");
scanf("%hd", &Xsid);

printf("\nEnter second ID: ");
scanf("%hd", &Ysid);

EXEC SQL WITH Friend (own_id, friend_id) AS
( SELECT own_id, friend_id
FROM c434h27.bond B
WHERE own_id = :Xsid
UNION ALL
SELECT B2.own_id, B2.friend_id
FROM Friend B1, c434h27.bond B2
WHERE B1.friend_id = B2.own_id
)
SELECT count(*) INTO :count
FROM Friend
WHERE (own_id = :Xsid and friend_id = :Ysid) OR
(own_id = :Ysid and friend_id = :Xsid);

if (count > 0) {
printf ("X and Y are indirect friends!\n");
return;
}

}

When I compile it I get this error at compile time:

LINE MESSAGES FOR main.sqc
------ ----------------------------------------------------------------
SQL0060W The "C" precompiler is in progress.
1096 SQL0104N An unexpected token ":count" was found
following "SELECT count(*) INTO". Expected tokens may
include: "<space>". SQLSTATE=42601
SQL0092N No package was created because of previous
errors.
SQL0091W Precompilation or binding was ended with "2"
errors and "0" warnings.

Any clues what's going on!?!?

Thanx.

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2002-08-02 06:01:28 Re: huge performance penalty from constraint triggers
Previous Message Thomas Lockhart 2002-08-02 04:48:34 Re: getpid() function