刪除的存儲過程
CREATE PROCEDURE sp_Column_Delete
(
@Column_ID int
)
AS
Declare @Err As int
Set @Err = 0
Begin Tran
--首先查詢該節點下是否有子節點
Select Column_ID From Tb_Column Where Parent_ID = @Column_ID
IF @@RowCount<>0
Begin
Set @Err = 1
Goto theEnd
End
--獲取該節點的Column_Order,為了刪除後整理其他記錄的順序
Declare @Column_Order As int
Select @Column_Order = Column_Order From Tb_Column Where Column_ID = @Column_ID
IF @Column_Order Is NUll
Begin
Set @Err =2
Goto theEnd
End
--更新其他記錄的Column_Order
Update Tb_Column Set Column_Order = Column_Order -1 Where Column_Order >@Column_Order
IF @@Error<>0
Begin
Set @Err =3
Goto theEnd
End
--刪除操作
Delete From Tb_Column Where Column_ID=@Column_ID
IF @@Error<>0
Begin
Set @Err =4
Goto theEnd
End
--更新其他記錄的Column_ID
--Update Tb_Column Set Column_ID= Column_ID - 1 Where Column_ID >@Column_ID
--IF @@Error<>0
-- Begin
-- Set @Err =5
-- Goto theEnd
-- End
theEnd:
IF @Err = 0
Begin
Commit Tran
Return 0 --刪除成功
End
Else
Begin
IF @Err=1
Begin
Rollback Tran
Return 1 --有子節點
End
Else
Begin
Rollback Tran
Return 2--未知錯誤
End
End
GO