SQL中的存储过程非常重要,下面就将为您介绍SQL中删除无限极分类的存储过程,供您参考,希望对您能够有所帮助。
01 CREATE PROCEDURE [dbo].[Category_Delete]
02 @CategoryId int
03 AS
04 --DELETE [Category]
05 -- WHERE CategoryId=@CategoryId
06
07 --DELETE [Category]
08 -- WHERE FatherId=@CategoryId
09
10 declare @taba TABLE([tabid] [int] NOT NULL)
11 insert @taba (tabid)
12 select CategoryId from Category WHERE FatherId=@CategoryId
13 declare @id int
14 while(exists(select top 1 * from @taba))--只要还有数据就继续循环
15 begin #p#
16 select top 1 @id=tabid from @taba--从变量表中取出一条记录
17 insert @taba (tabid)--插入父ID等于@id这条记录的子记录
18 select CategoryId from Category WHERE FatherId=@id
19 delete from Category where CategoryId=@id--从原表中删除该记录
20 delete from @taba where tabid=@id--从变量表中删除该记录,因为已取到它的所有子记录不必要了
21 end
22 DELETE [Category]
23 WHERE CategoryId=@CategoryId
24 ---这段可以适应无限级分类表
表设计: