Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
JHotchkiss
Starting Member
6 Posts |
Posted - 2003-11-10 : 06:24:05
|
| Hi I have a tree table which I want to delete records from (I would prefer not to use a recursive trigger as I have had problems using these with replication). My procedure is as follows:CREATE PROCEDURE spDeleteActorSoftware @iActorID INT, @iFileID INTAS DECLARE @iSuperActorID INT DECLARE software_cursor CURSOR FAST_FORWARD FOR SELECT ActorID FROM Actor WHERE SuperActorID = @iActorID---------------------------------------------------------------------------------------- DELETE FROM ActorSoftware WHERE ActorID = @iActorID AND FileID = @iFileID---------------------------------------------------------------------------------------- OPEN software_cursor FETCH NEXT FROM software_cursor INTO @iSuperActorID WHILE @@FETCH_STATUS = 0 BEGIN EXEC spDeleteActorSoftware @iSuperActorID, @FileID FETCH NEXT FROM software_cursor INTO @iSuperActorID END---------------------------------------------------------------------------------------- CLOSE software_cursor DEALLOCATE software_cursorGOon running it I get the following errors:(0 row(s) affected)Server: Msg 16915, Level 16, State 1, Procedure spDeleteActorSoftware, Line 7A cursor with the name 'software_cursor' already exists.(0 row(s) affected)Server: Msg 16905, Level 16, State 1, Procedure spDeleteActorSoftware, Line 20The cursor is already open.Server: Msg 16916, Level 16, State 1, Procedure spDeleteActorSoftware, Line 28A cursor with the name 'software_cursor' does not exist.Server: Msg 16916, Level 16, State 1, Procedure spDeleteActorSoftware, Line 32A cursor with the name 'software_cursor' does not exist.Server: Msg 16916, Level 16, State 1, Procedure spDeleteActorSoftware, Line 33A cursor with the name 'software_cursor' does not exist.I any ideas on what the workaround is?Cheers |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-11-10 : 07:11:41
|
| See this thread: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=30422 |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2003-11-10 : 07:15:42
|
Try this:http://www.sqlteam.com/item.asp?ItemID=8866If you want to delete all the children of a node, try:DELETE E FROM Employee E INNER JOIN Tree T ON E.EmployeeID=T.EmployeeIDINNER JOIN Tree B ON T.ParentNode=B.NodeWHERE T.Lineage LIKE '/' + Cast(T.ParentNode as varchar) + '/'AND B.EmployeeID=1003 --All Ian Faith's "children" get deleted |
 |
|
|
JHotchkiss
Starting Member
6 Posts |
Posted - 2003-11-25 : 05:27:23
|
| Thanks for the responses, I got some inspiration from them, I basically ended up with this, FYI (replaced leading tabs/spaces with _ to keep formatting, just search and replace in text editor with space):CREATE PROCEDURE spDeleteActorSoftware____@iActorID INT,____@iFileID INTAS____-- Create a temporary table to store the ID's of the records to delete, also the hierarchy level, used to get the next level____-- NOTE: primary key of table selecting from is ActorID, FileID as it is an intersection table____CREATE TABLE #tmpActorSoftware (ActorID INT NOT NULL, Level INT NOT NULL)____CREATE UNIQUE INDEX idxActorSoftware ON #tmpActorSoftware(ActorID, Level)____-- Stores the current hierarchy level (set to first level____DECLARE @iLevel INT____SET @iLevel = 1____-- Insert the ID's of all first level records into the temp table - level set to 1____INSERT INTO #tmpActorSoftware (ActorID, Level) ________SELECT ActorID, @iLevel________FROM ActorSoftware________WHERE ActorID = @iActorID ________AND FileID = @iFileID____-- Repeat while rows are returned, when not, will have reached bottom level____WHILE (@@ROWCOUNT > 0)____BEGIN________-- Increase level number________SET @iLevel = @iLevel + 1________-- Insert into temp table the next level rows, where the parent/super id of equals the row id ________-- of the previous level (level minus 1), lookup the row id of the child by accessing the Actor table________-- as hierarchy is not held in the insection table being deleted from ________INSERT INTO #tmpActorSoftware (ActorID, Level) ____________SELECT A.ActorID, @iLevel____________FROM #tmpActorSoftware T, Actor A, ActorSoftware S____________WHERE T.Level = (@iLevel - 1)____________AND T.ActorID = A.SuperActorID____________AND A.ActorID = S.ActorID____________AND S.FileID = @iFileID____END____-- Delete all rows where rowId the same as rows in temporary table and file id matches specified id____DELETE ActorSoftware____FROM ActorSoftware A, #tmpActorSoftware B____WHERE A.ActorID = B.ActorID____AND A.FileID = @iFileIDGO |
 |
|
|
|
|
|
|
|