473,473 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using recursive CTE in stored proc?

3 New Member
I have a need to run a recursive CTE within a stored proc, but I can't get it past this: SQL0104N An unexpected token "with" was found following "SET count=count+1; ". Expected tokens may include: "". LINE NUMBER=26.

My google-fu showed a couple of similar topics, but none with resolution.

The query functions as expected outside of the stored proc, so I'm hoping that there's some syntactic sugar I'm missing that'll let this work. Similarly, the proc compiles and works without the query.

Here's a contrived example:

Expand|Select|Wrap|Line Numbers
  1. --setup
  2. create table tree (id integer, name varchar(50), parent_id integer);
  3. insert into tree values (1, 'Alice', null);
  4. insert into tree values (2, 'Bob', 1);
  5. insert into tree values (3, 'Charlie', 2);
Expand|Select|Wrap|Line Numbers
  1. -- the proc
  2. create or replace procedure testme() RESULT SETS 1 LANGUAGE SQL
  3. BEGIN
  4. DECLARE SQLSTATE CHAR(5);
  5. DECLARE SQLCODE integer default 0;
  6. DECLARE count INTEGER;
  7. DECLARE sum INTEGER;
  8. DECLARE total INTEGER;
  9. DECLARE id INTEGER;
  10. DECLARE curs CURSOR WITH RETURN FOR 
  11. select count,sum from sysibm.sysdummy1;
  12.  
  13. DECLARE hiercurs CURSOR FOR 
  14. select id from tree order by id;
  15. SET bomQuery='';
  16. PREPARE stmt FROM bomQuery;
  17. SET count = 0;
  18. SET sum = 0;
  19. set total = 0;
  20. OPEN hiercurs;
  21. FETCH hiercurs INTO id;
  22. WHILE (SQLCODE <> 100) DO
  23. SET count=count+1;
  24.  
  25. with org (level,id,name,parent_id) as
  26. (select 1 as level,root.id,root.name,root.parent_id from tree root where root.id=id
  27. union all
  28. select level+1,employee.id,employee.name,employee.parent_ id from org boss, tree employee 
  29. where level < 5 and employee.parent_id=boss.id)
  30. select count(1) into sum from org;
  31.  
  32. SET total=total+sum;
  33. FETCH hiercurs INTO id;
  34. END WHILE;
  35. CLOSE hiercurs;
  36. OPEN curs;
  37. END
Did I mention this is a contrived example? I'm not looking for optimizations--just need help getting the CTE in the while loop to be accepted by DB2 9.5+

Thanks in advance,
-r
Jul 20 '10 #1
1 3715
Roger Barney
3 New Member
the cte in db2 doesn't seem to recognize the scalar result of the query, and so it won't let the select into work (not a problem on Oracle or SQLServer)...solution is to open a cursor and FETCH INTO (instead of SELECT INTO) instead.
Jul 21 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Wolfgang Kreuzer | last post by:
Try hard to become familiar with T-SQL. Can anybodey tell me the best way to deal with set's provided by a stored procedure. Til yesterday I thougt trapping set in temp table using INSERT EXEC...
0
by: Dave Sisk | last post by:
I've created a system or external trigger on an AS/400 file a.k.a DB2 table. (Note this is an external trigger defined with the ADDPFTRG CL command, not a SQL trigger defined with the CREATE...
14
by: Roy | last post by:
Apologies for the cross-post, but this truly is a two-sided question. Given the option of creating Looping statements within a stored proc of sql server or in the code-behind of an .net webpage,...
10
by: Eric E | last post by:
Hi all, I am using an Access client linked to a PG 7.4 server via ODBC. I have a stored proc on the server that inserts rows into a table.particular table, accomplished via an INSERT within the...
6
by: Paul M | last post by:
Hi All, I'm currently writing a z/OS DB2 Stored Proc in C, using an example from the IBM Stored Procedure guide (SG24-7083-00). The database calls to read and update the database work...
3
by: oregondba | last post by:
I have a database I have inherited (new job). I am trying to baseline the code and have done a DB2Look to get the code out of an existing db. The DB2Look produced a SP with the following form: ...
2
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I can successfully insert and update the oracle database by calling a oracles stored proc from my .net code. This oracle stored proc is returning some value. I cannot see that...
1
by: WebNewbie | last post by:
Hi, I am new to using mysql and there isn't any tutorials online on that shows how to create mysql stored procedure for paging purposes. Thus, I read tutorials on creating stored proc that were...
3
by: Deane | last post by:
I have a stored proc that returns a resultset. I would like to deposit that resultset into a table. Kind of like a "SELECT INTO", but using a stored proc. If I could do this -- SELECT INTO...
2
by: Rob Eventine | last post by:
hi all, how does one go about changing the details in the aspnetdb dbase using the supplied stored procs? i am using vb as my language but i have no idea how to use them, how to call them or...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.