Qunicy 2019-04-03
代码如下:
CREATE proc [dbo].[Category_Add] @CategoryName nvarchar(50), @BindCategoryID int, @CategoryID int output as declare @Success bit set @Success=1 --生成不重复的CategoryID declare @i bit set @i=0 while @i=0 begin set @CategoryID=LEFT(10000000 + CONVERT(bigint, ABS(CHECKSUM(NEWID()))), 8) if(not exists(select CategoryID from tomi_Category where CategoryID=@CategoryID)) set @i=1 end --得到depth declare @depth int set @depth=0 select @depth=depth from tomi_Category where CategoryID=@BindCategoryID set @depth=@depth+1 --插入 BEGIN TRAN insert into tomi_Category(categoryID,CategoryName,Depth) values(@CategoryID,@CategoryName,@Depth) if(@@ERROR<>0) BEGIN ROLLBACK TRAN set @Success=0 END insert into tomi_CategoryBind(CategoryID,BindCategoryID,Depth) values(@CategoryID,@CategoryID,@Depth) if(@@ERROR<>0) BEGIN ROLLBACK TRAN set @Success=0 END insert into tomi_CategoryBind(CategoryID,BindCategoryID,Depth) select @CategoryID,BindCategoryID,Depth from tomi_CategoryBind where CategoryID=@BindCategoryID if(@@ERROR<>0) BEGIN ROLLBACK TRAN set @Success=0 END COMMIT TRAN print @CategoryID
代码如下:
CREATE proc [dbo].[Category_Edit] @CategoryID int, @CategoryName nvarchar(50), @BindCategoryID int as --更新 BEGIN TRAN update tomi_Category set CategoryName=@CategoryName where CategoryID=@CategoryID IF @@ERROR<>0 BEGIN ROLLBACK TRAN return 0 END COMMIT TRAN --检测是否更改了上级目录 declare @is bit set @is=0 if(exists(select CategoryID from tomi_CategoryBind where CategoryID=@CategoryID and BindCategoryID=@BindCategoryID and Depth=(select Depth-1 from tomi_Category where CategoryID=@CategoryID))) set @is=1 print @is --更改了深度 if(@is=0) BEGIN --得到上级目录的depth declare @depth int set @depth=0 select @depth=depth from tomi_Category where CategoryID=@BindCategoryID set @depth=@depth+1 --print @depth --更改子目录 declare @i int declare @sCategoryID int declare @sBindCategoryID int declare @tCategoryIDList Table ( CategoryID int, FlagID tinyint ) insert @tCategoryIDList select c.CategoryID,0 from tomi_Category c left join tomi_CategoryBind b on c.CategoryID=b.CategoryID where b.BindCategoryID=@CategoryID order by c.Depth set @i=1 set @sBindCategoryID=@BindCategoryID declare @errs int set @errs=0 BEGIN TRAN while(@i>=1) BEGIN select @sCategoryID=0 select Top 1 @sCategoryID=CategoryID from @tCategoryIDList where FlagID=0 set @i=@@RowCount --print @sCategoryID if @sCategoryID>0 BEGIN --删除,更新 delete from tomi_CategoryBind where CategoryID=@sCategoryID set @errs=@errs+@@error update tomi_Category set depth=@depth where CategoryID=@sCategoryID set @errs=@errs+@@error --插入 insert into tomi_CategoryBind(CategoryID,BindCategoryID,Depth) values(@sCategoryID,@sCategoryID,@Depth) set @errs=@errs+@@error insert into tomi_CategoryBind(CategoryID,BindCategoryID,Depth) select @sCategoryID,BindCategoryID,Depth from tomi_CategoryBind where CategoryID=@sBindCategoryID set @errs=@errs+@@error set @sBindCategoryID=@sCategoryID set @Depth=@Depth+1 --print @sCategoryID --print @sBindCategoryID --print @Depth --print '--' END update @tCategoryIDList set FlagID=1 where CategoryID=@sCategoryID END if(@errs>0) BEGIN ROLLBACK TRAN return 0 END else COMMIT TRAN END
代码如下:
create proc Category_Del @CategoryID int as BEGIN TRAN delete from tomi_Category where CategoryID in (select CategoryID from tomi_CategoryBind where CategoryID=@CategoryID or BindCategoryID=@CategoryID) if(@@ERROR<>0) BEGIN ROLLBACK TRAN return 0 END delete from tomi_CategoryBind where CategoryID in (select CategoryID from tomi_CategoryBind where CategoryID=@CategoryID or BindCategoryID=@CategoryID) if(@@ERROR<>0) BEGIN ROLLBACK TRAN return 0 END COMMIT TRAN
代码如下:
CREATE proc Category_List as select c.* from tomi_Category c left join tomi_CategoryBind b on c.CategoryID=b.CategoryID where b.Depth=1 order by b.BindCategoryID,c.Depth GO
代码如下:
Create Proc Category_UpTree @CategoryID int as select c.* from tomi_Category c left join tomi_CategoryBind b on c.CategoryID=b.BindCategoryID where b.CategoryID=@CategoryID order by c.Depth GO
代码如下:
Create Proc Category_DownTree @CategoryID int as select c.* from tomi_Category c left join tomi_CategoryBind b on c.CategoryID=b.CategoryID where b.BindCategoryID=@CategoryID order by c.Depth GO