473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Regenerate a Nest Set for Access?

This is an SQL algorithm developed by Joe Celko. Has anyone converted the following SQL to vba?

*****************************************

To convert an adjacency list model into a nested set model, use a push
down stack algorithm. Assume that we have these tables:

Expand|Select|Wrap|Line Numbers
  1. -- Tree holds the adjacency model
  2. CREATE TABLE Tree
  3. (emp CHAR(10) NOT NULL,
  4. boss CHAR(10));
  5.  
  6. INSERT INTO Tree
  7. SELECT emp, boss FROM Personnel;
  8.  
  9. -- Stack starts empty, will holds the nested set model
  10. CREATE TABLE Stack
  11. (stack_top INTEGER NOT NULL,
  12. emp CHAR(10) NOT NULL,
  13. lft INTEGER,
  14. rgt INTEGER);
  15.  
  16. BEGIN ATOMIC
  17. DECLARE counter INTEGER;
  18. DECLARE max_counter INTEGER;
  19. DECLARE current_top INTEGER;
  20.  
  21. SET counter = 2;
  22. SET max_counter = 2 * (SELECT COUNT(*) FROM Tree);
  23. SET current_top = 1;
  24.  
  25. INSERT INTO Stack
  26. SELECT 1, emp, 1, NULL
  27. FROM Tree
  28. WHERE boss IS NULL;
  29.  
  30. DELETE FROM Tree
  31. WHERE boss IS NULL;
  32.  
  33. WHILE counter <= (max_counter - 2)
  34. LOOP IF EXISTS (SELECT *
  35. FROM Stack AS S1, Tree AS T1
  36. WHERE S1.emp = T1.boss
  37. AND S1.stack_top = current_top)
  38. THEN
  39. BEGIN -- push when top has subordinates, set lft value
  40. INSERT INTO Stack
  41. SELECT (current_top + 1), MIN(T1.emp), counter, NULL
  42. FROM Stack AS S1, Tree AS T1
  43. WHERE S1.emp = T1.boss
  44. AND S1.stack_top = current_top;
  45.  
  46. DELETE FROM Tree
  47. WHERE emp = (SELECT emp
  48. FROM Stack
  49. WHERE stack_top = current_top + 1);
  50.  
  51. SET counter = counter + 1;
  52. SET current_top = current_top + 1;
  53. END
  54. ELSE
  55. BEGIN -- pop the stack and set rgt value
  56. UPDATE Stack
  57. SET rgt = counter,
  58. stack_top = -stack_top -- pops the stack
  59. WHERE stack_top = current_top
  60. SET counter = counter + 1;
  61. SET current_top = current_top - 1;
  62. END IF;
  63. END LOOP;
  64. END;
Sep 22 '10 #1
1 1366
Already solved.

Thanks anyway.
Sep 24 '10 #2

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

Similar topics

1
by: Derek Fountain | last post by:
I have a script that has confirmed a user's login credentials and wants to move onward. I want to regenerate the session ID, so I have code something like this: $_SESSION = array(); //...
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
4
by: Cham | last post by:
Hi I have a complex Ms Access application with lots of forms and queries that needs to be moved on to the web. Is there an easy way to do this without re coding in ASP?
34
by: John Harrison | last post by:
An odd confession; an odd request; but here's the tale.... My company has a few PC systems which we have used for about 7 years and not updated - we've "made do", and besides, "if it ain't...
4
by: Dave | last post by:
If so, what is wrong with this code in APS 3.0? I open a recordset with order info. Each record contains the order header info plus a line item from the order detail. So the recordset looks...
5
by: ZikO | last post by:
Hi there. I have a problem. I have created nested classes but don't know how to access to inner classes. I know I can create objects: Hen Object; Hen::Nest ObjectNest; Hen::Nest::Egg...
3
by: Steven Nagy | last post by:
Hi all, ASP.NET : Framework 2.0 - C# A recent addition to my code generater will create GridView's and ObjectDataSource's in a control (ASCX). So the code gen creates an ascx, ascx.cs,...
7
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm not sure whether I'm missing something or not. I reviewed the SQL Statements generated by tableadapter when the selectcommand is select *. It seems they are extremely bad. The updatecommand...
9
by: prakashwadhwani | last post by:
Hi !! I'm about to develop a new project for a client. Should I go about it in Access 2003 or 2007 ? Purchasing it either for me or for my client is not a major consideration here ... what I'd...
3
by: readnlearn | last post by:
hii everyone can any one please tell me how to display a image after 3 time wrong entering of password or user name.... am using captcha but cannt display in the same page.... please help mee
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
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...

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.