zoukankan      html  css  js  c++  java
  • SQL中的循环获取根节点

    方法一:

    DECLARE @employeeid INT;
    set @employeeid = 8115;
    
    DECLARE @id INT;
    DECLARE @pid int;
    DECLARE @level int;
    SET @id=-1
    --获取父节点id
    SELECT @id = RecID, @pid = PID, @level = [Level]
    FROM Organization.dbo.OfficePlace
    WHERE recid= (SELECT TOP 1 OfficePlaceID FROM Organization.dbo.Employees WHERE EmployeeId=@employeeid)
    
    WHILE (@pid!=-1)
    BEGIN
      SELECT @id = RecID,@pid = PID,@level = [Level] 
      FROM Organization.dbo.OfficePlace
      where status=0 AND RecID=@pid
    END
    PRINT @id
    

    方法二: DECLARE @officePlaceId INT;

    DECLARE @officePlaceId INT;
     set @officePlaceId=117;
    
     DECLARE @id INT;
            SELECT  @id = ISNULL(( SELECT TOP 1
                                            pid
                                   FROM     Organization.dbo.OfficePlace
                                   WHERE    RecID = @officePlaceId
                                            AND pid <> -1
                                            AND pid IS NOT NULL
                                 ), -1) 
            WHILE @@ROWCOUNT > 0
                AND @id > 0
                BEGIN
                    SELECT  @id = pid
                    FROM    Organization.dbo.OfficePlace
                    WHERE   RecID = @id
                            AND pid <> -1
                            AND pid IS NOT NULL
                END
            print @id 
    

      

    你觉得那种好呢?

  • 相关阅读:
    广播通信
    IP多播
    套接字选项
    IO处理线程
    单源最短路径
    活动安排问题贪心算法
    贪心算法
    IO控制命令
    [转]qsort详解
    2012下半年学习目录
  • 原文地址:https://www.cnblogs.com/stevenjson/p/5007762.html
Copyright © 2011-2022 走看看