zoukankan      html  css  js  c++  java
  • sqlserver 父子级查询(理念适应所有数据库)

     实现技术: 存储过程   ,零时表(3)

    一句话说完 :把父级查询下来的子级ID 保存成零时表,并且将符合子级ID数据添加到另一张零时表。

           同时清空数据时需要使用到一张零时表作为容器;


    alter PROCEDURE sel_sum
    @a int  
    AS
    BEGIN

    select  id,nodename ,pid  into #fz from treenodes  where  id=@a--创建零时表,并且将最上级id添加记录
    select id into  #array from treenodes  where  pid=@a --创建子id零时表,并且将子id添加到子id记录表
    select id into  #array2 from treenodes  where  pid=@a  --子id 临时表二

    while exists  ( select top(1) *  from #array) --如果下一级存在存在
    begin
    --将子级数据插入零时表  
    insert  into  #fz(id,nodename ,pid)   select  id,nodename ,pid
       from treenodes
        where  id  in (
      select  id from  #array  )
     
    delete #array2 ;   --清空零时表2
    insert     into  #array2  select * from   #array ;
     
    delete #array ; --清空
         --查询子级的下一级赋值给#array零时表   
    insert    into  #array  
    select id from   treenodes where pid  in(
        select  id from  #array2
       );
       
    end  

    select  * from #fz order by id
    end
     

    mysql  ,Oracle  原理相同

  • 相关阅读:
    Metropolis-Hastings algorithm
    Base64编码原理
    修改远程端口号
    修改数据库配置文件
    Windows 2008下系统网站运行环境的搭建
    oracle 11 g数据库卸载(方法二)
    oracle11g的安装
    oracle 11g的卸载
    软件实施的技巧
    使用命令行快速打开系统文件
  • 原文地址:https://www.cnblogs.com/j2ee-web-01/p/7156602.html
Copyright © 2011-2022 走看看