当前位置: 代码迷 >> Sql Server >> 存储过程 树形查询?该怎么解决
  详细解决方案

存储过程 树形查询?该怎么解决

热度:71   发布时间:2016-04-24 10:32:18.0
存储过程 树形查询???
public void modifyCategoryName(MaterialCategory materialCate) {
        MaterialCategory parentMaterialCategory = materialCate.getParent();
        if (parentMaterialCategory == null) {
            materialCate.setCategoryName(materialCate.getName());
        } else {
            StringBuilder categoryName = new StringBuilder("").append(parentMaterialCategory.getCategoryName())
               .append("-").append(materialCate.getName()); 
            materialCate.setCategoryName(categoryName.toString()); 
        }
        this.materialCategoryDao.update(materialCate);  
        List<MaterialCategory> subList = this.listSubMaterialCategory(materialCate.getId());
        if (subList != null) {
            for (MaterialCategory matCategory : subList) {
                this.modifyCategoryName(matCategory);
            }           
        } else {
            return;
        }
    }

什么代码写的存储过程
create or replace package body material_mgmt is

 procedure modify_material_category_name(categoryId number) is
           parentCategoryName varchar2(256);
           parentId number(19);
           cateName varchar2(64);
           categoryName varchar2(256);
  begin
    select t.parentid, t.name into parentId, cateName from t_materialcategory t where t.id = categoryId;
    if parentId is null then 
       update t_materialcategory t set t.categoryname = cateName where t.id = categoryId;
    else
       select t.categoryname into parentCategoryName from t_materialcategory t where t.id = parentId;
       categoryName := parentCategoryName ||'-'|| cateName;
       update t_materialcategory t set t.categoryname = categoryName where t.id = categoryId;
    end if;
    
    update t_materialcategory t set t.categoryname = (
      select p.categoryname ||'-'||t.name from t_materialcategory p where p.id = t.parentid
    ) where t.id in (select distinct o.id from t_materialcategory o start with o.id = :categoryId connect by prior o.id = o.parentid);
  end  

遇到一个问题?
select distinct o.id from t_materialcategory o start with o.id = :categoryId connect by prior o.id = o.parentid
是树形查询,,但查询结果包括自己本身和他所有子孙。。现在要的结果是不包括自己本身!!
加where o.id <>:categoryId  但是没有查询结果了??树形查询后可以加where吗???
------解决方案--------------------
用union all把自己包含进去就可以了
  相关解决方案