--生成测试数据
create table BOM(CID INT,PID INT,CODE VARCHAR(10))
insert into BOM select 1,0,'01'
insert into BOM select 2,1,'02'
insert into BOM select 3,0,'03'
insert into BOM select 4,2,'04'
go
SELECT CID,PID,CODE=CAST(CODE as varchar(8000))
INTO #
FROM BOM
UPDATE A SET
CODE=P.CODE+'-'+A.CODE
FROM # P, # A
WHERE P.PID=0
AND P.CID=A.PID
WHILE @@ROWCOUNT>0
UPDATE A SET
CODE=P.CODE+'-'+A.CODE
FROM # P, # A
WHERE P.CID=A.PID
AND CHARINDEX('-',P.CODE)>0
AND CHARINDEX('-',A.CODE)=0
SELECT CID,CODE FROM #
DROP TABLE #
go
drop function dbo.f_1
create table tb(CID INT,PID INT,CODE VARCHAR(10))
insert into tb select 1,0,'01'
insert into tb select 2,1,'02'
insert into tb select 3,0,'03'
insert into tb select 4,2,'04'
go
create function f_1(@cid int)
returns varchar(8000)
as
begin
declare @s varchar(8000)
set @s=''
select @s=@s+'-'+code,@cid=pid from tb where cid=@cid
while @@rowcount>0
begin
select @s=@s+'-'+code,@cid=pid from tb where cid=@cid
end
return stuff(@s,1,1,'')
end
go
select cid,[code]=dbo.f_1(cid) from tb
/*
cid code
----------- ------------
1 01
2 02-01
3 03
4 04-02-01
create table A
(
CID int,
PID int,
CODE varchar(10)
)
insert A
select 1,0,'01' union
select 2,1,'02' union
select 3,0,'03' union
select 4,2,'04'
go
--创建函数
create function f_tb(@CID int)
returns @tb table(CID int,Code varchar(10))
as
begin
insert @tb select CID,Code from A where PID=@CID
while @@rowcount>0
begin
insert @tb
select A.CID
,B.Code+'-'+A.Code
from A
join @tb B on A.PID=B.CID
where A.CID not in(select CID from @tb)
end
--生成测试数据
create table BOM(CID INT,PID INT,CODE VARCHAR(10))
insert into BOM select 1,0,'01'
insert into BOM select 2,1,'02'
insert into BOM select 3,0,'03'
insert into BOM select 4,2,'04'
go
--创建用户定义函数
create function f_getChild(@CID VARCHAR(10))
returns varchar(8000)
as
begin
declare @i int,@ret varchar(8000)
declare @t table(CID INT,PID INT,CODE VARCHAR(10),Level INT)
set @i = 1
insert into @t select CID,PID,CODE,@i from BOM where CID = @CID
while @@rowcount<>0
begin
set @i = @i + 1
insert into @t
select
a.CID,a.PID,a.CODE,@i
from
BOM a,@t b
where
a.CID=b.PID and b.Level = @i-1
end
select @ret = isnull(@ret,'')+'-'+CODE from @t order by level desc
set @ret = stuff(@ret,1,1,'')
return @ret
end
go
--执行查询
select CID,PID,CODE=dbo.f_getChild(CID) from BOM
--生成测试数据
create table BOM(CID INT,PID INT,CODE VARCHAR(10))
insert into BOM select 1,0,'01'
insert into BOM select 2,1,'02'
insert into BOM select 3,0,'03'
insert into BOM select 4,2,'04'
go
--创建用户定义函数
create function f_getChild(@CID VARCHAR(10))
returns varchar(8000)
as
begin
declare @i int,@ret varchar(8000)
declare @t table(CID INT,PID INT,CODE VARCHAR(10),Level INT)
set @i = 1
insert into @t select CID,PID,CODE,@i from BOM where CID = @CID
while @@rowcount<>0
begin
set @i = @i + 1
insert into @t
select
a.CID,a.PID,a.CODE,@i
from
BOM a,@t b
where
a.CID=b.PID and b.Level = @i-1
end
select @ret = isnull(@ret,'')+'-'+CODE from @t order by level desc
set @ret = stuff(@ret,1,1,'')
return @ret
end
go
select CID,PID,CODE=dbo.f_getChild(CID) from BOM
go
create function f_1(@cid int)
returns varchar(8000)
as
begin
declare @s varchar(8000)
set @s=''
select @s=@s+code,@cid=pid from tb where cid=@cid
while @@rowcount>0
begin
select @s=@s+code,@cid=pid from tb where cid=@cid
end
return @s
end
go
select cid,[code]=dbo.f_1(cid) from tb