I'm attempting to use nested sets to represent a data hierarchy (see: http://dev.mysql.com/tech-resources/articles/hierarchical-data.html
, except I'm doing it in SQL 2000, not MySQL).
I've written a stored procedure that handles an insert, it looks something like this the simplified code snippet I've attached. It works fine, but my concern is about concurrent users (i.e. someone else updating/inserting and affecting the hierarchy at the same time). The MySQL example uses a lock on the what I can only assume is the whole table before doing anything, but this seems like it might be
dangerous and/or overkill, especially if this data is ultimately being viewed a lot on a public web site. I thought about just creating a seperate table with one field and one row to act as a lock in this stored procedure (and potentially a couple other related SPs), but that doesn't seem like a best practice.
CREATE PROCEDURE [dbo].[INSERT_NESTED_SET_WEBPAGE]
-- get the parent's rgt value
DECLARE @parentRight int;
SELECT @parentRight = rgt FROM my_data WHERE ID = @parent
-- make room for the new child (at the end of the nested set)
UPDATE my_data SET lft = lft + 2 WHERE lft >= @parentRight
UPDATE my_data SET rgt = rgt + 2 WHERE rgt >= @parentRight
-- insert the new child (at the end of the nested set)
INSERT INTO my_date (title, lft, rgt) VALUES (
@parentRight + 1)