Хранимая процедура upDBTreeRemoveNode
Процедура исключает узел из дерева без его удаления из базы.
CREATE PROCEDURE [upDBTreeRemoveNode]
@idDBTree INT,
@idNode INT
AS
DECLARE @Error INT
SET @Error=0
IF NOT Exists(SELECT * FROM DBTree WHERE idDBTree=@idDBTree AND idNode=@idNode) BEGIN
RAISERROR ('Нет такого узла',16,1)
RETURN 1
END
IF @@ERROR<>0 SET @Error=@@ERROR
DECLARE @idPrior int,@idNext int, @idParent int,@idFirstChild int
SELECT @idPrior=idPrior, @idNext=idNext,@idParent=idParent,@idFirstChild=idFirstChild FROM DBTree WHERE idDBTree=@idDBTree AND idNode=@idNode
IF @@ERROR<>0 SET @Error=@@ERROR
BEGIN TRAN
IF @idPrior IS NULL AND @idNext IS NULL AND @idParent IS NOT NULL
BEGIN
UPDATE DBTree SET idFirstChild=NULL WHERE idDBTree=@idDBTree AND idNode=@idParent
IF @@ERROR<>0 SET @Error=@@ERROR
END
ELSE IF @idPrior IS NULL AND @idNext IS NOT NULL
BEGIN
UPDATE DBTree SET idFirstChild=@idNext WHERE idDBTree=@idDBTree AND idNode=@idParent
IF @@ERROR<>0 SET @Error=@@ERROR
UPDATE DBTree SET idPrior=NULL WHERE idDBTree=@idDBTree AND idNode=@idNext
IF @@ERROR<>0 SET @Error=@@ERROR
END
ELSE IF @idPrior IS NOT NULL AND @idNext IS NULL
BEGIN
UPDATE DBTree SET idNext=NULL WHERE idDBTree=@idDBTree AND idNode=@idPrior
IF @@ERROR<>0 SET @Error=@@ERROR
END
ELSE IF @idPrior IS NOT NULL AND @idNext IS NOT NULL
BEGIN
UPDATE DBTree SET idNext=@idNext WHERE idDBTree=@idDBTree AND idNode=@idPrior
IF @@ERROR<>0 SET @Error=@@ERROR
UPDATE DBTree SET idPrior=@idPrior WHERE idDBTree=@idDBTree AND idNode=@idNext
IF @@ERROR<>0 SET @Error=@@ERROR
END
IF @Error=0
COMMIT TRAN
ELSE BEGIN
ROLLBACK TRAN
RETURN 1
END
Сайт управляется системой
uCoz