一个部门的数据表,里面是目录树结构.
如:  deptID  deptName  parentID
里面的数据  是     1    所有部门   0
                   2    财务部     1
                   3    市场部     1
                   4    仓库管理   1
                   5    东北市场   3
                   6    西北市场   3
                   7    东南市场   3
                   8    上海市     7
                   9    北京市     6等依次类推得目录树结构
我想写一个函数 传入部门ID号后 马上得到相应的 部门结构 如
输入8   得到的是    市场部-东南市场-上海市
输入9   得到的是    市场部-西北市场-北京市
输入6   得到的是    市场部-西北市场输入3   得到的是    市场部输入1   得到的是    所有部门

解决方案 »

  1.   

    create table dept(deptID int,deptName varchar(10),ParentID int)
    insert into dept select 1,    '所有部门',   0
    union all select   2,    '财务部' ,    1
    union all select   3 ,   '市场部'  ,   1
    union all select   4  ,  '仓库管理'  , 1
    union all select   5,   '东北市场'  , 3
    union all select   6 ,   '西北市场'  , 3
    union all select   7  ,  '东南市场'  , 3
    union all select   8  ,  '上海市'    , 7
    union all select   9 ,   '北京市'  ,   6
    CREATE function GetdeptName(@deptID varchar(10))
    returns varchar(4000)
    as
    begin
    declare @re table(deptID int,level int,ename varchar(4000))
    declare @level int,@name varchar(1000)
    set @name=''
    set @level=0
    insert @re select @deptID,@level,@name
    while @@rowcount>0
    begin
    set @level=@level+1
    insert @re select a.ParentID,@level,(deptName+'→'+@name) from dept as a,@re as b 
    where a.deptID=b.deptID and b.level=@level-1 and a.ParentID!=0
    select @name=ename from @re where level=@level
    end
    select @name=ename from @re where level in (select max(level) from @re)
    select @name=left(@name,len(@name)-1)
    return @name
    end
      

  2.   

    所要部门不要显示吗~~~create table test(deptID int,deptName varchar(20),parentID int)
    insert test
    select 1    ,'所有部门',   0 union all
    select 2    ,'财务部',     1 union all
    select 3    ,'市场部',     1 union all
    select 4    ,'仓库管理',   1 union all
    select 5    ,'东北市场',   3 union all
    select 6    ,'西北市场',   3 union all
    select 7    ,'东南市场',   3 union all
    select 8    ,'上海市',     7 union all
    select 9    ,'北京市',     6
    --select * from test
    gocreate proc pTest
        @id int
    as
    begin
        declare @result varchar(1000)
        declare @ProvID int
        select @ProvID=@id    select @result=deptName from test where deptID=@ProvID
        while exists(select 1 from test where deptID=@ProvID and parentID<>0)
        begin
            select @ProvID=parentID from test where deptID=@ProvID
            select @result=deptName +'-'+@result from test where deptID=@ProvID
        end    select @result
    end
    goexec pTest 3drop proc pTest
    drop table test
      

  3.   

    上面已经测试过了
    我是建了一个函数
    调用时输入要求的deptid就成
    例如:
    select dbo.GetdeptName(8)
    --结果:
    市场部→东南市场→上海市
      

  4.   

    BEGIN TRAN
    GO
    CREATE TABLE tbDept
    (
    deptID INT,
    deptName VARCHAR(50),  
    parentID INT
    )INSERT INTO tbDept
    SElECT 1,'所有部门',0 UNION ALL
    SElECT 2, '财务部',  1 UNION ALL
    SElECT 3, '市场部',  1 UNION ALL
    SElECT 4, '仓库管理',1 UNION ALL
    SElECT 5, '东北市场',3 UNION ALL
    SElECT 6, '西北市场',3 UNION ALL
    SElECT 7, '东南市场',3 UNION ALL
    SElECT 8, '上海市',  7 UNION ALL
    SElECT 9, '北京市',  6SELECT *
    FROM tbDeptGO
    CREATE FUNCTION GetDeptStruct
    (@deptID INT)
    RETURNS VARCHAR(500)
    AS 
    BEGINDECLARE @s VARCHAR(500), @dID INTSELECT @s = deptName, @dID = @deptID
    FROM tbDept
    WHERE deptID = @deptIDWHILE @@ROWCOUNT > 1
    BEGIN
    SELECT @s = b.deptName + '-' + @s, @dID = b.deptID
    FROM tbDept a 
    INNER JOIN tbDept b ON a.parentID = b.deptID
    WHERE a.deptID = @dID
    AND b.deptID > 1
    ENDRETURN @sEND
    GOSELECT dbo.GetDeptStruct(2)GO
    ROLLBACK
      

  5.   

    写错了一个地方
    “WHILE @@ROWCOUNT > 1”
    应为
    “WHILE @@ROWCOUNT > 0”
      

  6.   

    当传递1时要得到所有部门这项
    那就只需对下面这句添加一个对传入的的配套ID作判断来分条件
    if(@deptID!=1)
        insert @re select a.ParentID,@level,(deptName+'→'+@name) from dept 
    as a,@re as b where a.deptID=b.deptID and b.level=@level-1 and a.ParentID!=0
    else 
        insert @re select a.ParentID,@level,(deptName+'→'+@name) from dept 
    as a,@re as b where a.deptID=b.deptID and b.level=@level-1
      

  7.   

    --建立测试环境
    create table TableA(deptID int,deptName nvarchar(100),parentID int)
    insert into TableA
    select 1,'所有部门',0 union all
    select 2,'财务部', 1 union all
    select 3,'市场部', 1 union all
    select 4,'仓库管理',1 union all
    select 5,'东北市场',3 union all
    select 6,'西北市场',3 union all
    select 7,'东南市场',3 union all
    select 8,'上海市', 7 union all
    select 9,'北京市', 6--建立函数
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AllDept]') and xtype in (N'FN', N'IF', N'TF'))
    drop function [dbo].[AllDept]
    GOCREATE function AllDept(@iDeptID int)
    returns nvarchar(1000)
    as
    begin
    declare @vReturnValue nvarchar(1000)
    ,@iParentID int
    ,@vCurrentDeptName nvarchar(200)
    select @vReturnValue=''
    ,@vCurrentDeptName=''
    if(exists(select top 1 0 from TableA where DeptID=@iDeptID and parentID=0))
    begin
    select @vReturnValue =@vReturnValue+deptName+'-'
    from TableA
    where parentID<>0
    return (@vReturnValue)
    end
    if(exists(select top 1 0 from TableA where DeptID=@iDeptID and parentID=1))
    begin
    select @vReturnValue=@vReturnValue+deptName 
    from TableA where DeptID=@iDeptID and parentID=1
    --return (@vReturnValue)
    --set @vReturnValue=@vReturnValue+dbo.AllDept(@iParentID)
    end
    else
    begin
    select @iParentID=parentID 
    ,@vCurrentDeptName=deptName
    from TableA 
    where DeptID=@iDeptID
    set @vReturnValue=@vReturnValue+@vCurrentDeptName+'-'+dbo.AllDept(@iParentID)
    --return (@vReturnValue)
    end
    return (@vReturnValue)
    endGO
    SET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS ON 
    GOselect *,dbo.AllDept(deptid) AllDeptName from tableA
    --显示结果
    deptId deptName AllDeptName
    1 所有部门 0 财务部-市场部-仓库管理-东北市场-西北市场-东南市场-上海市-北京市-
    2 财务部 1 财务部
    3 市场部 1 市场部
    4 仓库管理 1 仓库管理
    5 东北市场 3 东北市场-市场部
    6 西北市场 3 西北市场-市场部
    7 东南市场 3 东南市场-市场部
    8 上海市 7 上海市-东南市场-市场部
    9 北京市 6 北京市-西北市场-市场部
    --删除测试环境
    drop table tableA
    drop table dbo.AllDept