Click here to Skip to main content
15,902,112 members
Home / Discussions / Database
   

Database

 
AnswerRe: Configure Auto Database Replication Problem Using Sql Server 2000 Pin
Wendelius4-Nov-08 5:05
mentorWendelius4-Nov-08 5:05 
QuestionDebugging a stored procedure Pin
dptalt3-Nov-08 10:29
dptalt3-Nov-08 10:29 
AnswerThis doesn't answer your question Pin
Ennis Ray Lynch, Jr.4-Nov-08 4:30
Ennis Ray Lynch, Jr.4-Nov-08 4:30 
AnswerRe: Debugging a stored procedure Pin
Wendelius4-Nov-08 5:18
mentorWendelius4-Nov-08 5:18 
QuestionCTE to Build XML Pin
Michael Vivet3-Nov-08 1:51
Michael Vivet3-Nov-08 1:51 
AnswerRe: CTE to Build XML Pin
Wendelius4-Nov-08 6:58
mentorWendelius4-Nov-08 6:58 
GeneralRe: CTE to Build XML Pin
Michael Vivet5-Nov-08 1:43
Michael Vivet5-Nov-08 1:43 
GeneralRe: CTE to Build XML Pin
Wendelius5-Nov-08 9:57
mentorWendelius5-Nov-08 9:57 
Hi,

First of all, you did a great job in creating this example! You've really put effort into it.

I've investigated this a bit and so far I haven't used the xml portion in any way. I think it's in this stage easier to play only with the path.

What I've done:
- created a test view based on the recursive query (it's easier to handle it in the next stage)
- in the CTE I locked the root to those records having parentid 0. In the previous version all the rows acted as parents
- changed the path to build the string in reverse order to gain correct path
- changed the join in second part of the cte (it was in wrong order)

So now the path is showing better results and also the parentid is giving the previous parentid, not the original parentid.

The view looks like this:
CREATE VIEW vw_test AS
WITH ProductTree (ID, [Name], ParentID, ParentName, [Path], [xml]) AS
(
SELECT P.ID,
       P.[Name],
       P.ParentID,
       P.ParentName,
       CAST(P.ID AS Varchar(200)) AS [Path],
       P.[xml] AS [xml]
FROM [TEST].[view_Xml_Product] AS P WITH (NOLOCK)
WHERE  p.parentid=0
UNION ALL
SELECT P2.ID,
       P2.[Name],
       P2.ParentID,
       P2.ParentName,
       CAST(RTRIM(P3.[Path]) + '->' + cast(P2.ID as varchar(200)) AS varchar(200)) AS [Path],
       (SELECT P2.ID,
               P2.[Name],
               P2.ParentID,
               P2.ParentName,
               P3.[xml] AS SubProductList
               FOR XML PATH('Product'), TYPE) AS SubProductList
FROM [TEST].[view_Xml_Product] AS P2 WITH (NOLOCK)
INNER JOIN ProductTree AS P3 ON P3.ID = P2.ParentID
)
SELECT * FROM ProductTree

The next phase was to remove the rows that have 'children'. I did it in a quick and dirty way but here's the query:
SELECT * 
FROM  vw_test a 
WHERE NOT EXISTS( SELECT 1 
                  FROM  vw_test b
                  WHERE b.[Path] LIKE a.[Path] + '%' 
                  AND   b.[Path] != a.[Path]) 
ORDER BY 5

The next step would be to eliminate the rows that are sub level row and have the same parentid. In this process those rows should be combined so that the id part should be added to the previous row. For example rows:
ID  Name         ParentID ParentName       Path
698 SmileSport pakke  124      Special TV Group	26->124->698
699 HD Package        124      Special TV Group	26->124->699
701 Discovery Package 124      Special TV Group	26->124->701
702 Kids Package      124      Special TV Group	26->124->702

should be converted to single row having path:
26->124->698 + 699 + 701 + 702

One way to do this is to create a stored proc which uses this select and then using cursor combine rows.

One thing though. I'm quite pessimistic with the efficiency. So basically your original idea of using a function may be better when bottlenecks in that function are eliminated.

Also few observations.
- You shouldn't use 0 for the root parent id. Use NULL instead.
- Consider creating a foreign key between parentid and id.
- Don't save the parentname in the table. Fetch it when needed so you won't have possible mismatches in data and you don't have to update several places when name is changed.

Does this help you forward?

Mika

The need to optimize rises from a bad design.

My articles[^]

GeneralRe: CTE to Build XML Pin
Michael Vivet6-Nov-08 23:32
Michael Vivet6-Nov-08 23:32 
GeneralRe: CTE to Build XML Pin
Wendelius7-Nov-08 6:59
mentorWendelius7-Nov-08 6:59 
QuestionCheck if a table is empty Pin
ONeil Tomlinson2-Nov-08 22:36
ONeil Tomlinson2-Nov-08 22:36 
AnswerRe: Check if a table is empty Pin
Ashfield2-Nov-08 22:49
Ashfield2-Nov-08 22:49 
GeneralRe: Check if a table is empty Pin
SomeGuyThatIsMe4-Nov-08 7:46
SomeGuyThatIsMe4-Nov-08 7:46 
GeneralRe: Check if a table is empty Pin
Ashfield4-Nov-08 8:47
Ashfield4-Nov-08 8:47 
GeneralRe: Check if a table is empty Pin
SomeGuyThatIsMe4-Nov-08 9:25
SomeGuyThatIsMe4-Nov-08 9:25 
QuestionBackup database is terminating abnormally Pin
sunil goyalG2-Nov-08 20:26
sunil goyalG2-Nov-08 20:26 
AnswerRe: Backup database is terminating abnormally Pin
Eddy Vluggen3-Nov-08 0:14
professionalEddy Vluggen3-Nov-08 0:14 
GeneralRe: Backup database is terminating abnormally Pin
sunil goyalG3-Nov-08 1:09
sunil goyalG3-Nov-08 1:09 
AnswerRe: Backup database is terminating abnormally Pin
Eddy Vluggen3-Nov-08 1:35
professionalEddy Vluggen3-Nov-08 1:35 
GeneralRe: Backup database is terminating abnormally Pin
sunil goyalG3-Nov-08 1:52
sunil goyalG3-Nov-08 1:52 
GeneralRe: Backup database is terminating abnormally Pin
Eddy Vluggen3-Nov-08 2:05
professionalEddy Vluggen3-Nov-08 2:05 
GeneralRe: Backup database is terminating abnormally Pin
sunil goyalG3-Nov-08 2:13
sunil goyalG3-Nov-08 2:13 
QuestionRe: Backup database is terminating abnormally Pin
Eddy Vluggen3-Nov-08 2:32
professionalEddy Vluggen3-Nov-08 2:32 
AnswerRe: Backup database is terminating abnormally Pin
sunil goyalG3-Nov-08 2:38
sunil goyalG3-Nov-08 2:38 
QuestionImporting data in SQL table from .csv file Pin
preetpal2-Nov-08 19:45
preetpal2-Nov-08 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.