|
Question : convert adjacency model table into nested set model table
|
|
OK, after reading tons of info on nested sets (really, I have read every article on the web concerning nested sets) I have squared my process into the following bit of SQL that will convert my adjacency table into a nested sets table:
TABLE: "tbl_Personnel_Supervisors" Supervision_ID (PK) User_ID Supervisor_ID Lft rgt
Based on this table the following SQL will poulate the lft/rgt values in my table to properly convert it for nestedset type queries. HOWEVER, when I deploy, I will not be able to deploy to MS SQL Server... INSTEAD, I have to deploy to MS Access(Dont GASP, yes thats right downsize). SO, how can I convert the following into something I can use with Access?
For some more input, I am only using Access as the back-end, the UI will be all ASP/VBScript on the Web, the company just hasnt forked up the money for SQL Server yet:
-- Create the stack table DECLARE @tmpStack TABLE ( Supervision_ID INT IDENTITY(1, 1), User_ID INT NOT NULL, Lft INT NULL, rgt INT NULL )
-- what we do is start from a parent, in this instance -- we want to start from the very top which is the NULL parent DECLARE @Supervisor_ID AS INTEGER SET @Supervisor_ID = NULL
-- our counter is the variable used to set the left and right extent DECLARE @counter AS INTEGER SET @counter = 1
-- what we do is go check the parentid for children, if it -- has children we push -- it into the stack and move on to the next child DECLARE @Supervision_ID AS INTEGER DECLARE @oldid AS INTEGER
SET NOCOUNT ON
WHILE 1 = 1 BEGIN SET @Supervision_ID = NULL
-- get the first row which is a child of @partentid, which has not already -- been pushed onto the stack INSERT INTO @tmpStack ( User_ID, Lft ) SELECT TOP 1 User_ID, @counter FROM tbl_Personnel_Supervisors WHERE ISNULL(Supervisor_ID, 0) = ISNULL(@Supervisor_ID,0) AND User_ID NOT IN (SELECT User_ID FROM @tmpStack)
-- just check we are getting the right identity value -- this value does not get reset once its reas so we -- need to see if its the same as the older one SET @Supervision_ID = SCOPE_IDENTITY() IF ISNULL(@Supervision_ID, 0) = ISNULL(@oldid, 0) SET @Supervision_ID = NULL ELSE BEGIN SET @oldid = @Supervision_ID SET @counter = @counter + 1 END
-- if we have children (i.e if this operation inserted a row, -- then we carry on IF @Supervision_ID IS NULL BEGIN -- no rows left, which means we want to pop the last item SELECT TOP 1 @Supervision_ID = Supervision_ID FROM @tmpStack WHERE rgt IS NULL ORDER BY Supervision_ID DESC -- there are no items left to pop, so exit the procedure IF @Supervision_ID IS NULL BREAK
UPDATE @tmpStack SET rgt = @counter WHERE Supervision_ID = @Supervision_ID
SET @counter = @counter + 1
SELECT @Supervisor_ID = Supervisor_ID FROM tbl_Personnel_Supervisors WHERE User_ID = (SELECT User_ID FROM @tmpStack WHERE Supervision_ID = @Supervision_ID) END ELSE BEGIN -- this is easy enough, move on to the next level -- we want the parent id of the item just inserted SELECT @Supervisor_ID = User_ID FROM @tmpStack WHERE Supervision_ID = @Supervision_ID END END
-- And update all the Items in Hier_Dept -- in the original hierarchy table UPDATE e SET Lft = ts.Lft, rgt = ts.rgt FROM tbl_Personnel_Supervisors e INNER JOIN @tmpStack ts ON ts.User_ID = e.User_ID
GO
|
|
Answer : convert adjacency model table into nested set model table
|
|
MSDE IS SQL Server. Do you regard SQL Server as being as robust as Access? (dont laugh)(:-)
Pete
You can go to Community Support to ask for a to be moved.
|
|
|
|