我寫了一個SQL函數,應該返回你正在尋找的東西。
/* Set up test data */
create table Folders (
FolderID int,
FolderName varchar(10),
ParentID int
)
insert into Folders
(FolderID, FolderName, ParentID)
select 1,'Folder1',0 union all
select 2,'Folder2',1 union all
select 3,'Folder3',2 union all
select 4,'Folder4',3
go
/* Create function */
create function dbo.CreateFolderPath (@FolderID int)
returns varchar(1000)
as
begin
declare @ParentID int
declare @FolderPath varchar(1000)
set @FolderPath = ''
select @ParentID = ParentID
from Folders
where FolderID = @FolderID
while @ParentID<>0 begin
select @FolderPath = FolderName + '\' + @FolderPath, @ParentID = ParentID
from Folders
where FolderID = @ParentID
end /* while */
return @FolderPath
end /* function */
go
/* Demo the function */
select dbo.CreateFolderPath(4)
/* Clean up after demo */
drop function dbo.CreateFolderPath
drop table Folders
我試圖改寫你的問題,希望能幫助你得到答案。 – 2010-10-05 13:07:07