Hello, I need to combine 2 XML variables into a parent node. Here's my code:
CREATE TABLE [dbo].[test1](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ParentID] [int] NULL,
[ClassCode] [int] NULL,
[ClassValue] [int] NULL,
CONSTRAINT [PK_test1] PRIMARY KEY CLUSTERED ([ID] ASC)CREATE TABLE [dbo].[test2](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ParentID] [int] NULL,
[ClassCode] [int] NULL,
[ClassValue] [int] NULL,
CONSTRAINT [PK_test2] PRIMARY KEY CLUSTERED ([ID] ASC)
)INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,1,0)
INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,2,0)
INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,3,0)
INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,4,0)
INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,5,0)
INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,6,0)INSERT INTO test2 (ParentID,ClassCode,ClassValue) VALUES (100,1,0)
INSERT INTO test2 (ParentID,ClassCode,ClassValue) VALUES (100,2,0)
INSERT INTO test2 (ParentID,ClassCode,ClassValue) VALUES (100,3,0)
INSERT INTO test2 (ParentID,ClassCode,ClassValue) VALUES (100,4,0)
DECLARE @ParentID int
DECLARE @xml1 XML
DECLARE @xml2 XMLSET @ParentID=100
SET @xml1=(SELECT
'A' as [@n],
(SELECT
ClassCode [@n],
(SELECT
'C' as [c/@n],ClassValue AS [c/v]
FROM test1 t1
WHERE t1.ParentID=@ParentID and t1.ID=t2.ID and t1.ParentID=t2.ParentID
FOR XML PATH(''),TYPE
)
FROM test1 t2
WHERE t2.ParentID=@ParentIDORDER BY t2.ClassCode
FOR XML PATH('cat'),TYPE
)
FROM test1 t3
WHERE t3.ParentID=@ParentID
GROUP BY ClassCode
FOR XML PATH('grp'),TYPE)SET @xml2=(SELECT
'B' as [@n],
(SELECT
ClassCode [@n],
(SELECT
'C' as [c/@n],ClassValue AS [c/v]
FROM test2 t1
WHERE t1.ParentID=@ParentID and t1.ID=t2.ID and t1.ParentID=t2.ParentID
FOR XML PATH(''),TYPE
)
FROM test2 t2
WHERE t2.ParentID=@ParentIDORDER BY t2.ClassCode
FOR XML PATH('cat'),TYPE
)
FROM test2 t3
WHERE t3.ParentID=@ParentID
GROUP BY ClassCode
FOR XML PATH('grp'),TYPE)
The desired result should be the following:
p n='H'
grp n='A' ....../grp
grp n='B' ......../grp
/p
(I removed the <> because they don't show for some reason)
Thank you in advance.