|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
其实Java之所以在曾经独步天下,就是因为他的跨平台、安全性,这两方面,效率可不是Java的强项,反而是他最短的一块挡板,虽然net总是用理论证明比.NET快。asp.net|存储历程|递回或多或少都做过树状目次、产物分类之类的二级或三级菜单,假如碰到更多级的分类,就一样平常利用递回了。在程序中利用递回或多或少会增添一些功能上的开支。
之前我用ASP.net在程序中完成过非递回的无穷级分类目次,但思索到移植性不强,就改成了存储历程,收回来人人配合研讨一下,到今朝为止,测试过程当中还没发明成绩,别的,代码方面没经由甚么优化。
一般情形下,我们更多的操纵是读取目次,以是,鄙人面的完成中,读取我们只必要一Select语句就够了,不利用递回,实际上无穷级~!
===================================================
表布局:
表名:Tb_Column
表布局(一切字段非空):
Column_IDint主键(注:非标识)
Column_Namenvarchar(50)分类称号
Parent_IDint父分类ID(默许值0)
Column_Pathnvarchar(1000)分类路径
Column_Depthint分类深度(默许值0)
Column_Orderint排序(默许值0)
Column_Intronvarchar(1000)分类申明
================================================
存储历程一:新建分类
CREATEPROCEDUREsp_Column_Insert
(
@Parent_IDint,
@Column_Namenvarchar(50),
@Column_Intronvarchar(1000)
)
AS
Declare@ErrAsint
Set@Err=0
BeginTran
--经由过程现有纪录猎取栏目ID
Declare@Column_IDAsint
Declare@Column_DepthAsint
Select@Column_ID=Max(Column_ID)FromTb_Column
IF@Column_IDIsNotNull
Set@Column_ID=@Column_ID+1
Else
Set@Column_ID=1
--判别是不是是顶级栏目,设置其Column_Path和Column_Order
Declare@Column_PathAsnvarchar(1000)
Declare@Column_OrderAsint
IF@Parent_ID=0
Begin
Set@Column_Path=Ltrim(Str(@Column_ID))
Select@Column_Order=Max(Column_Order)FromTb_Column
IF@Column_OrderIsNotNull
Set@Column_Order=@Column_Order+1
Else--假如没有查询到纪录,申明这是第一笔记录
Set@Column_Order=1
--深度
Set@Column_Depth=1
End
Else
Begin
--猎取父节点的路径和深度
Select@Column_Path=Column_Path,@Column_Depth=Column_DepthFromTb_ColumnWhere
Column_ID=@Parent_ID
IF@Column_PathIsNull
Begin
Set@Err=1
GototheEnd
End
--猎取同父节点下的最年夜序号
Select@Column_Order=Max(Column_Order)FromTb_PicColumnWhereColumn_Pathlike
+@Column_Path+|%OrColumn_ID=@Parent_ID
IF@Column_OrderIsNotNull--假如序号存在,那末将该序号后的一切序号都加1
Begin
--更新以后要拔出节点后一切节点的序号
UpdateTb_ColumnSetColumn_Order=Column_Order+1WhereColumn_Order
>@Column_Order
--同父节点下的最年夜序号加上1,组成本人的序号
Set@Column_Order=@Column_Order+1
End
Else
Begin
Set@Err=1
GototheEnd
End
--父节点的路径加上本人的ID号,组成本人的路径
Set@Column_Path=@Column_Path+|+Ltrim(Str(@Column_ID))
--深度
Set@Column_Depth=@Column_Depth+1
End
InsertIntoTb_Column(Column_ID,Column_Name,Parent_ID,Column_Path,Column_Depth,Column_Order,Column_Intro)
Values(@Column_ID,@Column_Name,@Parent_ID,@Column_Path,@Column_Depth,@Column_Order,@Column_Intro)
IF@@Error0
Begin
Set@Err=1
GototheEnd
End
--更新以后纪录以后的纪录的ORDER
--UpdateTb_ColumnSetColumn_Order=Column_Order+1WhereColumn_Order>@Column_Order
theEnd:
IF@Err=0
Begin
CommitTran
Return@Column_ID
End
Else
Begin
RollbackTran
Return0
End
GO
===================================================
存储历程二:删除分类
CREATEPROCEDUREsp_Column_Delete
(
@Column_IDint
)
AS
Declare@ErrAsint
Set@Err=0
BeginTran
--起首查询该节点下是不是有子节点
SelectColumn_IDFromTb_ColumnWhereParent_ID=@Column_ID
IF@@RowCount0
Begin
Set@Err=1
GototheEnd
End
--猎取该节点的Column_Order,为了删除后收拾其他纪录的按次
Declare@Column_OrderAsint
Select@Column_Order=Column_OrderFromTb_ColumnWhereColumn_ID=@Column_ID
IF@Column_OrderIsNUll
Begin
Set@Err=2
GototheEnd
End
--更新其他纪录的Column_Order
UpdateTb_ColumnSetColumn_Order=Column_Order-1WhereColumn_Order>@Column_Order
IF@@Error0
Begin
Set@Err=3
GototheEnd
End
--删除操纵
DeleteFromTb_ColumnWhereColumn_ID=@Column_ID
IF@@Error0
Begin
Set@Err=4
GototheEnd
End
--更新其他纪录的Column_ID
--UpdateTb_ColumnSetColumn_ID=Column_ID-1WhereColumn_ID>@Column_ID
--IF@@Error0
--Begin
--Set@Err=5
--GototheEnd
--End
theEnd:
IF@Err=0
Begin
CommitTran
Return0--删除乐成
End
Else
Begin
IF@Err=1
Begin
RollbackTran
Return1--有子节点
End
Else
Begin
RollbackTran
Return2--未知毛病
End
End
GO
=============================================
存储历程三:编纂分类
CREATEPROCEDUREsp_Column_Update
(
@Column_IDint,
@Parent_IDint,
@Column_Namenvarchar(50),
@Column_Intronvarchar(1000)
)
AS
Declare@ErrAsint
Set@Err=0
BeginTran
--猎取修正前的:Parent_ID,Column_Depth,Column_Order
Declare@oParent_IDAsint
Declare@oColumn_DepthAsint
Declare@oColumn_OrderAsint
Declare@oColumn_PathAsnvarchar(1000)
Select@oParent_ID=Parent_ID,@oColumn_Depth=Column_Depth,@oColumn_Order=Column_Order,@oColumn_Path=Column_PathFromTb_ColumnWhereColumn_ID=@Column_ID
IF@oParent_IDIsNull
Begin
Set@Err=1
GototheEnd
End
--假如父ID没有改动,则间接修正栏目名和栏目简介
IF@oParent_ID=@Parent_ID
Begin
UpdateTb_ColumnSetColumn_Name=@Column_Name,Column_Intro=@Column_IntroWhereColumn_ID=@Column_ID
IF@@Error0
Set@Err=2
GototheEnd
End
Declare@nColumn_PathAsnvarchar(1000)
Declare@nColumn_DepthAsint
Declare@nColumn_OrderAsint
--猎取以后节点作为父节点所包括的节点数[包含本身]注:假如前往“1”申明是单节点
Declare@theCountAsint
Select@theCount=Count(Column_ID)FromTb_ColumnWhereColumn_ID=@Column_IDOrColumn_Pathlike+@oColumn_Path+|%
IF@theCountIsNull
Begin
Set@Err=3
GototheEnd
End
IF@Parent_ID=0--假如是设置为顶级节点,将节点设置为最初一个顶级节点
Begin
--Print设置为顶级栏目
Set@nColumn_Path=Ltrim(Str(@Column_ID))
Set@nColumn_Depth=1
Select@nColumn_Order=Max(Column_Order)FromTb_Column
IF@nColumn_OrderIsNULL
Begin
Set@Err=4
GototheEnd
End
Set@nColumn_Order=@nColumn_Order-@theCount+1
--更新三部分1节点自己2一切子节点2本树变动之前的前面纪录的按次
--Print更新本栏目之前地位前面的一切栏目[不包含本栏面前目今的子栏目]的:Column_Order
UpdateTb_ColumnSetColumn_Order=Column_Order-@theCountWhere(Column_Order>@oColumn_Order)And(Column_PathNotlike+@oColumn_Path+|%)
IF@@Error0
Begin
Set@Err=7
GototheEnd
End
--Print更新本栏目标:Parent_ID,Column_Path,Column_Depth,Column_Order,Column_Name,Column_Intro
PrintOrder:+Ltrim(Str(@nColumn_Order))
UpdateTb_ColumnSetParent_ID=@Parent_ID,Column_Path=@nColumn_Path,Column_Depth=@nColumn_Depth,Column_Order=@nColumn_Order,Column_Name=@Column_Name,Column_Intro=@Column_IntroWhereColumn_ID=@Column_ID
IF@@Error0
Begin
Set@Err=5
GototheEnd
End
--Print更新本栏面前目今的一切子栏目标:Column_Path,Column_Depth,Column_Order
UpdateTb_ColumnSetColumn_Path=Replace(Column_Path,@oColumn_Path,@nColumn_Path),Column_Depth=Column_Depth+(@nColumn_Depth-@oColumn_Depth),Column_Order=Column_Order+(@nColumn_Order-@oColumn_Order)WhereColumn_Pathlike+@oColumn_Path+|%
IF@@Error0
Begin
Set@Err=6
GototheEnd
End
End
Else
Begin
--猎取将来父节点的相干信息,并设置本节点的相干值
Select@nColumn_Depth=Column_Depth,@nColumn_Path=Column_PathFromTb_ColumnWhereColumn_ID=@Parent_ID
IF@nColumn_DepthIsNULLOr@nColumn_PathIsNull
Begin
Set@Err=8
GototheEnd
End
Set@nColumn_Depth=@nColumn_Depth+1
Select@nColumn_Order=Max(Column_Order)FromTb_ColumnWhereColumn_ID=@Parent_IDOrColumn_Pathlike+@nColumn_Path+|%
IF@nColumn_OrderIsNULL
Begin
Set@Err=9
GototheEnd
End
Set@nColumn_Path=@nColumn_Path+|+Ltrim(Str(@Column_ID))
IF@nColumn_Order=@oColumn_Order+1--假如新的父节点是本来地位上端比来一个兄弟,则一切节点的按次都不改动
Begin
UpdateTb_ColumnSetParent_ID=@Parent_ID,Column_Path=@nColumn_Path,Column_Depth=@nColumn_Depth,Column_Name=@Column_Name,Column_Intro=@Column_IntroWhereColumn_ID=@Column_ID
IF@@Error0
Begin
Set@Err=10
GototheEnd
End
End
Set@nColumn_Order=@nColumn_Order+1
--更新三部分1本树变动之前的前面(或后面)纪录的按次1节点自己3一切子节点
--分为向上移或象下移
--Print更新本栏目之前地位前面的一切栏目[大概本栏目以后地位][不包含本栏面前目今的子栏目]的:Column_Order
IF@nColumn_Order<@oColumn_Order
Begin
UpdateTb_ColumnSetColumn_Order=Column_Order+@theCountWhereColumn_Order<@oColumn_OrderAndColumn_Order>=@nColumn_OrderAnd(Column_PathNotlike+@oColumn_Path+|%)AndColumn_ID@Column_ID
IF@@Error0
Begin
Set@Err=12
GototheEnd
End
End
Else
Begin
UpdateTb_ColumnSetColumn_Order=Column_Order-@theCountWhereColumn_Order>@oColumn_OrderAndColumn_Order<@nColumn_OrderAnd(Column_PathNotlike+@oColumn_Path+|%)AndColumn_ID@Column_ID
IF@@Error0
Begin
Set@Err=13
GototheEnd
End
End
--Print更新本栏目标:Parent_ID,Column_Path,Column_Depth,Column_Order,Column_Name,Column_Intro
PrintOrder:+Ltrim(Str(@nColumn_Order))
IF@nColumn_Order>@oColumn_Order
Set@nColumn_Order=@nColumn_Order-@theCount
UpdateTb_ColumnSetParent_ID=@Parent_ID,Column_Path=@nColumn_Path,Column_Depth=@nColumn_Depth,Column_Order=@nColumn_Order,Column_Name=@Column_Name,Column_Intro=@Column_IntroWhereColumn_ID=@Column_ID
IF@@Error0
Begin
Set@Err=10
GototheEnd
End
--Print更新本栏面前目今的一切子栏目标:Column_Paht,Column_Depth,Column_Order
UpdateTb_ColumnSetColumn_Path=Replace(Column_Path,@oColumn_Path,@nColumn_Path),Column_Depth=Column_Depth+(@nColumn_Depth-@oColumn_Depth),Column_Order=Column_Order+(@nColumn_Order-@oColumn_Order)WhereColumn_Pathlike+@oColumn_Path+|%
IF@@Error0
Begin
Set@Err=11
GototheEnd
End
End
theEnd:
IF@Err0--假如有毛病则前往毛病号
Begin
RollbackTran
Return@Err
End
Else--假如没有毛病就前往0
Begin
CommitTran
Return0
End
GO
=========================================
存储历程四:显现分类(只是一条select语句)
分类列表:
CREATEPROCEDUREsp_Column_List
AS
SELECTColumn_ID,Column_Name,Parent_ID,Column_Path,Column_Depth,
Column_Order,Column_Intro
FROMTb_Column
ORDERBYColumn_Order
GO
=======================================
你觉得学习.NET怎么样,我懂的少,问的可能很幼稚,见笑了啊:) |
|