Click here to Skip to main content
15,919,931 members
Home / Discussions / Database
   

Database

 
QuestionConfigure Auto Database Replication Problem Using Sql Server 2000 Pin
veereshIndia3-Nov-08 18:55
veereshIndia3-Nov-08 18:55 
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 
I created a full example including tables, views and data required to test the SQL.


-------------------------------------------------------------------------------------------------
---- DATA STRUCTURE PREPERATION -------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-- Create a Test Schema
CREATE SCHEMA TEST
GO



-- Create a table with parent/child relationship.
CREATE TABLE [TEST].[Product](
[ID] [int] NOT NULL,
[Name] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ParentID] [int] NOT NULL,
[ParentName] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
CONSTRAINT [PK_TEST_Product_1] PRIMARY KEY CLUSTERED
(
[ID] ASC,
[ParentID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO



-- Populate table with sample data.
INSERT INTO [TEST].[Product] VALUES (26,'TV',0, NULL)
INSERT INTO [TEST].[Product] VALUES (43,'Buy Settopbox',47,'Settopbox Group')
INSERT INTO [TEST].[Product] VALUES (47,'Settopbox Group',26,'TV')
INSERT INTO [TEST].[Product] VALUES (48,'Rent Settopbox',47,'Settopbox Group')
INSERT INTO [TEST].[Product] VALUES (82,'DR1',98,'Plus TV')
INSERT INTO [TEST].[Product] VALUES (83,'DR2',98,'Plus TV')
INSERT INTO [TEST].[Product] VALUES (84,'Sverige TV1',98,'Plus TV')
INSERT INTO [TEST].[Product] VALUES (85,'Sverige TV2',98,'Plus TV')
INSERT INTO [TEST].[Product] VALUES (88,'TV2',98,'Plus TV')
INSERT INTO [TEST].[Product] VALUES (97,'Basic TV',100,'Standard TV Group')
INSERT INTO [TEST].[Product] VALUES (98,'Plus TV',100,'Standard TV Group')
INSERT INTO [TEST].[Product] VALUES (100,'Standard TV Group',26,'TV')
INSERT INTO [TEST].[Product] VALUES (124,'Special TV Group',26,'TV')
INSERT INTO [TEST].[Product] VALUES (152,'Oprettelse TV',26,'TV')
INSERT INTO [TEST].[Product] VALUES (698,'SmileSport pakke',124,'Special TV Group')
INSERT INTO [TEST].[Product] VALUES (699,'HD Package',124,'Special TV Group')
INSERT INTO [TEST].[Product] VALUES (701,'Discovery Package',124,'Special TV Group')
INSERT INTO [TEST].[Product] VALUES (702,'Kids Package',124,'Special TV Group')
INSERT INTO [TEST].[Product] VALUES (82,'DR1', 97, 'Basic TV')
INSERT INTO [TEST].[Product] VALUES (83,'DR2', 97, 'Basic TV')



-- Create view that selects from the table and defines an xml column.
CREATE VIEW [TEST].[view_Xml_Product]
AS
SELECT
P.ID,
P.[Name],
P.ParentID,
P.ParentName,
(
SELECT
P.ID,
P.[Name],
P.ParentID,
P.ParentName
FOR XML PATH('Product'), TYPE
)
AS [xml]
FROM [TEST].[Product] P WITH (NOLOCK)
GO
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------





-------------------------------------------------------------------------------------------------
---- THE RECUSIVE CTE THAT CREATES RECURSIVE XML DOCUMENTS ------------------------------
-------------------------------------------------------------------------------------------------
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)

UNION ALL

SELECT
P2.ID,
P2.[Name],
P2.ParentID,
P2.ParentName,
CAST(RTRIM(P2.ID) + '->' + P3.[Path] 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 P2.ID = P3.ParentID
)
SELECT * FROM ProductTree ORDER BY 1
OPTION (MAXRECURSION 50)
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------


When executing the CTE you recieve a "folder-like" structure as shown in the Path field. Actually the XML column has the same structure as the path column.

[b]Like this:[/b]
Product1
Product2
Product1->Product2
Product1->Product2->Product3
Product1->Product2->Product4
Product3
Product4

and the xml:
<Product>
<ID>26</ID>
<Name>TV</Name>
<ParentID>0</ParentID>
<SubProductList>
<Product>
<ID>124</ID>
<Name>Special TV Group</Name>
<ParentID>26</ParentID>
<ParentName>TV</ParentName>
<SubProductList>
<Product>
<ID>701</ID>
<Name>Discovery Package</Name>
<ParentID>124</ParentID>
<ParentName>Special TV Group</ParentName>
</Product>
</SubProductList>
</Product>
</SubProductList>
</Product>

[b]What I wanted was like this:[/b]
Product1/Product2/Product3+Product4
Product2/Product3+Product4
Product3
Product4

and the xml (not complete, but show the idea):
<Product>
<ID>26</ID>
<Name>TV</Name>
<ParentID>0</ParentID>
<SubProductList>
<Product>
<ID>124</ID>
<Name>Special TV Group</Name>
<ParentID>26</ParentID>
<ParentName>TV</ParentName>
<SubProductList>
<Product>
<ID>698</ID>
<Name>SmileSport pakke</Name>
<ParentID>124</ParentID>
<ParentName>Special TV Group</ParentName>
</Product>
<Product>
<ID>699</ID>
<Name>HD Package</Name>
<ParentID>124</ParentID>
<ParentName>Special TV Group</ParentName>
</Product>
<Product>
<ID>702</ID>
<Name>Kids Package</Name>
<ParentID>124</ParentID>
<ParentName>Special TV Group</ParentName>
</Product>
<Product>
<ID>701</ID>
<Name>Discovery Package</Name>
<ParentID>124</ParentID>
<ParentName>Special TV Group</ParentName>
</Product>
</SubProductList>
</Product>
<Product>
<ID>100</ID>
<Name>Standard TV Group</Name>
<ParentID>26</ParentID>
<ParentName>TV</ParentName>
<SubProductList>
<Product>
<ID>97</ID>
<Name>Basic TV</Name>
<ParentID>100</ParentID>
<ParentName>Standard TV Group</ParentName>
</Product>
<Product>
<ID>98</ID>
<Name>Plus TV</Name>
<ParentID>100</ParentID>
<ParentName>Standard TV Group</ParentName>
</Product>
</SubProductList>
</Product>
</SubProductList>
</Product>

I want one row per product having all subproducts beneath it. A complete tree when looking at a specific product no matter the level in the hierarchy.

I hope you understand what I intend to do.

Michael Vivet
GeneralRe: CTE to Build XML Pin
Wendelius5-Nov-08 9:57
mentorWendelius5-Nov-08 9:57 
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 

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.