应该是老问题了,求SQL语句:查询一列的多条记录
应该是老问题了,表Sort的结构如下:
sort_id sort_name parent_id
1冶金0
2焊接某某人1
3初级工2
4机械0
5机械加工4
6铣工5
7初级工6
8初级职业功能7
9电弧焊1
10初级工9
sort_id是主键,parent_id是上一级sort_id,现在我要得到的就是每一个sort的路径,结果应该如下:
sort_id path
1冶金
2冶金.焊接某某人
3冶金.焊接某某人.初级工
4机械
5机械.机械加工
5机械.机械加工.铣工
6机械.机械加工.铣工.初级工
....................
也就是说,每个path的值实际上为他的parent的path加上他自己的名字,在线等待,谢谢!!
[解决办法]
剛貼了個相似的,LZ自己改下吧
create table tree(id int,pid int ,name varchar(02))
insert into tree
select 1,0, 'A ' union all
select 2,1, 'B ' union all
select 3,4, 'C ' union all
select 4,7, 'D ' union all
select 5,2, 'E ' union all
select 6,4, 'F ' union all
select 7,2, 'G ' union all
select 8,5, 'H ' union all
select 9,0, 'I ' union all
select 10,8, 'J '
GO
create procedure dbo.usp_test
@pid int
AS
begin
set nocount on
declare @level int ,@i int ,@flag int
declare @stack table (pid int,id int, level int,row int , flag int,name varchar(02))
/*將開始層插入 */
insert into @stack(pid,id,level,row)
select @pid,@pid,0,0
select @level = 1,@i=1,@flag=1
insert @stack
select pid,id, @level,0,1,name
from tree(nolock)
where pid = @pid and id is not null
while @level > 0
begin
if exists (select * from @stack where level = @level and flag=1)
begin
select @pid = min(id)
from @stack
where level = @level and flag=1
update @stack set flag =0 , row=@i
where level = @level
and id = @pid and flag =1
set @i = @i +1
insert @stack
select pid,id, @level +1,0,1 ,name
from tree(nolock)
where pid = @pid and id is not null
if @@rowcount > 0
select @level = @level + 1
end
else
begin
select @level = @level - 1
end
end
declare @str varchar(50)
set @str= ' '
select @str=@str+ '- '+name from @stack where name is not null order by row
select stuff(@str,1,1, ' ')
set nocount off
end
GO
--
--exec usp_test 0
drop table tree
drop proc usp_test
[解决办法]
--建立函數
Create Function F_GetSort(@sort_id Int)
Returns Nvarchar(2000)
As
Begin
Declare @S Nvarchar(2000)
Select @S = sort_name, @sort_id = parent_id From Sort Where sort_id = @sort_id
While @@Rowcount > 0
Select @sort_id = parent_id, @S = @S + ', ' + IsNull(sort_name, ' ') From Sort Where sort_id = @sort_id
Return @S
End
GO
--測試
Select
sort_id,
dbo.F_GetSort(sort_id) As path
From
Sort
GO
[解决办法]
--sql server 2005 方法:
--建立测试环境
create table tb(sort_id int,sort_name varchar(20),parent_id int)
insert into tb select 1, '冶金 ',0
union all select 2, '焊接某某人 ',1
union all select 3, '初级工 ',2
union all select 4, '机械 ',0
union all select 5, '机械加工 ',4
union all select 6, '铣工 ',5
union all select 7, '初级工 ',6
union all select 8, '初级职业功能 ',7
union all select 9, '电弧焊 ',1
union all select 10, '初级工 ',9
go
--建立函数
create function f_getvale(@sort_id int)
returns varchar(50)
as
begin
declare @returns varchar(200)
set @returns= ' ';
with CTE_TB(sort_id,sort_name,parent_id,lvl)
as
(
select sort_id,sort_name,parent_id,0
from tb
where sort_id=@sort_id
union all
select ta.sort_id,ta.sort_name,ta.parent_id,tc.lvl+1
from tb ta inner join CTE_TB tc
on ta.sort_id=tc.parent_id
)
select @returns=@returns+ ', '+sort_name
from CTE_TB
order by parent_id
return stuff(@returns,1,1, ' ')
end
go
--查询语句
select sort_id,path=dbo.f_getvale(sort_id)
from tb
group by sort_id
--删除测试环境
go
drop table tb
go
drop function f_getvale
--测试结果
/*
sort_id path
----------- --------------------------------------------------
1 冶金
2 冶金,焊接某某人
3 冶金,焊接某某人,初级工
4 机械
5 机械,机械加工
6 机械,机械加工,铣工
7 机械,机械加工,铣工,初级工
8 机械,机械加工,铣工,初级工,初级职业功能
9 冶金,电弧焊
10 冶金,电弧焊,初级工
(10 行受影响)
*/
[解决办法]
--抢几分
create table tablea( sort_id int, sort_name varchar(50), parent_id int)
insert into tablea select 1, '冶金 ',0
union all select 2, '焊接某某人 ',1
union all select 3, '初级工 ',2
union all select 4, '机械 ',0
union all select 5, '机械加工 ',4
union all select 6, '铣工 ',5
union all select 7, '初级工 ',6
union all select 8, '初级职业功能 ',7
union all select 9, '电弧焊 ',1
union all select 10, '初级工 ',9
select dbo.myfon(parent_id,sort_name) from tablea
alter FUNCTION dbo.myfon (@parent_id int,@sort_name varchar(1000))
RETURNS varchar(1000)
AS
begin
while (@parent_id <> 0)
select @sort_name=sort_name+ '. '+@sort_name,@parent_id=parent_id from tablea where sort_id=@parent_id
RETURN @sort_name
end
/*
----------------------------------------------------------------------------------------------------------------
冶金
冶金.焊接某某人
冶金.焊接某某人.初级工
机械
机械.机械加工
机械.机械加工.铣工
机械.机械加工.铣工.初级工
机械.机械加工.铣工.初级工.初级职业功能
冶金.电弧焊
冶金.电弧焊.初级工
(所影响的行数为 10 行)
*/