表 department 
departmenid(部门ID)   parentid(父部门ID)
1                        0
2                        1
3                        1
4                        2
5                        2
6                        3
7                        3
8                        7       我查询部门ID ,张三对应的部门ID为8 ,如何查出张三的所有部门ID(包括父部门ID),如8,7,3,1

解决方案 »

  1.   

    --建立測試環境
    Create Table department
    (departmenid Int,
    parentid Int)
    Insert department Select 60, null
    Union All Select 1, 0
    Union All Select 2, 1
    Union All Select 3, 1
    Union All Select 4, 2
    Union All Select 5, 2
    Union All Select 6, 3
    Union All Select 7, 3
    Union All Select 8, 7
    GO
    --建立函數
    Create Function F_GetParent(@departmenid Int)
    Returns @Tree Table (departmenid Int, parentid Int)
    As
    Begin
    Insert @Tree Select * From department Where departmenid = @departmenid
    While @@Rowcount > 0
    Insert @Tree Select A.* From department A Inner Join @Tree B On A.departmenid = B.parentid And A.departmenid Not In (Select departmenid From @Tree) Where A.parentid Is Not Null
    Return
    End
    GO
    --測試
    Select departmenid From dbo.F_GetParent(8) Order By parentid
    GO
    --刪除測試環境
    Drop Table department
    Drop Function F_GetParent
    --結果
    /*
    departmenid
    1
    3
    7
    8
    */
      

  2.   

    游标可以实现吧,不用刻意去写个函数,查询出parentid后付给变量再去查找就可以啊
      

  3.   

    我寫的是一個函數,你建立好函數之後,在查詢的時候直接調用函數即可。主要是這部分--建立函數
    Create Function F_GetParent(@departmenid Int)
    Returns @Tree Table (departmenid Int, parentid Int)
    As
    Begin
    Insert @Tree Select * From department Where departmenid = @departmenid
    While @@Rowcount > 0
    Insert @Tree Select A.* From department A Inner Join @Tree B On A.departmenid = B.parentid And A.departmenid Not In (Select departmenid From @Tree) Where A.parentid Is Not Null
    Return
    End
    GO
    --測試
    Select departmenid From dbo.F_GetParent(8) Order By parentid
      

  4.   

    babyold() ( ) 信誉:100  2007-08-29 10:25:36  得分: 0  
     
     
       游标可以实现吧,不用刻意去写个函数,查询出parentid后付给变量再去查找就可以啊
      
     
    --------------
    你認為用游標比用這個函數更好嗎?游標的效率很低的。
      

  5.   

    --建立測試環境
    Create Table department
    (departmenid Int,
    parentid Int)
    Insert department Select 60, null
    Union All Select 1, 0
    Union All Select 2, 1
    Union All Select 3, 1
    Union All Select 4, 2
    Union All Select 5, 2
    Union All Select 6, 3
    Union All Select 7, 3
    Union All Select 8, 7
    GO
    --創建存儲過程
    Create ProceDure SP_GetParent(@departmenid Int)
    As
    Begin
    Select * Into #Tree From department Where departmenid = @departmenid
    While @@Rowcount > 0
    Insert #Tree Select A.* From department A Inner Join #Tree B On A.departmenid = B.parentid And A.departmenid Not In (Select departmenid From #Tree) Where A.parentid Is Not Null
    Select departmenid From #Tree Order By parentid
    Drop Table #Tree
    End
    GO
    --測試
    EXEC SP_GetParent 8
    GO
    --刪除測試環境
    Drop Table department
    Drop ProceDure SP_GetParent
    --結果
    /*
    departmenid
    1
    3
    7
    8
    */
      

  6.   

    qgmtfj() ( ) 信誉:100  2007-08-29 11:20:22  得分: 0  
     
     
       贴子回复次数大于跟给分次数
    这咋办啊???
      
     
    ------------------
    刷新下,再重新結即可。 :)不是說你人品有問題,只是看到你所有的帖子都沒有結,以為你不知道怎麼結貼。