I am looking for a way to update (.modify('insert into..') to be specific) an XML column in a table with xml fragments from another table linked by a foreign key.
For example, my table structure looks like below (simplified):
- Fields 1 to 5 in each table can be ignored; their only purpose is to explain the fields Xx.
- Fields Xx in each table are defined as XML and is pre-populated with an XML fragment which contains fields from the table inside a tag, table name. XML fragments are depicted after this list.
- Table B and Table C has a foreign key FK_A, which links them to Table A. Table A - B and Table A - C are one to many (one record in A can have multiple records in B and C).
Now, to the Xx field sample values prior to what I need to achieve:
<!-- Table A record 1 -->
<TableA PK_A="1">
<A1>Avalue</A1>
<A2>Avalue</A2>
<A3>Avalue</A3>
<A4>Avalue</A4>
<A5>Avalue</A5>
</TableA>
<!-- Table B record 1 -->
<TableB PK_B="1" FK_A="1">
<B1>Bvalue1</B1>
<B2>Bvalue1</B2>
<B3>Bvalue1</B3>
<B4>Bvalue1</B4>
<B5>Bvalue1</B5>
</TableB>
<!-- Table B record 2 -->
<TableB PK_B="2" FK_A="1">
<B1>Bvalue2</B1>
<B2>Bvalue2</B2>
<B3>Bvalue2</B3>
<B4>Bvalue2</B4>
<B5>Bvalue2</B5>
</TableB>
<!-- Table C record 1 -->
<TableC PK_C="1" FK_A="1">
<C1>Cvalue1</C1>
<C2>Cvalue1</C2>
<C3>Cvalue1</C3>
<C4>Cvalue1</C4>
<C5>Cvalue1</C5>
</TableC>
<!-- Table C record 2 -->
<TableC PK_C="2" FK_A="1">
<C1>Cvalue2</C1>
<C2>Cvalue2</C2>
<C3>Cvalue2</C3>
<C4>Cvalue2</C4>
<C5>Cvalue2</C5>
</TableC>
The problem here is, how would I update Table A by inserting all XBs and XCs as first (or last) in to the corresponding XA? I prefer a single operation to update the whole table.
After the operation, XA should look like:
<!-- Table A record 1 -->
<TableA PK_A="1">
<!-- Table B record 1 -->
<TableB PK_B="1" FK_A="1">
<B1>Bvalue1</B1>
<B2>Bvalue1</B2>
<B3>Bvalue1</B3>
<B4>Bvalue1</B4>
<B5>Bvalue1</B5>
</TableB>
<!-- Table B record 2 -->
<TableB PK_B="2" FK_A="1">
<B1>Bvalue2</B1>
<B2>Bvalue2</B2>
<B3>Bvalue2</B3>
<B4>Bvalue2</B4>
<B5>Bvalue2</B5>
</TableB>
<!-- Table C record 1 -->
<TableC PK_C="1" FK_A="1">
<C1>Cvalue1</C1>
<C2>Cvalue1</C2>
<C3>Cvalue1</C3>
<C4>Cvalue1</C4>
<C5>Cvalue1</C5>
</TableC>
<!-- Table C record 2 -->
<TableC PK_C="2" FK_A="1">
<C1>Cvalue2</C1>
<C2>Cvalue2</C2>
<C3>Cvalue2</C3>
<C4>Cvalue2</C4>
<C5>Cvalue2</C5>
</TableC>
<A1>Avalue</A1>
<A2>Avalue</A2>
<A3>Avalue</A3>
<A4>Avalue</A4>
<A5>Avalue</A5>
</TableA>
What have I tried so far?
My best lead so far has been a divide and rule approach (a two part solution, which doesn't work).
WITH CTEB (PK_A, XA, XB)
AS (SELECT A.PK_A,
a.XA,
b.XB
FROM TableA AS a
INNER JOIN
TableB AS b
ON b.FK_A = a.PK_A)
UPDATE CTEB
SET XA.modify('insert sql:column("XB") as last into (/TableA)[1]');
WITH CTEC (PK_A, XA, XC)
AS (SELECT A.PK_A,
a.XA,
c.XC
FROM TableA AS a
INNER JOIN
TableC AS c
ON c.FK_A = a.PK_A)
UPDATE CTEC
SET XA.modify('insert sql:column("XC") as last into (/TableA)[1]');
Edit:
Apologies for not giving any text values to copy paste. Here we go.
DECLARE @A TABLE (PK_A INT, XA XML);
DECLARE @B TABLE (PK_B INT, XB XML, FK_A INT);
DECLARE @C TABLE (PK_C INT, XC XML, FK_A INT);
INSERT INTO @A
VALUES (1, '<TableA PK_A="1"><A1>Avalue</A1><A2>Avalue</A2><A3>Avalue</A3><A4>Avalue</A4><A5>Avalue</A5></TableA>')
INSERT INTO @A
VALUES (2, '<TableA PK_A="2"><A1>Avalue</A1><A2>Avalue</A2><A3>Avalue</A3><A4>Avalue</A4><A5>Avalue</A5></TableA>')
INSERT INTO @B
VALUES (1,'<TableB PK_B="1" FK_A="1"><B1>Bvalue1</B1><B2>Bvalue1</B2><B3>Bvalue1</B3><B4>Bvalue1</B4><B5>Bvalue1</B5></TableB>', 1)
INSERT INTO @B
VALUES (2, '<TableB PK_B="2" FK_A="1"><B1>Bvalue2</B1><B2>Bvalue2</B2><B3>Bvalue2</B3><B4>Bvalue2</B4><B5>Bvalue2</B5></TableB>', 1)
INSERT INTO @B
VALUES (3, '<TableB PK_B="3" FK_A="2"><B1>Bvalue3</B1><B2>Bvalue3</B2><B3>Bvalue3</B3><B4>Bvalue3</B4><B5>Bvalue3</B5></TableB>', 2)
INSERT INTO @B
VALUES (4, '<TableB PK_B="4" FK_A="2"><B1>Bvalue4</B1><B2>Bvalue4</B2><B3>Bvalue4</B3><B4>Bvalue4</B4><B5>Bvalue4</B5></TableB>', 2)
INSERT INTO @C
VALUES (1, '<TableC PK_C="1" FK_A="1"><C1>Cvalue1</C1><C2>Cvalue1</C2><C3>Cvalue1</C3><C4>Cvalue1</C4><C5>Cvalue1</C5></TableC>', 1)
INSERT INTO @C
VALUES (2, '<TableC PK_C="2" FK_A="1"><C1>Cvalue2</C1><C2>Cvalue2</C2><C3>Cvalue2</C3><C4>Cvalue2</C4><C5>Cvalue2</C5></TableC>', 1)
INSERT INTO @C
VALUES (3, '<TableC PK_C="3" FK_A="2"><C1>Cvalue3</C1><C2>Cvalue3</C2><C3>Cvalue3</C3><C4>Cvalue3</C4><C5>Cvalue3</C5></TableC>', 2)
INSERT INTO @C
VALUES (4, '<TableC PK_C="4" FK_A="2"><C1>Cvalue4</C1><C2>Cvalue4</C2><C3>Cvalue4</C3><C4>Cvalue4</C4><C5>Cvalue4</C5></TableC>', 2);
WITH CTEB (PK_A, XA, XB)
AS (SELECT A.PK_A,
a.XA,
b.XB
FROM @A AS a, @B as b
WHERE b.FK_A = a.PK_A)
UPDATE CTEB
SET XA.modify('insert sql:column("XB") as first into (/TableA)[1]');
WITH CTEC (PK_A, XA, XC)
AS (SELECT A.PK_A,
a.XA,
c.XC
FROM @A AS a
INNER JOIN
@C AS c
ON c.FK_A = a.PK_A)
UPDATE CTEC
SET XA.modify('insert sql:column("XC") as first into (/TableA)[1]');
SELECT * FROM @A;
This updates the XML only with the first XML fragment from Table B and Table C.
Update: For anyone who might come across this question, please take a look at @Shnugo 's answer along with the marked answer. Both approaches are perfect. I marked @gofr1 's solution as the answer simply because he was the first to reply. Another consideration for future quest hunters is whether you like CTE or sub-select (as Shnugo pointed out).