zoukankan      html  css  js  c++  java
  • 无限极分类

    CREATE PROCEDURE [dbo].[GoodsCategories_Insert]

     @parentId int, --父类Id
     @className nvarchar(50) --分类名称

    AS
    BEGIN
     SET NOCOUNT ON; --不返回计数(表示受 Transact-SQL 语句影响的行数)
     Declare @RootID int;     --根ID(顶级分类的RootID)
     DeClare @ParentName nvarchar(500);  --父类名称
     Declare @Depth int;      --父类深度
     Declare @MaxOrders int;     --同级最大排序号
     Declare @ParentIDPath nvarchar(500);  --父类Id全路径
     Declare @ParentNamePath nvarchar(500); --父类名称全路径
     declare @newId int;

     select @newId= isnull(max(ID),0) from  [dbo].[GoodsCategories]
     set @newId=@newId+1;
     --如果是顶级类
     if @parentId=0
     begin
      if not exists(select ID From GoodsCategories where ClassName=@className  and ParentID=0)  
      begin
       set @RootID = 0   
       if exists(select ID From GoodsCategories Where  ParentID=0)
       begin
        select @RootID = max(RootID) From GoodsCategories Where  ParentID=0;--得到当前顶级分类的最大RootID    
       end
            
       insert into GoodsCategories(
        ID,

        ParentID,
        ClassName,
     
        ParentIDPath,
        ParentNamePath,
        RootID,
        Orders)
       values(
        @newId,
     
        '0',
        @className,
     
        '',
        '',
        @RootID+1,
        0)
       return 0;--顶级分类成功插入
      end
      else
       return 1;--顶级分类已经存在
     end

     --如果不是顶级类
     if @parentId<>0   
     begin
      --检查父类ID的合法性
      if exists(select ID From GoodsCategories Where ID=@parentID)  
      begin
       --检查该节点是否已经存在  
       if not exists(select ID From GoodsCategories where ClassName=@className and ParentID=@parentId)   
       begin      
        select @RootID=RootID,@ParentName=ClassName,@Depth=Depth,@ParentIDPath=ParentIDPath,@ParentNamePath=ParentNamePath From GoodsCategories Where ID = @ParentId;
        
        
        
            
        set @maxOrders = 0


        

        --如果父类无子类
        if not exists(select ID From GoodsCategories where ParentIDPath like '%' + Convert(Nvarchar(50),@parentId) + '%')     
         select @maxOrders = Orders From GoodsCategories where ID=@parentId    
        else
         select @maxOrders = Max(Orders) From GoodsCategories where ParentIDPath like '%' + Convert(Nvarchar(50),@parentId) + '%'         
               
            
        

               --if (@ParentIDPath='')
               --begin
               --set @ParentIDPath= convert(nvarchar(50),@parentID);
               --end
               --else
               --set @ParentIDPath=   @ParentIDPath + ',' + convert(nvarchar(50),@parentID);
               --end

               --if (@ParentNamePath='')
               --begin
               --set @ParentNamePath=@parentName;
               --end
               --else
               --set @ParentNamePath=@ParentNamePath + ',' + @parentName;
               --end

        insert into GoodsCategories(
         ID,
     
         ParentID,
         ClassName,
     
         ParentIDPath,
         ParentNamePath,
         Orders,
         Depth,
         RootID  
         )
        values(
         @newId,
     
         @parentId,
         @className,
     
      @ParentIDPath + ',' + convert(nvarchar(50),@parentID)+',',
        @ParentNamePath + ',' + @parentName+',',
         @maxOrders + 1,
         @depth + 1,
         @RootID
         )
     
        update GoodsCategories set Orders = Orders + 1 where (Orders > @maxOrders) And (RootID=@RootID) And ID<>@NewId
     
        
        return 0;  
       end
       else
        return 1;
      end
     end
    end


    GO

  • 相关阅读:
    [zz]redhat6.0无法识别ntfs分区的解决方法
    使用ftp搭建yum源问题解决
    [zz]搭建centos6.0本地yum源(32位)
    JAVA传统线程技术
    JAVA判断字符串是否为数字
    java之异常
    随便记两笔Java中的反射
    【转】单例模式完全解析
    java.lang.Enum
    文件搜索
  • 原文地址:https://www.cnblogs.com/zhangweixin/p/3934298.html
Copyright © 2011-2022 走看看