P
Paolo
Hi I've been banging my head with this for some time.
I'm converting Joe Celko's nested sets into Access, and I have it
working, but it's so inelegant, I'm convinced there's got to be a
better solution. Basically a nested set is a hierarchical
representation for relational databases. There's not much need to
understand other than each record has two field named 'left' and
'right' which determine where the record falls in a hierarchy and that
Element ID is a primary key. Let's say I have 3 fields in tblElements:
ElementID Left Right
The top record alone would look like this:
Parent 1 2
If I were to add a child record, it would look like this:
Parent 1 4
Child 2 3
In SQL to add a record I can do this:
-----------------------------------------------------------------------------------------------------
1) SELECT Left, Right
FROM tblElements
INTO tblIsolateElementAdd
WHERE ElementID= 'parent element as determined by the value of some
form control'
2) UPDATE tblElements, tblIsolateElementAdd
SET tblElements.Left = tblElements.Left+2
WHERE tblElements.Left>tblIsolateElementAdd.Right
3) UPDATE tblElements, tblIsolateElementAdd
SET tblElements.Right = tblElements.Right+2
WHERE tblElements.Right >= tblIsolateElementAdd.Right
4) UPDATE tblIsolateElementAdd
SET ElementID ='value of some control',
I'm converting Joe Celko's nested sets into Access, and I have it
working, but it's so inelegant, I'm convinced there's got to be a
better solution. Basically a nested set is a hierarchical
representation for relational databases. There's not much need to
understand other than each record has two field named 'left' and
'right' which determine where the record falls in a hierarchy and that
Element ID is a primary key. Let's say I have 3 fields in tblElements:
ElementID Left Right
The top record alone would look like this:
Parent 1 2
If I were to add a child record, it would look like this:
Parent 1 4
Child 2 3
In SQL to add a record I can do this:
-----------------------------------------------------------------------------------------------------
1) SELECT Left, Right
FROM tblElements
INTO tblIsolateElementAdd
WHERE ElementID= 'parent element as determined by the value of some
form control'
2) UPDATE tblElements, tblIsolateElementAdd
SET tblElements.Left = tblElements.Left+2
WHERE tblElements.Left>tblIsolateElementAdd.Right
3) UPDATE tblElements, tblIsolateElementAdd
SET tblElements.Right = tblElements.Right+2
WHERE tblElements.Right >= tblIsolateElementAdd.Right
4) UPDATE tblIsolateElementAdd
SET ElementID ='value of some control',
=
,
=
+ 1,
=
+ 1,
5) INSERT INTO tblElements (ElementID,
5) INSERT INTO tblElements (ElementID,
,
)
SELECT ElementID,
SELECT ElementID,
,
FROM tblIsolateElementAdd
6) DROP TABLE tblIsolateElementAdd
------------------------------------------------------------------------------------------
As you can see I am using Cartesian joins and well as creating a
temporary table. Indeed, I could have created a one record recordset
(snapshot) using DAO to do avoid the cartesian joins and the temporary
table (pseudocode):
--------------------------------------------------------------------------------------------
Set rs = Db.OpenRecordset("SELECT Left, Right FROM " & _
"tblElements WHERE ElementID ='" & parent element as determined
by the value of
some form control & "'", dbOpenSnapshot)
UPDATE tblElements
SET
FROM tblIsolateElementAdd
6) DROP TABLE tblIsolateElementAdd
------------------------------------------------------------------------------------------
As you can see I am using Cartesian joins and well as creating a
temporary table. Indeed, I could have created a one record recordset
(snapshot) using DAO to do avoid the cartesian joins and the temporary
table (pseudocode):
--------------------------------------------------------------------------------------------
Set rs = Db.OpenRecordset("SELECT Left, Right FROM " & _
"tblElements WHERE ElementID ='" & parent element as determined
by the value of
some form control & "'", dbOpenSnapshot)
UPDATE tblElements
SET
=
+2
WHERE
WHERE
>rs!Left
UPDATE tblElements
SET
UPDATE tblElements
SET
=
+2
WHERE
WHERE
>= rs!Right
INSERT INTO tblElements (ElementID,
INSERT INTO tblElements (ElementID,
,
)
VALUES 'value of some control', rs!Right, rs!Right+1
------------------------------------------------------------------------------------------------
Is the latter solution the way to go? Or is there a simpler solution?
What about avoiding the SQL altogether and just using DAO? Does that
sound like a better way to go?
Thanks in advance,
Paolo
(e-mail address removed), eliminating NOSPAM
VALUES 'value of some control', rs!Right, rs!Right+1
------------------------------------------------------------------------------------------------
Is the latter solution the way to go? Or is there a simpler solution?
What about avoiding the SQL altogether and just using DAO? Does that
sound like a better way to go?
Thanks in advance,
Paolo
(e-mail address removed), eliminating NOSPAM