Code
1通常情况下,我们更多的操作是读取目录,所以,在下面的实现中,读取我们只需要一Select语句就够了,不使用递归,理论上无限级~!
2===================================================
3表结构:
4表名:Tb_Column
5表结构(所有字段非空):
6Column_ID int 主键(注:非标识)
7Column_Name nvarchar(50)分类名称
8Parent_ID int 父分类ID(默认值0)
9Column_Path nvarchar(1000) 分类路径
10Column_Depth int分类深度(默认值0)
11Column_Order int排序(默认值0)
12Column_Intro nvarchar(1000)分类说明
13
14================================================
15存储过程一:新建分类
16
17CREATE PROCEDURE sp_Column_Insert
18(
19@Parent_ID int,
20@Column_Name nvarchar(50),
21@Column_Intro nvarchar(1000)
22)
23AS
24Declare @Err As int
25Set @Err=0
26
27Begin Tran
28--通过现有记录获取栏目ID
29Declare @Column_ID As int
30Declare @Column_Depth As int
31Select @Column_ID = Max(Column_ID) From Tb_Column
32IF @Column_ID Is Not Null
33Set @Column_ID = @Column_ID+1
34Else
35Set @Column_ID = 1
36
37--判断是否是顶级栏目,设置其Column_Path和Column_Order
38Declare @Column_Path As nvarchar(1000)
39Declare @Column_Order As int
40IF @Parent_ID = 0
41Begin
42Set @Column_Path =Ltrim(Str(@Column_ID))
43Select @Column_Order = Max(Column_Order) From Tb_Column
44IF @Column_Order Is Not Null
45Set @Column_Order = @Column_Order + 1
46Else --如果没有查询到记录,说明这是第一条记录
47Set @Column_Order = 1
48
49--深度
50Set @Column_Depth = 1
51End
52Else
53Begin
54--获取父节点的路径和深度
55Select @Column_Path = Column_Path ,@Column_Depth = Column_Depth From Tb_Column Where
56
57Column_ID=@Parent_ID
58IF @Column_Path Is Null
59Begin
60Set @Err = 1
61Goto theEnd
62End
63
64--获取同父节点下的最大序号
65Select @Column_Order = Max(Column_Order) From Tb_PicColumn Where Column_Path like
66
67''+@Column_Path+'|%' Or Column_ID = @Parent_ID
68IF @Column_Order Is Not Null --如果序号存在,那么将该序号后的所有序号都加1
69Begin
70--更新当前要插入节点后所有节点的序号
71Update Tb_Column Set Column_Order = Column_Order +1 Where Column_Order
72
73>@Column_Order
74--同父节点下的最大序号加上1,构成自己的序号
75Set @Column_Order = @Column_Order + 1
76End
77Else
78Begin
79Set @Err=1
80Goto theEnd
81End
82
83--父节点的路径加上自己的ID号,构成自己的路径
84Set @Column_Path = @Column_Path + '|' + Ltrim(Str(@Column_ID))
85
86--深度
87Set @Column_Depth = @Column_Depth+1
88
89End
90
91Insert Into Tb_Column(Column_ID,Column_Name,Parent_ID,Column_Path,Column_Depth,Column_Order,Column_Intro)
92
93Values(@Column_ID,@Column_Name,@Parent_ID,@Column_Path,@Column_Depth,@Column_Order,@Column_Intro)
94
95IF @@Error<>0
96Begin
97Set @Err=1
98Goto theEnd
99End
100
101--更新当前记录之后的记录的ORDER
102--Update Tb_Column Set Column_Order = Column_Order+1 Where Column_Order > @Column_Order
103
104theEnd:
105IF @Err=0
106Begin
107Commit Tran
108Return @Column_ID
109End
110Else
111Begin
112 Rollback Tran
113Return 0
114End
115GO
116
117===================================================
118存储过程二:删除分类
119CREATE PROCEDURE sp_Column_Delete
120(
121@Column_ID int
122)
123AS
124Declare @Err As int
125Set @Err = 0
126Begin Tran
127--首先查询该节点下是否有子节点
128Select Column_ID From Tb_Column Where Parent_ID = @Column_ID
129IF @@RowCount<>0
130 Begin
131 Set @Err = 1
132 Goto theEnd
133 End
134
135--获取该节点的Column_Order,为了删除后整理其他记录的顺序
136Declare @Column_Order As int
137Select @Column_Order = Column_Order From Tb_Column Where Column_ID = @Column_ID
138IF @Column_Order Is NUll
139 Begin
140 Set @Err =2
141 Goto theEnd
142 End
143
144--更新其他记录的Column_Order
145Update Tb_Column Set Column_Order = Column_Order -1 Where Column_Order >@Column_Order
146IF @@Error<>0
147 Begin
148 Set @Err =3
149 Goto theEnd
150 End
151
152--删除操作
153Delete From Tb_Column Where Column_ID=@Column_ID
154IF @@Error<>0
155 Begin
156 Set @Err =4
157 Goto theEnd
158 End
159
160--更新其他记录的Column_ID
161--Update Tb_Column Set Column_ID= Column_ID - 1 Where Column_ID >@Column_ID
162--IF @@Error<>0
163-- Begin
164-- Set @Err =5
165-- Goto theEnd
166-- End
167
168theEnd:
169IF @Err = 0
170 Begin
171 Commit Tran
172 Return 0 --删除成功
173 End
174Else
175 Begin
176 IF @Err=1
177 Begin
178 Rollback Tran
179 Return 1 --有子节点
180End
181 Else
182Begin
183 Rollback Tran
184 Return 2--未知错误
185End
186 End
187GO
188=============================================
189存储过程三:编辑分类
190CREATE PROCEDURE sp_Column_Update
191(
192@Column_ID int,
193@Parent_ID int,
194@Column_Name nvarchar(50),
195@Column_Intro nvarchar(1000)
196)
197AS
198Declare @Err As int
199Set @Err=0
200
201Begin Tran
202
203--获取修改前的:Parent_ID,Column_Depth,Column_Order
204Declare @oParent_ID As int
205Declare @oColumn_Depth As int
206Declare @oColumn_Order As int
207Declare @oColumn_Path As nvarchar(1000)
208
209Select @oParent_ID = Parent_ID, @oColumn_Depth = Column_Depth,@oColumn_Order = Column_Order, @oColumn_Path = Column_Path From Tb_Column Where Column_ID = @Column_ID
210IF @oParent_ID Is Null
211 Begin
212 Set @Err = 1
213 Goto theEnd
214 End
215
216--如果父ID没有改变,则直接修改栏目名和栏目简介
217IF @oParent_ID = @Parent_ID
218 Begin
219 Update Tb_Column Set Column_Name = @Column_Name,Column_Intro = @Column_Intro Where Column_ID = @Column_ID
220 IF @@Error <> 0
221 Set @Err = 2
222 Goto theEnd
223 End
224
225
226Declare @nColumn_Path As nvarchar(1000)
227Declare @nColumn_Depth As int
228Declare @nColumn_Order As int
229
230--获取当前节点作为父节点所包含的节点数[包括自身] 注:如果返回 “1” 说明是单节点
231Declare @theCount As int
232Select @theCount = Count(Column_ID) From Tb_Column Where Column_ID=@Column_ID Or Column_Path like ''+@oColumn_Path+'|%'
233IF @theCount Is Null
234Begin
235 Set @Err = 3
236 Goto theEnd
237End
238
239IF @Parent_ID=0 --如果是设置为顶级节点,将节点设置为最后一个顶级节点
240Begin
241--Print '设置为顶级栏目'
242Set @nColumn_Path = Ltrim(Str(@Column_ID))
243Set @nColumn_Depth =1
244
245Select @nColumn_Order = Max(Column_Order) From Tb_Column
246IF @nColumn_Order Is NULL
247 Begin
248 Set @Err = 4
249 Goto theEnd
250 End
251
252Set @nColumn_Order = @nColumn_Order - @theCount + 1
253
254--更新三部分 1 节点本身 2 所有子节点 2 本树更改之前的后面记录的顺序
255--Print '更新本栏目之前位置后面的所有栏目[不包括本栏目下的子栏目]的:Column_Order'
256Update Tb_Column Set Column_Order = Column_Order-@theCount Where (Column_Order >@oColumn_Order) And (Column_Path Not like ''+@oColumn_Path+'|%')
257IF @@Error <> 0
258 Begin
259 Set @Err = 7
260 Goto theEnd
261 End
262
263--Print '更新本栏目的:Parent_ID,Column_Path,Column_Depth,Column_Order,Column_Name,Column_Intro'
264Print 'Order : '+Ltrim(Str(@nColumn_Order))
265Update Tb_Column Set Parent_ID=@Parent_ID,Column_Path = @nColumn_Path,Column_Depth = @nColumn_Depth,Column_Order = @nColumn_Order, Column_Name = @Column_Name,Column_Intro = @Column_Intro Where Column_ID = @Column_ID
266IF @@Error <> 0
267 Begin
268 Set @Err = 5
269 Goto theEnd
270 End
271
272--Print '更新本栏目下的所有子栏目的:Column_Path,Column_Depth,Column_Order'
273 Update Tb_Column Set Column_Path = Replace(Column_Path,@oColumn_Path,@nColumn_Path),Column_Depth = Column_Depth + (@nColumn_Depth-@oColumn_Depth),Column_Order = Column_Order+( @nColumn_Order-@oColumn_Order) Where Column_Path like ''+@oColumn_Path+'|%'
274IF @@Error <> 0
275 Begin
276 Set @Err = 6
277 Goto theEnd
278 End
279
280
281End
282Else
283Begin
284--获取未来父节点的相关信息,并设置本节点的相关值
285Select @nColumn_Depth = Column_Depth,@nColumn_Path = Column_Path From Tb_Column Where Column_ID = @Parent_ID
286IF @nColumn_Depth Is NULL Or @nColumn_Path Is Null
287 Begin
288 Set @Err = 8
289 Goto theEnd
290 End
291Set @nColumn_Depth = @nColumn_Depth +1
292Select @nColumn_Order =Max(Column_Order) From Tb_Column Where Column_ID = @Parent_ID Or Column_Path like ''+@nColumn_Path+'|%'
293IF @nColumn_Order Is NULL
294 Begin
295 Set @Err = 9
296 Goto theEnd
297 End
298
299Set @nColumn_Path = @nColumn_Path +'|'+ Ltrim(Str(@Column_ID))
300
301IF @nColumn_Order = @oColumn_Order+1 --如果新的父节点是原来位置上端最近一个兄弟,则所有节点的顺序都不改变
302 Begin
303Update Tb_Column Set Parent_ID=@Parent_ID,Column_Path = @nColumn_Path,Column_Depth = @nColumn_Depth, Column_Name = @Column_Name,Column_Intro = @Column_Intro Where Column_ID = @Column_ID
304IF @@Error <> 0
305 Begin
306 Set @Err = 10
307 Goto theEnd
308 End
309 End
310
311Set @nColumn_Order = @nColumn_Order + 1
312
313--更新三部分 1 本树更改之前的后面(或前面)记录的顺序 1 节点本身 3 所有子节点
314--分为向上移或象下移
315--Print '更新本栏目之前位置后面的所有栏目[或者本栏目之后位置] [不包括本栏目下的子栏目]的:Column_Order'
316IF @nColumn_Order < @oColumn_Order
317Begin
318Update Tb_Column Set Column_Order = Column_Order+@theCount Where Column_Order<@oColumn_Order And Column_Order >=@nColumn_Order And (Column_Path Not like ''+@oColumn_Path+'|%') And Column_ID<>@Column_ID
319IF @@Error <> 0
320 Begin
321 Set @Err = 12
322 Goto theEnd
323 End
324End
325Else
326Begin
327Update Tb_Column Set Column_Order = Column_Order-@theCount Where Column_Order >@oColumn_Order And Column_Order<@nColumn_Order And (Column_Path Not like ''+@oColumn_Path+'|%') And Column_ID<>@Column_ID
328IF @@Error <> 0
329 Begin
330 Set @Err = 13
331 Goto theEnd
332 End
333End
334
335--Print '更新本栏目的:Parent_ID,Column_Path,Column_Depth,Column_Order,Column_Name,Column_Intro'
336Print 'Order : '+Ltrim(Str(@nColumn_Order))
337IF @nColumn_Order > @oColumn_Order
338Set @nColumn_Order = @nColumn_Order - @theCount
339Update Tb_Column Set Parent_ID=@Parent_ID,Column_Path = @nColumn_Path,Column_Depth = @nColumn_Depth,Column_Order = @nColumn_Order, Column_Name = @Column_Name,Column_Intro = @Column_Intro Where Column_ID = @Column_ID
340IF @@Error <> 0
341 Begin
342 Set @Err = 10
343 Goto theEnd
344 End
345
346--Print '更新本栏目下的所有子栏目的:Column_Paht,Column_Depth,Column_Order'
347 Update Tb_Column Set Column_Path = Replace(Column_Path,@oColumn_Path,@nColumn_Path),Column_Depth = Column_Depth + (@nColumn_Depth-@oColumn_Depth),Column_Order = Column_Order+(@nColumn_Order-@oColumn_Order) Where Column_Path like ''+@oColumn_Path+'|%'
348IF @@Error <> 0
349 Begin
350 Set @Err = 11
351 Goto theEnd
352 End
353End
354
355theEnd:
356IF @Err<>0 --如果有错误则返回错误号
357 Begin
358 Rollback Tran
359 Return @Err
360 End
361Else --如果没有错误就返回0
362 Begin
363 Commit Tran
364 Return 0
365 End
366GO
367=========================================
368存储过程四:显示分类(只是一条select语句)
369分类列表:
370CREATE PROCEDURE sp_Column_List
371AS
372SELECT Column_ID, Column_Name, Parent_ID, Column_Path, Column_Depth,
373 Column_Order, Column_Intro
374FROM Tb_Column
375ORDER BY Column_Order
376GO
1通常情况下,我们更多的操作是读取目录,所以,在下面的实现中,读取我们只需要一Select语句就够了,不使用递归,理论上无限级~!
2===================================================
3表结构:
4表名:Tb_Column
5表结构(所有字段非空):
6Column_ID int 主键(注:非标识)
7Column_Name nvarchar(50)分类名称
8Parent_ID int 父分类ID(默认值0)
9Column_Path nvarchar(1000) 分类路径
10Column_Depth int分类深度(默认值0)
11Column_Order int排序(默认值0)
12Column_Intro nvarchar(1000)分类说明
13
14================================================
15存储过程一:新建分类
16
17CREATE PROCEDURE sp_Column_Insert
18(
19@Parent_ID int,
20@Column_Name nvarchar(50),
21@Column_Intro nvarchar(1000)
22)
23AS
24Declare @Err As int
25Set @Err=0
26
27Begin Tran
28--通过现有记录获取栏目ID
29Declare @Column_ID As int
30Declare @Column_Depth As int
31Select @Column_ID = Max(Column_ID) From Tb_Column
32IF @Column_ID Is Not Null
33Set @Column_ID = @Column_ID+1
34Else
35Set @Column_ID = 1
36
37--判断是否是顶级栏目,设置其Column_Path和Column_Order
38Declare @Column_Path As nvarchar(1000)
39Declare @Column_Order As int
40IF @Parent_ID = 0
41Begin
42Set @Column_Path =Ltrim(Str(@Column_ID))
43Select @Column_Order = Max(Column_Order) From Tb_Column
44IF @Column_Order Is Not Null
45Set @Column_Order = @Column_Order + 1
46Else --如果没有查询到记录,说明这是第一条记录
47Set @Column_Order = 1
48
49--深度
50Set @Column_Depth = 1
51End
52Else
53Begin
54--获取父节点的路径和深度
55Select @Column_Path = Column_Path ,@Column_Depth = Column_Depth From Tb_Column Where
56
57Column_ID=@Parent_ID
58IF @Column_Path Is Null
59Begin
60Set @Err = 1
61Goto theEnd
62End
63
64--获取同父节点下的最大序号
65Select @Column_Order = Max(Column_Order) From Tb_PicColumn Where Column_Path like
66
67''+@Column_Path+'|%' Or Column_ID = @Parent_ID
68IF @Column_Order Is Not Null --如果序号存在,那么将该序号后的所有序号都加1
69Begin
70--更新当前要插入节点后所有节点的序号
71Update Tb_Column Set Column_Order = Column_Order +1 Where Column_Order
72
73>@Column_Order
74--同父节点下的最大序号加上1,构成自己的序号
75Set @Column_Order = @Column_Order + 1
76End
77Else
78Begin
79Set @Err=1
80Goto theEnd
81End
82
83--父节点的路径加上自己的ID号,构成自己的路径
84Set @Column_Path = @Column_Path + '|' + Ltrim(Str(@Column_ID))
85
86--深度
87Set @Column_Depth = @Column_Depth+1
88
89End
90
91Insert Into Tb_Column(Column_ID,Column_Name,Parent_ID,Column_Path,Column_Depth,Column_Order,Column_Intro)
92
93Values(@Column_ID,@Column_Name,@Parent_ID,@Column_Path,@Column_Depth,@Column_Order,@Column_Intro)
94
95IF @@Error<>0
96Begin
97Set @Err=1
98Goto theEnd
99End
100
101--更新当前记录之后的记录的ORDER
102--Update Tb_Column Set Column_Order = Column_Order+1 Where Column_Order > @Column_Order
103
104theEnd:
105IF @Err=0
106Begin
107Commit Tran
108Return @Column_ID
109End
110Else
111Begin
112 Rollback Tran
113Return 0
114End
115GO
116
117===================================================
118存储过程二:删除分类
119CREATE PROCEDURE sp_Column_Delete
120(
121@Column_ID int
122)
123AS
124Declare @Err As int
125Set @Err = 0
126Begin Tran
127--首先查询该节点下是否有子节点
128Select Column_ID From Tb_Column Where Parent_ID = @Column_ID
129IF @@RowCount<>0
130 Begin
131 Set @Err = 1
132 Goto theEnd
133 End
134
135--获取该节点的Column_Order,为了删除后整理其他记录的顺序
136Declare @Column_Order As int
137Select @Column_Order = Column_Order From Tb_Column Where Column_ID = @Column_ID
138IF @Column_Order Is NUll
139 Begin
140 Set @Err =2
141 Goto theEnd
142 End
143
144--更新其他记录的Column_Order
145Update Tb_Column Set Column_Order = Column_Order -1 Where Column_Order >@Column_Order
146IF @@Error<>0
147 Begin
148 Set @Err =3
149 Goto theEnd
150 End
151
152--删除操作
153Delete From Tb_Column Where Column_ID=@Column_ID
154IF @@Error<>0
155 Begin
156 Set @Err =4
157 Goto theEnd
158 End
159
160--更新其他记录的Column_ID
161--Update Tb_Column Set Column_ID= Column_ID - 1 Where Column_ID >@Column_ID
162--IF @@Error<>0
163-- Begin
164-- Set @Err =5
165-- Goto theEnd
166-- End
167
168theEnd:
169IF @Err = 0
170 Begin
171 Commit Tran
172 Return 0 --删除成功
173 End
174Else
175 Begin
176 IF @Err=1
177 Begin
178 Rollback Tran
179 Return 1 --有子节点
180End
181 Else
182Begin
183 Rollback Tran
184 Return 2--未知错误
185End
186 End
187GO
188=============================================
189存储过程三:编辑分类
190CREATE PROCEDURE sp_Column_Update
191(
192@Column_ID int,
193@Parent_ID int,
194@Column_Name nvarchar(50),
195@Column_Intro nvarchar(1000)
196)
197AS
198Declare @Err As int
199Set @Err=0
200
201Begin Tran
202
203--获取修改前的:Parent_ID,Column_Depth,Column_Order
204Declare @oParent_ID As int
205Declare @oColumn_Depth As int
206Declare @oColumn_Order As int
207Declare @oColumn_Path As nvarchar(1000)
208
209Select @oParent_ID = Parent_ID, @oColumn_Depth = Column_Depth,@oColumn_Order = Column_Order, @oColumn_Path = Column_Path From Tb_Column Where Column_ID = @Column_ID
210IF @oParent_ID Is Null
211 Begin
212 Set @Err = 1
213 Goto theEnd
214 End
215
216--如果父ID没有改变,则直接修改栏目名和栏目简介
217IF @oParent_ID = @Parent_ID
218 Begin
219 Update Tb_Column Set Column_Name = @Column_Name,Column_Intro = @Column_Intro Where Column_ID = @Column_ID
220 IF @@Error <> 0
221 Set @Err = 2
222 Goto theEnd
223 End
224
225
226Declare @nColumn_Path As nvarchar(1000)
227Declare @nColumn_Depth As int
228Declare @nColumn_Order As int
229
230--获取当前节点作为父节点所包含的节点数[包括自身] 注:如果返回 “1” 说明是单节点
231Declare @theCount As int
232Select @theCount = Count(Column_ID) From Tb_Column Where Column_ID=@Column_ID Or Column_Path like ''+@oColumn_Path+'|%'
233IF @theCount Is Null
234Begin
235 Set @Err = 3
236 Goto theEnd
237End
238
239IF @Parent_ID=0 --如果是设置为顶级节点,将节点设置为最后一个顶级节点
240Begin
241--Print '设置为顶级栏目'
242Set @nColumn_Path = Ltrim(Str(@Column_ID))
243Set @nColumn_Depth =1
244
245Select @nColumn_Order = Max(Column_Order) From Tb_Column
246IF @nColumn_Order Is NULL
247 Begin
248 Set @Err = 4
249 Goto theEnd
250 End
251
252Set @nColumn_Order = @nColumn_Order - @theCount + 1
253
254--更新三部分 1 节点本身 2 所有子节点 2 本树更改之前的后面记录的顺序
255--Print '更新本栏目之前位置后面的所有栏目[不包括本栏目下的子栏目]的:Column_Order'
256Update Tb_Column Set Column_Order = Column_Order-@theCount Where (Column_Order >@oColumn_Order) And (Column_Path Not like ''+@oColumn_Path+'|%')
257IF @@Error <> 0
258 Begin
259 Set @Err = 7
260 Goto theEnd
261 End
262
263--Print '更新本栏目的:Parent_ID,Column_Path,Column_Depth,Column_Order,Column_Name,Column_Intro'
264Print 'Order : '+Ltrim(Str(@nColumn_Order))
265Update Tb_Column Set Parent_ID=@Parent_ID,Column_Path = @nColumn_Path,Column_Depth = @nColumn_Depth,Column_Order = @nColumn_Order, Column_Name = @Column_Name,Column_Intro = @Column_Intro Where Column_ID = @Column_ID
266IF @@Error <> 0
267 Begin
268 Set @Err = 5
269 Goto theEnd
270 End
271
272--Print '更新本栏目下的所有子栏目的:Column_Path,Column_Depth,Column_Order'
273 Update Tb_Column Set Column_Path = Replace(Column_Path,@oColumn_Path,@nColumn_Path),Column_Depth = Column_Depth + (@nColumn_Depth-@oColumn_Depth),Column_Order = Column_Order+( @nColumn_Order-@oColumn_Order) Where Column_Path like ''+@oColumn_Path+'|%'
274IF @@Error <> 0
275 Begin
276 Set @Err = 6
277 Goto theEnd
278 End
279
280
281End
282Else
283Begin
284--获取未来父节点的相关信息,并设置本节点的相关值
285Select @nColumn_Depth = Column_Depth,@nColumn_Path = Column_Path From Tb_Column Where Column_ID = @Parent_ID
286IF @nColumn_Depth Is NULL Or @nColumn_Path Is Null
287 Begin
288 Set @Err = 8
289 Goto theEnd
290 End
291Set @nColumn_Depth = @nColumn_Depth +1
292Select @nColumn_Order =Max(Column_Order) From Tb_Column Where Column_ID = @Parent_ID Or Column_Path like ''+@nColumn_Path+'|%'
293IF @nColumn_Order Is NULL
294 Begin
295 Set @Err = 9
296 Goto theEnd
297 End
298
299Set @nColumn_Path = @nColumn_Path +'|'+ Ltrim(Str(@Column_ID))
300
301IF @nColumn_Order = @oColumn_Order+1 --如果新的父节点是原来位置上端最近一个兄弟,则所有节点的顺序都不改变
302 Begin
303Update Tb_Column Set Parent_ID=@Parent_ID,Column_Path = @nColumn_Path,Column_Depth = @nColumn_Depth, Column_Name = @Column_Name,Column_Intro = @Column_Intro Where Column_ID = @Column_ID
304IF @@Error <> 0
305 Begin
306 Set @Err = 10
307 Goto theEnd
308 End
309 End
310
311Set @nColumn_Order = @nColumn_Order + 1
312
313--更新三部分 1 本树更改之前的后面(或前面)记录的顺序 1 节点本身 3 所有子节点
314--分为向上移或象下移
315--Print '更新本栏目之前位置后面的所有栏目[或者本栏目之后位置] [不包括本栏目下的子栏目]的:Column_Order'
316IF @nColumn_Order < @oColumn_Order
317Begin
318Update Tb_Column Set Column_Order = Column_Order+@theCount Where Column_Order<@oColumn_Order And Column_Order >=@nColumn_Order And (Column_Path Not like ''+@oColumn_Path+'|%') And Column_ID<>@Column_ID
319IF @@Error <> 0
320 Begin
321 Set @Err = 12
322 Goto theEnd
323 End
324End
325Else
326Begin
327Update Tb_Column Set Column_Order = Column_Order-@theCount Where Column_Order >@oColumn_Order And Column_Order<@nColumn_Order And (Column_Path Not like ''+@oColumn_Path+'|%') And Column_ID<>@Column_ID
328IF @@Error <> 0
329 Begin
330 Set @Err = 13
331 Goto theEnd
332 End
333End
334
335--Print '更新本栏目的:Parent_ID,Column_Path,Column_Depth,Column_Order,Column_Name,Column_Intro'
336Print 'Order : '+Ltrim(Str(@nColumn_Order))
337IF @nColumn_Order > @oColumn_Order
338Set @nColumn_Order = @nColumn_Order - @theCount
339Update Tb_Column Set Parent_ID=@Parent_ID,Column_Path = @nColumn_Path,Column_Depth = @nColumn_Depth,Column_Order = @nColumn_Order, Column_Name = @Column_Name,Column_Intro = @Column_Intro Where Column_ID = @Column_ID
340IF @@Error <> 0
341 Begin
342 Set @Err = 10
343 Goto theEnd
344 End
345
346--Print '更新本栏目下的所有子栏目的:Column_Paht,Column_Depth,Column_Order'
347 Update Tb_Column Set Column_Path = Replace(Column_Path,@oColumn_Path,@nColumn_Path),Column_Depth = Column_Depth + (@nColumn_Depth-@oColumn_Depth),Column_Order = Column_Order+(@nColumn_Order-@oColumn_Order) Where Column_Path like ''+@oColumn_Path+'|%'
348IF @@Error <> 0
349 Begin
350 Set @Err = 11
351 Goto theEnd
352 End
353End
354
355theEnd:
356IF @Err<>0 --如果有错误则返回错误号
357 Begin
358 Rollback Tran
359 Return @Err
360 End
361Else --如果没有错误就返回0
362 Begin
363 Commit Tran
364 Return 0
365 End
366GO
367=========================================
368存储过程四:显示分类(只是一条select语句)
369分类列表:
370CREATE PROCEDURE sp_Column_List
371AS
372SELECT Column_ID, Column_Name, Parent_ID, Column_Path, Column_Depth,
373 Column_Order, Column_Intro
374FROM Tb_Column
375ORDER BY Column_Order
376GO