Update Xml Field with replace value of (XML DML) - sql-server

I face the situation to update an xml field data with mutliple rows within it.
The XML looks like this ( a cart data containing multiple items )
<cart>
<items>
<ItemId>111613</ItemId>
<ItemCode>P.KNT330.01.85.1.1.1</ItemCode>
<ItemName>KANAT330 85 TEAK PETEK ODA PVT</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>1</Quantity>
<MU>AD</MU>
<Price_Net>143.50</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>143.50</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>110.997250</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>19.97950500</VAT_TL>
<Price_Gross_TL>130.97675500</Price_Gross_TL>
<LineDesc />
</items>
<items>
<ItemId>116950</ItemId>
<ItemCode>KS.220.S.0850.51.00</ItemCode>
<ItemName>220 LIK D.TEAK 85 LIK KNT YÖNSÜZ KASA</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>1.000</Quantity>
<MU>TK</MU>
<Price_Net>115.20</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>115.20</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>89.107200</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>16.03929600</VAT_TL>
<Price_Gross_TL>105.14649600</Price_Gross_TL>
<LineDesc />
</items>
<items>
<ItemId>112357</ItemId>
<ItemCode>PRV.071.S.51</ItemCode>
<ItemName>PERVAZ 70 DÜZ D.TEAK TAKIM</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>1.000</Quantity>
<MU>TK</MU>
<Price_Net>45.80</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>45.80</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>35.426300</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>6.37673400</VAT_TL>
<Price_Gross_TL>41.80303400</Price_Gross_TL>
<LineDesc />
</items>
<items>
<ItemId>108561</ItemId>
<ItemCode>34016-13030</ItemCode>
<ItemName>IÇ ODA KAPI KILIDI NIKEL 141-45</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>1.000</Quantity>
<MU>AD</MU>
<Price_Net>10.35</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>10.35</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>8.005725</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>1.44103050</VAT_TL>
<Price_Gross_TL>9.44675550</Price_Gross_TL>
<LineDesc />
</items>
<items>
<ItemId>108568</ItemId>
<ItemCode>34026-11160</ItemCode>
<ItemName>ADOKAPI KÖSE BIRL TKZ TAKIMI</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>1.000</Quantity>
<MU>AD</MU>
<Price_Net>1.80</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>1.80</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>1.392300</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>0.25061400</VAT_TL>
<Price_Gross_TL>1.64291400</Price_Gross_TL>
<LineDesc />
</items>
<items>
<ItemId>108543</ItemId>
<ItemCode>34006-70370</ItemCode>
<ItemName>ADOKAPI MENTESE 10401.0000.0.2</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>3.000</Quantity>
<MU>AD</MU>
<Price_Net>1.30</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>1.30</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>1.005550</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>0.18099900</VAT_TL>
<Price_Gross_TL>1.18654900</Price_Gross_TL>
<LineDesc />
</items>
<items>
<ItemId>108575</ItemId>
<ItemCode>34026-90300</ItemCode>
<ItemName>ADOKAPI ROZETI NIKEL 20298.0000.0.2</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>2.000</Quantity>
<MU>AD</MU>
<Price_Net>0.45</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>0.45</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>0.348075</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>0.06265350</VAT_TL>
<Price_Gross_TL>0.41072850</Price_Gross_TL>
<LineDesc />
</items>
<items>
<ItemId>115240</ItemId>
<ItemCode>34024-35016</ItemCode>
<ItemName>TPE ADOKAPI CONTA AÇIK KAHVE</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>5.000</Quantity>
<MU>MT</MU>
<Price_Net>0.36</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>0.36</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>0.278460</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>0.05012280</VAT_TL>
<Price_Gross_TL>0.32858280</Price_Gross_TL>
<LineDesc />
</items>
<GenDiscPercent_1 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<GenDiscPercent_2 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<GenDiscPercent_3 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<ResChar1 />
<ResChar2>A-0201-1748</ResChar2>
<ResChar3 />
<ResNum1 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<ResNum2 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<ResNum3 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<ResNum4 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<ResDate1>2016-02-18T00:00:00</ResDate1>
<ResDate2>2016-02-18T00:00:00</ResDate2>
<ResDate3 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<ResDate4 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
</cart>
How to modify (let's say) the price of a specific item (with ItemId 116950) in this xml field data?

I assume, that your table contains several rows with the same structure. Do you want to change the price in one row, or - might be dangerous - in all rows?
For this I assume, that you want to change one row. Therefore I'll add a WHERE-clause to the end. If you have to change all, you can change this with CART_DATA.exist(...)=1. This was the fastes XML-based approach to get only rows which really contain the given ItemId.
But: Why is this XML?
The design cries for a classical 1:n-relation via physically defined tables. XML is good for final storage (archives), for interfaces, web-service (data transmission in general), but not for heavily moving data, or data you want to look into with filters or statistical question.
A solution
Nevertheless, this is the code:
Change #YourTable and YourXMLColumn to the actual names.
--Two variables:
DECLARE #itemID INT=116950;
DECLARE #newPrice DECIMAL(10,4)=0.99;
--This will display the price as-is
SELECT t.YourXMLColumn.value('(/cart/items[ItemId=sql:variable("#itemID")]/Price_Net/text())[1]','decimal(10,4)')
FROM #YourTable AS t
WHERE t.ID=1;
--This will modify the price
UPDATE #YourTable
SET YourXMLColumn.modify(N'replace value of (/cart/items[ItemId=sql:variable("#itemID")]/Price_Net/text())[1] with sql:variable("#newPrice")')
WHERE ID=1;
--This will display the new price if the operation was successfull
SELECT t.YourXMLColumn.value('(/cart/items[ItemId=sql:variable("#itemID")]/Price_Net/text())[1]','decimal(10,4)')
FROM #YourTable AS t
WHERE t.ID=1;

Let's say the xml field is called CART_DATA in the table called GM_CART.
We will also use sql-variables as in most cases you will need to run your query with parameters.
DECLARE #cartid int = 1000, #itemid int = 116950, #newprice float = 99.90
UPDATE GM_CART
SET CART_DATA.modify('replace value of (/cart/items/Price_Net/text()[.>>(/cart/items/ItemId[.=sql:variable("#itemid")])[1] ])[1]
with sql:variable("#newprice")')
WHERE CARTID=#cartid
to update other nodes if neccesary, you have to call the sql update statement seperately for each one of them

Related

Generate Database Schema information as XML

I am trying to generate XML based on an SQL Server Schema. I have followed a few references including: (How to convert database table structure to XML file in sql server?)
After some trial and error, I have gotten as far as:
SELECT TABLE_NAME as '#tablename',
(
SELECT
DATA_TYPE as 'Column/#datatype',
case data_type
when 'nvarchar'
then CHARACTER_MAXIMUM_LENGTH
when 'varchar'
then CHARACTER_MAXIMUM_LENGTH
else null
end as 'Column/#Length',
case IS_NULLABLE
when 'NO' --caseinsensitive by default
then 'False'
when 'YES'
then 'True'
else null
end
AS 'Column/#is_nullable',
Column_Name as 'Column'
FROM INFORMATION_SCHEMA.COLUMNS
where INFORMATION_SCHEMA.COLUMNS.TABLE_NAME =
INFORMATION_SCHEMA.TABLES.TABLE_NAME
order by INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION
-- For XML Path('Column'), type
For XML Path, type
)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='dbo'
ORDER BY TABLE_NAME ASC
For XML PATH ('Table'),Root('Tables')
What I get is:
<Tables>
<Table tablename="Table1">
<row>
<Column datatype="int" is_nullable="False">AA</Column>
</row>
<row>
<Column datatype="nvarchar" Length="50" is_nullable="True">Name</Column>
</row>
<row>
<Column datatype="bit" is_nullable="False">Active</Column>
</row>
<row>
<Column datatype="timestamp" is_nullable="False">CreatedDate</Column>
</row>
</Table>
<Table tablename="Table2">
<row>
<Column datatype="int" is_nullable="False">AA</Column>
</row>
<row>
<Column datatype="datetime2" is_nullable="True">CreatedDate</Column>
</row>
</Table>
</Tables>
The expected output would have the following structure.
<Tables>
<Table tablename="Table1">
<Columns>
<Column datatype="int" is_nullable="False">AA</Column>
<Column datatype="nvarchar" Length="50" is_nullable="True">Name</Column>
<Column datatype="bit" is_nullable="False">Active</Column>
<Column datatype="timestamp" is_nullable="False">CreatedDate</Column>
</Columns>
</Table>
<Table tablename="Table2">
<Columns>
<Column datatype="int" is_nullable="False">AA</Column>
<Column datatype="datetime" is_nullable="True">CreatedDate</Column>
<Columns>
</Table>
</Tables>
Can someone shed some light on
How to remove the individual <row></row> tags and replace with a single set of outer <columns></columns>
how to change datatype= datetime2 to datatype= datetime
Thank you for your help.
Your were close. Just take note of (...) as columns and the Path('') in the subquery
Updated... I missed the datetime requirement
Select TABLE_NAME as '#tablename'
,(
Select case when DATA_TYPE like 'datetime%' then 'datetime' else DATA_TYPE end as 'Column/#datatype',
case data_type
when 'nvarchar'
then CHARACTER_MAXIMUM_LENGTH
when 'varchar'
then CHARACTER_MAXIMUM_LENGTH
else null
end as 'Column/#Length',
case IS_NULLABLE
when 'NO' --caseinsensitive by default
then 'False'
when 'YES'
then 'True'
else null
end AS 'Column/#is_nullable',
Column_Name as 'Column'
FROM INFORMATION_SCHEMA.COLUMNS
where INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = INFORMATION_SCHEMA.TABLES.TABLE_NAME
order by INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION
For XML Path(''), type
) AS Columns
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='dbo'
ORDER BY TABLE_NAME ASC
For XML Path('Table'),Root('Tables')
Sample Results
<Tables>
<Table tablename="OD">
<Columns>
<Column datatype="int" is_nullable="False">OD-Nr</Column>
<Column datatype="int" is_nullable="True">OD-Pt</Column>
<Column datatype="int" is_nullable="True">OD-PS</Column>
<Column datatype="varchar" Length="50" is_nullable="False">OD-Class</Column>
<Column datatype="varchar" Length="250" is_nullable="False">OD-Title</Column>
<Column datatype="int" is_nullable="False">OD-LM-Usr</Column>
<Column datatype="datetime" is_nullable="False">OD-LM-UTC</Column>
<Column datatype="int" is_nullable="False">OD-Deleted</Column>
</Columns>
</Table>
<Table tablename="OD-Map">
<Columns>
<Column datatype="int" is_nullable="False">Map-Nr</Column>
<Column datatype="varchar" Length="50" is_nullable="True">Map-Grp</Column>
<Column datatype="varchar" Length="500" is_nullable="True">Map-Val1</Column>
<Column datatype="varchar" Length="500" is_nullable="True">Map-Val2</Column>
<Column datatype="varchar" Length="500" is_nullable="True">Map-Val3</Column>
<Column datatype="varchar" Length="500" is_nullable="True">Map-Val4</Column>
<Column datatype="int" is_nullable="True">Map-LM-Usr</Column>
<Column datatype="datetime" is_nullable="True">Map-LM-UTC</Column>
<Column datatype="bit" is_nullable="True">Map-Deleted</Column>
</Columns>
</Table>
</Tables>

How to parse XML which is stored in a column in SQL Server

I want to parse XML which is stored in a column in SQL Server:
Table column with XML in this picture
and the XML is:
<?xml version="1.0" encoding="UTF-8"?>
<propertylist id="root">
<property expanded="Y" id="createsdi" selectedindex="0" type="collection">
<collection>
<propertylist id="1305619640064">
<property expanded="Y" id="columnvalues" selectedindex="0" type="collection">
<collection>
<propertylist id="1396440519721" />
</collection>
</property>
<property expanded="Y" id="tests" selectedindex="0" type="collection">
<collection>
<propertylist id="p1602059752707" sequence="1000000">
<property id="id" type="simple"><![CDATA[item1]]></property>
<property id="workitemid" type="simple"><![CDATA[SOURAVTESTER|1]]></property>
</propertylist>
</collection>
</property>
<property expanded="Y" id="specs" selectedindex="0" type="collection">
<collection>
<propertylist id="p1602059825237" sequence="1000000">
<property id="id" type="simple"><![CDATA[item1]]></property>
<property id="specid" type="simple"><![CDATA[EMSpec|1]]></property>
</propertylist>
</collection>
</property>
</propertylist>
</collection>
</property>
<property expanded="Y" id="workorder" type="propertylist">
<propertylist id="root_0">
<property expanded="Y" id="graceperiod" type="propertylist">
<propertylist id="root_0_workorder_0">
<property expanded="Y" id="graceperiod" type="propertylist">
<propertylist id="root_0_workorder_0_graceperiod_0" />
</property>
<property expanded="Y" id="deviation" type="propertylist">
<propertylist id="root_0_workorder_0_graceperiod_0" />
</property>
</propertylist>
</property>
</propertylist>
</property>
</propertylist>
How can I parse the values of workitemid, specid?
I have written the following code
SELECT
ID,
ParentPropertyId = xc.value('(../#id)[1]', 'varchar(100)'),
WorkItemId = xc.value('(.)[1]', 'varchar(100)')
FROM
scheduleplanitem
CROSS APPLY
valuetree.nodes('//propertylist/property[#id="workitemid"]') xt(xc)
but I get an error
The XMLDT method 'nodes' can only be invoked on columns of type xml
The result should be:
workitemid SOURAVTESTER
specid EMSpec
Please try the following solution.
It does the following:
Removes XML prolog with the UTF-8 encoding. SQL Server implicitly converts any XML internally to UTF-16.
Converts NVARCHAR() data type to XML data type. After that XQuery
methods to handle XML become available.
SQL
-- DDL and sample data population, start
DECLARE #tbl TABLE (ID INT IDENTITY PRIMARY KEY, col NVARCHAR(MAX));
INSERT INTO #tbl (col) VALUES
(N'<?xml version="1.0" encoding="UTF-8"?>
<propertylist id="root">
<property expanded="Y" id="createsdi" selectedindex="0" type="collection">
<collection>
<propertylist id="1305619640064">
<property expanded="Y" id="columnvalues" selectedindex="0"
type="collection">
<collection>
<propertylist id="1396440519721"/>
</collection>
</property>
<property expanded="Y" id="tests" selectedindex="0"
type="collection">
<collection>
<propertylist id="p1602059752707" sequence="1000000">
<property id="id" type="simple"><![CDATA[item1]]></property>
<property id="workitemid" type="simple"><![CDATA[SOURAVTESTER|1]]></property>
</propertylist>
</collection>
</property>
<property expanded="Y" id="specs" selectedindex="0"
type="collection">
<collection>
<propertylist id="p1602059825237" sequence="1000000">
<property id="id" type="simple"><![CDATA[item1]]></property>
<property id="specid" type="simple"><![CDATA[EMSpec|1]]></property>
</propertylist>
</collection>
</property>
</propertylist>
</collection>
</property>
<property expanded="Y" id="workorder" type="propertylist">
<propertylist id="root_0">
<property expanded="Y" id="graceperiod" type="propertylist">
<propertylist id="root_0_workorder_0">
<property expanded="Y" id="graceperiod" type="propertylist">
<propertylist id="root_0_workorder_0_graceperiod_0"/>
</property>
<property expanded="Y" id="deviation" type="propertylist">
<propertylist id="root_0_workorder_0_graceperiod_0"/>
</property>
</propertylist>
</property>
</propertylist>
</property>
</propertylist>')
, (NULL);
-- DDL and sample data population, end
--DECLARE #startPos INT = LEN('<?xml version="1.0" encoding="utf-8"?>') + 1;
DECLARE #prolog NVARCHAR(100) = '<?xml version="1.0" encoding="utf-8"?>';
;WITH rs AS
(
SELECT ID
--, TRY_CAST(SUBSTRING(col
-- , #startPos
-- , LEN(col)) AS XML) AS xmldata
, TRY_CAST('<root>' + REPLACE(col, #prolog, '') + '</root>' AS XML) AS xmldata FROM #tbl
)
SELECT ID
, c.value('(./text())[1]', 'VARCHAR(100)') AS Item
FROM rs
CROSS APPLY xmldata.nodes('//property[#id=("specid","workitemid")]') AS t(c);
Output
+----+----------------+
| ID | Item |
+----+----------------+
| 1 | SOURAVTESTER|1 |
| 1 | EMSpec|1 |
+----+----------------+

Join XML columns from SQL Server 2016+

I have a TABLE_A with an XML column mycategoryXML which contains this XML:
<mainxml>
<category id="1" ftype="1"/>
<category id="2" ftype="1"/>
<category id="3" ftype="1"/>
</mainxml>
I also have another TABLE_B with a XML column called mymappedids
<mapids>
<ids>
<myid id="1" name="Category Name1"/>
<myid id="2" name="Category Name2"/>
<myid id="3" name="Category Name3"/>
<myid id="4" name="Category Name4"/>
<myid id="5" name="Category Name5"/>
<myid id="6" name="Category Name6"/>
<myid id="7" name="Category Name7"/>
<myid id="8" name="Category Name8"/>
<myid id="9" name="Category Name9"/>
</ids>
</mapids>
Is it possible to select all Category nodes from TABLE_A and also the correct names for each category from table_B, so I ultimately get the follow result:
id | categoryid | categoryname
---+------------+-------------
1 | 1 | categoryname1
2 | 2 | categoryname2
3 | 3 | categoryname3
with one query from SQL Server?
There are several approaches but you did not tell us enough about your background.
Especially unclear: How is the connection between these two tables a and b?
You might try something along this:
A mockup scenario to simulate your issue (please provide this yourself in your next question):
DECLARE #tblA TABLE(aId INT,Xml1 XML);
DECLARE #tblB TABLE(bId INT,Xml2 XML);
INSERT INTO #tblA(aId,Xml1) VALUES
(100
,N'<mainxml>
<category id="1" ftype="1"/>
<category id="2" ftype="1"/>
<category id="3" ftype="1"/>
</mainxml>');
INSERT INTO #tblB(bId,Xml2) VALUES
(200
,N'<mapids>
<ids>
<myid id="1" name="Category Name1"/>
<myid id="2" name="Category Name2"/>
<myid id="3" name="Category Name3"/>
<myid id="4" name="Category Name4"/>
<myid id="5" name="Category Name5"/>
<myid id="6" name="Category Name6"/>
<myid id="7" name="Category Name7"/>
<myid id="8" name="Category Name8"/>
<myid id="9" name="Category Name9"/>
</ids>
</mapids>');
--The query
SELECT a.aId
,aXmlValues.*
,(SELECT Xml2.value('(/mapids
/ids
/myid[#id=sql:column("aXmlValues.CategoryId")]
/#name)[1]','nvarchar(max)')
FROM #tblB b
WHERE bID=200 /*however you find this...*/) AS CategoryName
FROM #tblA a
CROSS APPLY a.Xml1.nodes('/mainxml/category') aXml(c)
CROSS APPLY(SELECT aXml.c.value('#id','int') AS CategoryId
,aXml.c.value('#ftype','int') AS CategoryType) aXmlValues;
The idea in short:
We use APPLY ... .nodes() to get the categories of table "a" in separate rows (a derived set).
now we can use APPLY again. This is a trick to get the XML's value into the result set as a normal column.
This column can be placed into XQuery against table "b" using sql:column().
I assume you likely want something like this:
DECLARE #XML1 xml = '<mainxml>
<category id="1" ftype="1"/>
<category id="2" ftype="1"/>
<category id="3" ftype="1"/>
</mainxml>';
DECLARE #XML2 xml = '<mapids>
<ids>
<myid id="1" name="Category Name1"/>
<myid id="2" name="Category Name2"/>
<myid id="3" name="Category Name3"/>
<myid id="4" name="Category Name4"/>
<myid id="5" name="Category Name5"/>
<myid id="6" name="Category Name6"/>
<myid id="7" name="Category Name7"/>
<myid id="8" name="Category Name8"/>
<myid id="9" name="Category Name9"/>
</ids>
</mapids>';
SELECT X1.c.value('#id','int') AS id,
X2.mi.value('#id','int') AS categoryid, --Seem silly to have this value twice
X2.mi.value('#name','varchar(30)') AS categoryname
FROM (VALUES(#XML1)) V1(X)
CROSS APPLY (VALUES(#XML2)) V2(X)
CROSS APPLY V1.X.nodes('/mainxml/category')X1(c)
CROSS APPLY V2.X.nodes('/mapids/ids/myid')X2(mi)
WHERE X1.c.value('#id','int') = X2.mi.value('#id','int');

NHibernate and sql timestamp columns as version

I've been racking my head trying to get Nhibernate to work with a byte
array as version mapping to an sql timestamp. I'd implemented an
IUserVersionType but Nhibernate was creating varbinary in the database
rather than timestamp. Inspired by a blog post by Ayende recently on
concurrency, I changed my mapping to specify the sql-type to timestamp
which worked perfectly. However I now face a rather curious problem
wherein Nhibernate does an insert, gets the new version and then
immediately tries to do an update and attempts to set the version
column, which being an sql timestamp fails.
This is my mapping:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Core.Domain, Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" namespace="Core.Domain.Entities"
default-lazy="false">
<class name="Contact" table="Contacts" xmlns="urn:nhibernate-
mapping-2.2" optimistic-lock="version" dynamic-insert="true" dynamic-
update="true">
<id name="Id" type="Int32" column="Id">
<generator class="identity" />
</id>
<version name="Version" type="BinaryBlob" generated="always"
unsaved-value="null">
<column name="Version" sql-type="timestamp" not-null="false" />
</version>
<property name="Title" type="String">
<column name="Title" length="5" />
</property>
<property name="FirstName" type="String">
<column name="FirstName" not-null="true" length="50" />
</property>
<property name="MiddleName" type="String">
<column name="MiddleName" length="50" />
</property>
<property name="LastName" type="String">
<column name="LastName" not-null="true" length="50" />
</property>
<property name="Suffix" type="String">
<column name="Suffix" length="5" />
</property>
<property name="Email" type="String">
<column name="Email" length="50" />
</property>
<bag name="PhoneNumbers" inverse="true" cascade="all-delete-
orphan">
<key foreign-key="FK_Contacts_PhoneNumbers_ContactId" on-
delete="cascade" column="ContactId" />
<one-to-many class="Core.Domain.Entities.PhoneNumber,
Core.Domain, Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" />
</bag>
<property name="DateCreated" type="DateTime">
<column name="DateCreated" />
</property>
<property name="DateModified" type="DateTime">
<column name="DateModified" />
</property>
<property name="LastModifiedBy" type="String">
<column name="LastModifiedBy" />
</property>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Core.Domain, Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" namespace="Core.Domain.Entities"
default-lazy="false">
<class name="Customer" table="Customers" xmlns="urn:nhibernate-
mapping-2.2" optimistic-lock="version" dynamic-insert="true" dynamic-
update="true">
<id name="Id" type="Int32" column="Id">
<generator class="identity" />
</id>
<version name="Version" type="BinaryBlob" generated="always"
unsaved-value="null">
<column name="Version" sql-type="timestamp" not-null="false" />
</version>
<property name="AccountNumber" access="nosetter.pascalcase-
underscore" type="String">
<column name="AccountNumber" unique="true" length="25" />
</property>
<!-- other mappings... -->
<property name="DateCreated" type="DateTime">
<column name="DateCreated" />
</property>
<property name="DateModified" type="DateTime">
<column name="DateModified" />
</property>
<property name="LastModifiedBy" type="String">
<column name="LastModifiedBy" />
</property>
<joined-subclass name="Core.Domain.Entities.Individual,
Core.Domain, Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" table="Individuals">
<key column="CustomerId" />
<many-to-one fetch="join" lazy="false" not-null="true"
cascade="all" unique="true" not-found="exception" name="Contact"
column="ContactID" />
<bag name="Addresses" table="Addresses_Individuals">
<key column="AddressId" foreign-
key="FK_Addresses_Individuals_Addresses_AddressId" />
<many-to-many column="IndividualId"
class="Core.Domain.Entities.Address, Core.Domain,
Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" foreign-
key="FK_Addresses_Individuals_Individuals_IndividualId" />
</bag>
</joined-subclass>
<joined-subclass name="Core.Domain.Entities.Store, Core.Domain,
Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" table="Stores">
<key column="CustomerId" />
<many-to-one unique="true" cascade="save-update" fetch="join"
not-null="true" not-found="exception" name="Address"
column="AddressId" />
<many-to-one lazy="proxy" not-null="true" cascade="all" not-
found="exception" name="Client" column="ClientId" />
<property name="StoreName" type="String">
<column name="StoreName" not-null="true" length="50" />
</property>
<bag name="Contacts" table="Contacts_Stores">
<key column="ContactId" foreign-
key="FK_Contacts_Stores_Contacts_ContactId" />
<many-to-many column="StoreId"
class="Core.Domain.Entities.Contact, Core.Domain,
Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" foreign-
key="FK_Contacts_Stores_Stores_StoreId" />
</bag>
</joined-subclass>
</class>
</hibernate-mapping>
Calling Session.Save on an Individual with associated Contact results
in the following error:
NHibernate: INSERT INTO Addresses (Line1, PostalCode, Country,
DateCreated, DateModified, LastModifiedBy) VALUES (#p0, #p1, #p2, #p3,
#p4, #p5); select SCOPE_IDENTITY(); #p0 = 'Order Address Line 1', #p1
= 'CV31 6BW', #p2 = 'United Kingdom', #p3 = '20/04/2009 19:45:32', #p4
= '20/04/2009 19:45:32', #p5 = ''
NHibernate: SELECT address_.Version as Version22_ FROM Addresses
address_ WHERE address_.Id=#p0; #p0 = '1'
NHibernate: INSERT INTO Contacts (FirstName, LastName, DateCreated,
DateModified, LastModifiedBy) VALUES (#p0, #p1, #p2, #p3, #p4); select
SCOPE_IDENTITY(); #p0 = 'Joe', #p1 = 'Bloggs', #p2 = '20/04/2009
19:45:34', #p3 = '20/04/2009 19:45:34', #p4 = ''
NHibernate: SELECT contact_.Version as Version33_ FROM Contacts
contact_ WHERE contact_.Id=#p0; #p0 = '1'
NHibernate: INSERT INTO Customers (AccountNumber, DateCreated,
DateModified, LastModifiedBy) VALUES (#p0, #p1, #p2, #p3); select
SCOPE_IDENTITY(); #p0 = '', #p1 = '20/04/2009 19:45:34', #p2 =
'20/04/2009 19:45:34', #p3 = ''
NHibernate: INSERT INTO Individuals (ContactID, CustomerId) VALUES
(#p0, #p1); #p0 = '1', #p1 = '1'
NHibernate: SELECT individual_1_.Version as Version2_ FROM Individuals
individual_ inner join Customers individual_1_ on
individual_.CustomerId=individual_1_.Id WHERE
individual_.CustomerId=#p0; #p0 = '1'
NHibernate: UPDATE Contacts SET Version = #p0 WHERE Id = #p1 AND
Version = #p2; #p0 = 'System.Byte[]', #p1 = '1', #p2 = 'System.Byte[]'
System.Data.SqlClient.SqlException: Cannot update a timestamp column.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception,
Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning
(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,
SqlCommand cmdHandler, SqlDataReader dataStream,
BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject
stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader
ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds
(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean
returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String
method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery
(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd)
in c:\CSharp\NH\nhibernate\src\NHibernate\AdoNet\AbstractBatcher.cs:
line 203
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object
id, Object[] fields, Object[] oldFields, Object rowId, Boolean[]
includeProperty, Int32 j, Object oldVersion, Object obj,
SqlCommandInfo sql, ISessionImplementor session) in c:\CSharp\NH
\nhibernate\src\NHibernate\Persister\Entity
\AbstractEntityPersister.cs: line 2713
NHibernate.Exceptions.GenericADOException: could not update:
[Core.Domain.Entities.Contact#1][SQL: UPDATE Contacts SET Version =
#p0 WHERE Id = #p1 AND Version = #p2]
Any ideas why NHibernate is attempting to update the version column
for Contact, even though it didn't for the Address?
I have found that using dynamic-insert="true" on the class along with causes this issue. I use the following mapping successfully:
...
<class name="Contact" table="Contact">
<id name="ID" column="ID" type="int">
<generator class="identity" />
</id>
<version name="Version" generated="always" unsaved-value="null" type="BinaryBlob"/>
...
The Address doesn't have a version column I assume.
I wonder where you have the sql-type from. Why not this way?
<version name="Version" type="Timestamp" generated="always" unsaved-value="null">
<column name="Version" not-null="false" />
</version>
You need of course a DateTime in the entity.
http://ayende.com/Blog/archive/2009/04/15/nhibernate-mapping-concurrency.aspx

SQL Server xml.modify delete method

I have a problem with removing an attribute from a node.
Example:
DECLARE #processID int
SET #processID = 8
DECLARE #xml XML
SET #xml =
'<Process id="10" name="Test 1">
<Shapes>
<Shape id="1" name="Shape 1" subProcessID="8">
</Shape>
<Shape id="2" name="Shape 2" subProcessID="9">
</Shape>
</Shapes>
<Lines />
</Process>'
SET #xml.modify('delete (/Process/Shapes/Shape/#subProcessID[/Process/Shapes/Shape/#subProcessID = sql:variable("#processID")])')
SELECT #xml
Gives the result:
<Process id="10" name="Test 1">
<Shapes>
<Shape id="1" name="Shape 1" />
<Shape id="2" name="Shape 2" />
</Shapes>
<Lines />
</Process>
What I would like is:
<Process id="10" name="Test 1">
<Shapes>
<Shape id="1" name="Shape 1" />
<Shape id="2" name="Shape 2" subProcessID="9" />
</Shapes>
<Lines />
</Process>
What is the syntax to achieve this?
As the OP is gone but he left the solution in a comment let me add that as an answer:
DECLARE #processID int
SET #processID = 8
DECLARE #xml XML
SET #xml =
'<Process id="10" name="Test 1">
<Shapes>
<Shape id="1" name="Shape 1" subProcessID="8">
</Shape>
<Shape id="2" name="Shape 2" subProcessID="9">
</Shape>
</Shapes>
<Lines />
</Process>'
SET #xml.modify('delete (/Process/Shapes/Shape[#subProcessID = sql:variable("#processID")]/#subProcessID)')
SELECT #xml
Here is a working sqlfiddle for that.

Resources