Click here to Skip to main content
16,015,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi I have a table with this records(GroupId,ParentGroupId,GroupName).
i want to show a group with its full path, from main parent to it self

like this: red cherry: red cherry>>cherry>>fruit


i write a Procedure that give me its parents names,but i want to have a prosedure that append the parents name and give me one string

i want to do this for all records in that table

thanx for u answers
Posted
Updated 1-Feb-12 19:15pm
v2
Comments
Rajesh Anuhya 2-Feb-12 1:15am    
Your Question is not clear for me
--RA

Hi,
take retival data of procedure to tempery table.
write funtion to set the parent heighrachichy.
then you can insert data.
 
Share this answer
 
first you have to write a function like that in your database

SQL
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

SQL
RETURN @vcReturnValue
End


after that call it in your procedure by passing the GroupID
like that

SQL
<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
 
Share this answer
 
v2
Comments
maliheSadat 2-Feb-12 1:27am    
our number of subgroups are not fixed.user can add subgroups!
i want the full path of child
you can add 'n' number of subgroups it work properly
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900