create
procedure
[dbo].[category_delete]
@categoryid
int
as
--delete [category]
-- where categoryid=@categoryid
--delete [category]
-- where fatherid=@categoryid
declare
@taba
table
([tabid] [
int
]
not
null
)
insert
@taba (tabid)
select
categoryid
from
category
where
fatherid=@categoryid
declare
@id
int
while(exists(
select
top
1 *
from
@taba))
--只要還有資料就繼續迴圈
begin
select
top
1 @id=tabid
from
@taba
--從變數表中取出一條記錄
insert
@taba (tabid)
--插入父id等於@id這條記錄的子記錄
select
categoryid
from
category
where
fatherid=@id
delete
from
category
where
categoryid=@id
--從原表中刪除該記錄
delete
from
@taba
where
tabid=@id
--從變數表中刪除該記錄,因為已取到它的所有子記錄不必要了
end
delete
[category]
where
categoryid=@categoryid
create
procedure
[dbo].[category_delete]
@categoryid
int
as
--delete [category]
-- where categoryid=@categoryid
--delete [category]
-- where fatherid=@categoryid
declare
@taba
table
([tabid] [
int
]
not
null
)
insert
@taba (tabid)
select
categoryid
from
category
where
fatherid=@categoryid
declare
@id
int
while(exists(
select
top
1 *
from
@taba))
--只要還有資料就繼續迴圈
begin
select
top
1 @id=tabid
from
@taba
--從變數表中取出一條記錄
insert
@taba (tabid)
--插入父id等於@id這條記錄的子記錄
select
categoryid
from
category
where
fatherid=@id
delete
from
category
where
categoryid=@id
--從原表中刪除該記錄
delete
from
@taba
where
tabid=@id
--從變數表中刪除該記錄,因為已取到它的所有子記錄不必要了
end
delete
[category]
where
categoryid=@categoryid
刪除無限極分類的sql儲存過程!
create procedure dbo category delete categoryid int as delete category where categoryid categoryid delete category where fatherid categoryid declare t...
無限極分類
面試的時候被問到無限極分類的設計和實現,比較常見的做法是在建表的時候,增加乙個pid欄位用來區別自己所屬的分類 array array array id 1,pid 0,name 河北省 array id 2,pid 0,name 北京市 array id 3,pid 1,name 邯鄲市 arra...
無限極分類,遞迴分類
有一組陣列如下 items array 1 array id 1,pid 0,name 江西省 2 array id 2,pid 0,name 黑龍江省 3 array id 3,pid 1,name 南昌市 4 array id 4,pid 2,name 哈爾濱市 5 array id 5,pid...