Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
moshe1001
Starting Member
1 Post |
Posted - 2008-02-16 : 08:17:10
|
| hi here is my interpellation in sqland yes im looping and waiting in between each iteration -- to get results back over .i have a Category table that have 3 columns :table structure :CategoryID CategoryName CategoryPID 1 Electricity -1 // all the (-1) are the fathers 2 For Garden -1 3 Computer -1 4 lawn-mower 2 // the son of "For Garden" 5 Memory Card 3 // the son of "Computer" 6 Lamp 1 // the son of "Electricity" i want to make a menu in my website with this structure :Electricity LampMicrowavevacuum cleanerFor Garden Lawn-mowerHammockComputerMemory CardMother BoardScreenetc....interpellation in sql:ALTER procedure [dbo].[Category_Select_XML]ASDECLARE fileCsr CURSOR READ_ONLY FORselect CategoryID,CategoryName,CategoryPID from dbo.Categories where CategoryPID=-1DECLARE @CategoryID INTDECLARE @CategoryName NVARCHAR(50)DECLARE @CategoryPID INTOPEN fileCsrFETCH NEXT FROM fileCsr INTO @CategoryID,@CategoryName,@CategoryPIDWHILE @@FETCH_STATUS = 0BEGINselect 1 as tag,null as parent,null as [Category!1],null as [CategoryP!2!ID],null as [CategoryP!2!Name],null as [CategoryChild!3!ID],null as [CategoryChild!3!Name]union allselect 2 as tag,1 as parent,null,@CategoryID,@CategoryName, null,nullunion allselect 3 as tag,2 as parent,null,null,null,CategoryID,CategoryNamefrom dbo.Categorieswhere CategoryPID=@CategoryIDfor xml explicitFETCH NEXT FROM fileCsr INTO @CategoryID,@CategoryName,@CategoryPIDEND close fileCsrDEALLOCATE fileCsr |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-16 : 12:08:42
|
You can use the recursive CTE approach if you are having expanding type of hierarchy.;With CategoryCTE (CategoryID,CategoryName) AS(SELECT CategoryID,CategoryNameFROM CategoryWHERE CategoryPID=-1UNION ALLSELECT c.CategoryID,c.CategoryNameFROM Category cINNER JOIN CategoryCTE ccON cc.CategoryID=c.CategoryPID)SELECT * FROM CategoryCTE for xml explicit this should provide with required xml for front end to create menu |
 |
|
|
|
|
|
|
|