first you have to write a function like that in your database
set ANSI_NULLS ON
set QUOTED_IDENTIFIER OFF
GO
ALTER Function [dbo].[funcGenerate_TreeViewDescsNEW]
(
@Code as Varchar(15),
@TreeViewType as varchar(15)
)Returns Varchar(255)
As
Begin
DECLARE @vcReturnValue varchar(255)
declare @ParentIDLevel1 varchar(255)
declare @ParentIDLevel2 varchar(255)
declare @ParentIDLevel3 varchar(255)
declare @ParentIDLevel4 varchar(255)
declare @SubcodeLevel2 varchar(255)
declare @SubcodeLevel3 varchar(255)
IF @TreeViewType='LOCATION'
BEGIN
declare @intLocLevel int
declare @numLocID numeric(18,0)
declare @numRefLocID numeric(18,0)
declare @vcLocationCode varchar(15)
declare @vcLocationDescription varchar(255)
declare @vcLocDescLevel1 varchar(255)
declare @vcLocDescLevel2 varchar(255)
declare @vcLocDescLevel3 varchar(255)
declare @vcLocDescLevel4 varchar(255)
set @vcLocationCode = @Code
select @vcLocationCode = numLocID,@numRefLocID = ParentID from LocationMaster where numLocID=@vcLocationCode
Set @intLocLevel = 0
if @numRefLocID = 0
BEGIN
Select @vcLocationDescription =vcLocationDescription from LocationMaster where numLocID=@vcLocationCode
set @vcReturnValue= @vcLocationDescription
END
While (@numRefLocID <> 0)
BEGIN
if (@intLocLevel = 1 )
BEGIN
Set @vcLocationDescription = @vcLocationDescription + (Select vcLocationDescription from LocationMaster where numLocID=@vcLocationCode) + '>'
END
Else
BEGIN
Set @intLocLevel = 1
Set @vcLocationDescription =(Select vcLocationDescription from LocationMaster where numLocID=@vcLocationCode) + '>'
END
select @vcLocationCode=numLocID,@numRefLocID=ParentID from LocationMaster where numLocID=@numRefLocID
IF (@numRefLocID = 0)
BEGIN
Select @vcLocDescLevel1 =vcLocationDescription from LocationMaster where numLocID=@vcLocationCode
Set @vcLocationDescription = @vcLocationDescription + @vcLocDescLevel1
END
END
set @vcReturnValue= @vcLocationDescription
END
RETURN @vcReturnValue
End
after that call it in your procedure by passing the GroupID
like that
<pre lang="sql">SELECT [dbo].[funcGenerate_TreeViewDescsNEW](250,'LOCATION') as 'vcLocationDescription',
numLocID
FROM LocationMaster
your output will be like that.......
Bb-Chennai-Venus Millenium Square-Royapuram>Chennai>Tamilnadu