How can i load an XML file to SQL - sql

Im trying to load an XML file to SQL Server, but i m not getting anything
Here is my XML File:
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="http://www.demandware.com/xml/impex/catalog/2006-10-31" catalog-id="master-catalog-sso-us">
<header>
<image-settings>
<internal-location base-path="/"/>
<view-types>
<view-type>large</view-type>
<view-type>medium</view-type>
<view-type>small</view-type>
<view-type>swatch</view-type>
</view-types>
<alt-pattern>${productname}</alt-pattern>
<title-pattern>${productname}</title-pattern>
</image-settings>
</header>
<category category-id="root">
<display-name xml:lang="x-default">root Category</display-name>
<description xml:lang="x-default">root Category</description>
<online-flag>true</online-flag>
<template/>
<page-attributes/>
<refinement-definitions>
<refinement-definition type="attribute" bucket-type="none" attribute-id="shops" system="false">
<display-name xml:lang="x-default">Shops</display-name>
<value-set>search-result</value-set>
<sort-mode>value-name</sort-mode>
<sort-direction>ascending</sort-direction>
<cutoff-threshold>5</cutoff-threshold>
</refinement-definition>
</refinement-definitions>
</category>
<category category-id="default">
<display-name xml:lang="x-default">default Category</display-name>
<description xml:lang="x-default">default Category</description>
<online-flag>true</online-flag>
<parent>root</parent>
<template/>
<page-attributes/>
</category>
<product product-id="0217328320-sso-us">
<ean/>
<upc/>
<unit/>
<min-order-quantity>1</min-order-quantity>
<step-quantity>1</step-quantity>
<display-name xml:lang="x-default">Magnetibook - 4 Seasons</display-name>
<long-description xml:lang="x-default">48 magnets that stick to the metallic &#34;canvas&#34; where your child can choose between any of the four season &#34;back drops&#34; and then pick out the appropriate outfits and dress the family for a day outside. Comes in a magnetic closing &#34;book&#34; for safe keeping and easy storage.</long-description>
<store-force-price-flag>false</store-force-price-flag>
<store-non-inventory-flag>false</store-non-inventory-flag>
<store-non-revenue-flag>false</store-non-revenue-flag>
<store-non-discountable-flag>false</store-non-discountable-flag>
<online-flag>true</online-flag>
<online-from>2017-01-01T05:00:00.000Z</online-from>
<available-flag>true</available-flag>
<searchable-flag>false</searchable-flag>
<tax-class-id>standard</tax-class-id>
<brand>Janod</brand>
<manufacturer-name>Juratoys Company</manufacturer-name>
<sitemap-included-flag site-id="rco-us">true</sitemap-included-flag>
<sitemap-changefrequency site-id="rco-us">weekly</sitemap-changefrequency>
<sitemap-priority site-id="rco-us">1.0</sitemap-priority>
<page-attributes/>
<custom-attributes>
<custom-attribute attribute-id="ID">6339080</custom-attribute>
How do i load that ID field into SQL? I tried this:
But i believe my cross apply c.nodes is wrong! What Markups should i use?
Thanks
SELECT a.id.query('id').value('.','varchar(50)') as id FROM
( SELECT CAST(C AS XML) FROM OPENROWSET (BULK '\\test\master.xml', SINGLE_BLOB ) as T(c) ) AS S(c)
cross apply c.nodes('product/id') as A(id)

You ca try this:
SELECT c.id.query('id').value('.','varchar(50)') as id FROM
( SELECT CAST(C AS XML) FROM OPENROWSET (BULK '\\test\master.xml', SINGLE_BLOB ) as T(c) ) AS T(c)
cross apply c.nodes('product/id') as c(id)
But in your shared xml couldn't find an id tag within product tag.
Here I am sharing an Example:
XML File content:
<?xml version="1.0" encoding="utf-8"?>
<Customers>
<Customer>
<Document>000 000 000</Document>
<Name>Mary Angel</Name>
<Address>Your City, YC 1212</Address>
<Profession>Systems Analyst</Profession>
</Customer>
<Customer>
<Document>000 000 001</Document>
<Name>John Lenon</Name>
<Address>Your City, YC 1212</Address>
<Profession>Driver</Profession>
</Customer>
<Customer>
<Document>000 000 002</Document>
<Name>Alice Freeman</Name>
<Address>Your City, YC 1212</Address>
<Profession>Architect</Profession>
</Customer>
<Customer>
<Document>000 000 003</Document>
<Name>George Sands</Name>
<Address>Your City, YC 1212</Address>
<Profession>Doctor</Profession>
</Customer>
<Customer>
<Document>000 000 004</Document>
<Name>Mark Oliver</Name>
<Address>Your City, YC 1212</Address>
<Profession>Writer</Profession>
</Customer>
</Customers>
Copy above content and save as a xml file in d drive named testxmlfile.xml
Then try below query:
SELECT
MY_XML.Customer.query('Document').value('.', 'VARCHAR(20)') Document,
MY_XML.Customer.query('Name').value('.', 'VARCHAR(50)') Name,
MY_XML.Customer.query('Address').value('.', 'VARCHAR(50)')Address,
MY_XML.Customer.query('Profession').value('.', 'VARCHAR(50)' )Profession
FROM (SELECT CAST(MY_XML AS xml)
FROM OPENROWSET(BULK 'd:\testxmlfile.xml', SINGLE_BLOB) AS T(MY_XML)) AS T(MY_XML)
CROSS APPLY MY_XML.nodes('Customers/Customer') AS MY_XML (Customer);
Output:
Document Name Address Profession
000 000 000 Mary Angel Your City, YC 1212 Systems Analyst
000 000 001 John Lenon Your City, YC 1212 Driver
000 000 002 Alice Freeman Your City, YC 1212 Architect
000 000 003 George Sands Your City, YC 1212 Doctor
000 000 004 Mark Oliver Your City, YC 1212 Writer

You haven't been clear about what exactly you want, but it looks like you want the attribute product-id from here:
<product product-id="0217328320-sso-us">
You haven't taken into account namespaces, you need to add that.
It's also unclear if you have multiple product nodes. If you do, then you need this
WITH XMLNAMESPACES (DEFAULT 'http://www.demandware.com/xml/impex/catalog/2006-10-31')
SELECT A.product.value('#product-id','varchar(50)') as id
FROM
( SELECT CAST(C AS XML)
FROM OPENROWSET (BULK '\\test\master.xml', SINGLE_BLOB ) as T(c)
) AS S(c)
cross apply S.c.nodes('/catalog/product') as A(product)
If you have only one product node then you don't need .values at all:
WITH XMLNAMESPACES (DEFAULT 'http://www.demandware.com/xml/impex/catalog/2006-10-31')
SELECT S.c.value('/catalog/product/#product-id','varchar(50)') as id FROM
FROM
( SELECT CAST(C AS XML)
FROM OPENROWSET (BULK '\\test\master.xml', SINGLE_BLOB ) as T(c)
) AS S(c)

Related

How to insert with namespace

U have a XML file. I want to add this data to an SQL table. But something is wrong with the namespace. But I don't know what. please help me.
<?xml version="1.0" encoding="utf-8"?>
<sl:bagStand>
<Customer>
<Document>000 000 000</Document>
<names>
<Name>Mary Angel</Name>
</names>
<Address>Your City, YC 1212</Address>
<prop><Profession>Systems Analyst</Profession></prop>
</Customer>
</sl:bagStand>
I want to run this insert query
;WITH XMLNAMESPACES('sl' as x)
INSERT INTO CUSTOMERS_TABLE (DOCUMENT, NAME, ADDRESS, PROFESSION)
SELECT
MY_XML.Customer.query('Document').value('.', 'VARCHAR(20)'),
MY_XML.Customer.query('Name').value('.', 'VARCHAR(50)'),
MY_XML.Customer.query('Address').value('.', 'VARCHAR(50)'),
MY_XML.Customer.query('Profession').value('.', 'VARCHAR(50)')
FROM (SELECT CAST(MY_XML AS xml)
FROM OPENROWSET(BULK 'C:\temp\MSSQLTIPS_XML.xml', SINGLE_BLOB) AS T(MY_XML)) AS T(MY_XML)
CROSS APPLY MY_XML.nodes('x:bagStand/Customer') AS MY_XML (Customer);
Now i get a error:
Msg 9459, Level 16, State 1, Line 1
XML parsing: line 2, character 13, undeclared prefix
what am i doing wrong?

Convert XML to SQL Server table (cfdi document)

I'm trying to convert a XML to a SQL Server table.
The XML is the following (sample):
declare #XML as XML
set #XML = '<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/3" xmlns:nomina12="http://www.sat.gob.mx/nomina12" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Fecha="2020-02-15">
<cfdi:Emisor Rfc="12345" />
<cfdi:Conceptos>
<cfdi:Concepto ClaveProdServ="84111505" Cantidad="1" />
</cfdi:Conceptos>
<cfdi:Complemento>
<tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/cfd/timbrefiscaldigital/TimbreFiscalDigitalv11.xsd"
Version="1.1" UUID="28C9D7DC-A9CB-4663-9606-3B7E37D922D4" FechaTimbrado="2020-04-15T18:59:18" />
<nomina12:Nomina Version="1.2" TipoNomina="O" FechaPago="2020-04-15" FechaInicialPago="2020-04-01" FechaFinalPago="2020-04-15" >
<nomina12:Emisor RegistroPatronal="Z2935172101" />
<nomina12:Receptor Curp="XYZ" />
<nomina12:Percepciones TotalSueldos="6807.63" TotalGravado="5865.48" TotalExento="942.15">
<nomina12:Percepcion TipoPercepcion="001" Clave="P001" Concepto="Sueldo Normal" ImporteGravado="4887.90" ImporteExento="0.00" />
<nomina12:Percepcion TipoPercepcion="005" Clave="P013" Concepto="Fondo Ahorro Empresa" ImporteGravado="0.00" ImporteExento="342.15" />
</nomina12:Percepciones>
<nomina12:Deducciones TotalOtrasDeducciones="1282.01" TotalImpuestosRetenidos="610.94">
<nomina12:Deduccion TipoDeduccion="001" Clave="D003" Concepto="IMSS" Importe="70.40" />
<nomina12:Deduccion TipoDeduccion="001" Clave="D034" Concepto="Cuota IMSS RCV" Importe="58.66" />
</nomina12:Deducciones>
</nomina12:Nomina>
</cfdi:Complemento>
</cfdi:Comprobante>'
My SQL code is the following:
;WITH XMLNAMESPACES(
'http://www.sat.gob.mx/cfd/3' as cfdi,
'http://www.w3.org/2001/XMLSchema-instance' as xsi,
'http://www.sat.gob.mx/TimbreFiscalDigital' as tfd,
'http://www.sat.gob.mx/TimbreFiscalDigital' as schemaLocation,
'http://www.sat.gob.mx/nomina12' as nomina12)
SELECT
xmldata.value('(#Fecha)', 'varchar(20)') AS fecha_elaboracion,
xmldata.value('(cfdi:Emisor/#Rfc)[1]', 'varchar(20)') AS rfc_emisor,
xmldata1.value('(#UUID)', 'varchar(100)') AS UUID,
xmldata1.value('(#FechaTimbrado)', 'varchar(50)') AS fecha_timbre,
xmldata2.value('(#FechaPago)', 'nvarchar(50)') AS fecha_pago,
xmldata2.value('(#FechaInicialPago)', 'nvarchar(50)') AS fecha_inicio,
xmldata2.value('(#FechaFinalPago)', 'nvarchar(50)') AS fecha_final
FROM
(SELECT #XML AS x) AS x1
CROSS APPLY
x.nodes('/cfdi:Comprobante') AS a(xmldata)
CROSS APPLY
xmldata.nodes('cfdi:Complemento/tfd:TimbreFiscalDigital') AS a1(xmldata1)
OUTER APPLY
xmldata.nodes('cfdi:Complemento/tfd:TimbreFiscalDigital/nomina12:Nomina') AS a2(xmldata2);
I'm getting information for the first nodes but when I want to get the node "nomina12:Nomina", I got a null VALUE, some idea what's wrong?
Thanks in advance for your help.
Somewhat cleaned up version.
I removed not needed namespace declarations, and adjusted proper data types.
SQL
DECLARE #xml XML =
N'<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/3"
xmlns:nomina12="http://www.sat.gob.mx/nomina12"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Fecha="2020-02-15">
<cfdi:Emisor Rfc="12345"/>
<cfdi:Conceptos>
<cfdi:Concepto ClaveProdServ="84111505" Cantidad="1"/>
</cfdi:Conceptos>
<cfdi:Complemento>
<tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/cfd/timbrefiscaldigital/TimbreFiscalDigitalv11.xsd"
Version="1.1"
UUID="28C9D7DC-A9CB-4663-9606-3B7E37D922D4"
FechaTimbrado="2020-04-15T18:59:18"/>
<nomina12:Nomina Version="1.2" TipoNomina="O" FechaPago="2020-04-15"
FechaInicialPago="2020-04-01"
FechaFinalPago="2020-04-15">
<nomina12:Emisor RegistroPatronal="Z2935172101"/>
<nomina12:Receptor Curp="XYZ"/>
<nomina12:Percepciones TotalSueldos="6807.63" TotalGravado="5865.48"
TotalExento="942.15">
<nomina12:Percepcion TipoPercepcion="001" Clave="P001"
Concepto="Sueldo Normal"
ImporteGravado="4887.90"
ImporteExento="0.00"/>
<nomina12:Percepcion TipoPercepcion="005" Clave="P013"
Concepto="Fondo Ahorro Empresa"
ImporteGravado="0.00"
ImporteExento="342.15"/>
</nomina12:Percepciones>
<nomina12:Deducciones TotalOtrasDeducciones="1282.01"
TotalImpuestosRetenidos="610.94">
<nomina12:Deduccion TipoDeduccion="001" Clave="D003"
Concepto="IMSS" Importe="70.40"/>
<nomina12:Deduccion TipoDeduccion="001" Clave="D034"
Concepto="Cuota IMSS RCV" Importe="58.66"/>
</nomina12:Deducciones>
</nomina12:Nomina>
</cfdi:Complemento>
</cfdi:Comprobante>';
;WITH XMLNAMESPACES('http://www.sat.gob.mx/cfd/3' as cfdi
, 'http://www.sat.gob.mx/TimbreFiscalDigital' as tfd
, 'http://www.sat.gob.mx/nomina12' as nomina12)
SELECT xmldata.value('#Fecha', 'DATE') AS fecha_elaboracion
, xmldata.value('(cfdi:Emisor/#Rfc)[1]', 'VARCHAR(20)') AS rfc_emisor
, xmldata1.value('#UUID', 'UNIQUEIDENTIFIER') AS UUID
, xmldata1.value('#FechaTimbrado', 'VARCHAR(50)') AS fecha_timbre
, xmldata2.value('#FechaPago', 'DATE') AS fecha_pago
, xmldata2.value('#FechaInicialPago', 'DATE') AS fecha_inicio
, xmldata2.value('#FechaFinalPago', 'DATE') AS fecha_final
FROM #xml.nodes('/cfdi:Comprobante') AS a(xmldata)
OUTER APPLY xmldata.nodes('cfdi:Complemento/tfd:TimbreFiscalDigital') AS a1(xmldata1)
OUTER APPLY xmldata.nodes('cfdi:Complemento/nomina12:Nomina') AS a2(xmldata2);
Output
+-------------------+------------+--------------------------------------+---------------------+------------+--------------+-------------+
| fecha_elaboracion | rfc_emisor | UUID | fecha_timbre | fecha_pago | fecha_inicio | fecha_final |
+-------------------+------------+--------------------------------------+---------------------+------------+--------------+-------------+
| 2020-02-15 | 12345 | 28C9D7DC-A9CB-4663-9606-3B7E37D922D4 | 2020-04-15T18:59:18 | 2020-04-15 | 2020-04-01 | 2020-04-15 |
+-------------------+------------+--------------------------------------+---------------------+------------+--------------+-------------+
You are refering to the wrong path in your outer apply statement:
xmldata.nodes('cfdi:Complemento/nomina12:Nomina') AS a2(xmldata2);
And also here is the simplified version of your query:
WITH XMLNAMESPACES ('http://www.sat.gob.mx/cfd/3' AS cfdi
, 'http://www.w3.org/2001/XMLSchema-instance' AS xsi
, 'http://www.sat.gob.mx/TimbreFiscalDigital' AS tfd
, 'http://www.sat.gob.mx/TimbreFiscalDigital' AS schemaLocation
, 'http://www.sat.gob.mx/nomina12' AS nomina12
)
SELECT
xmldata.value('(/cfdi:Comprobante/#Fecha)[1]', 'varchar(20)') AS fecha_elaboracion
, xmldata.value('(/cfdi:Comprobante/cfdi:Emisor/#Rfc)[1]', 'varchar(20)') AS rfc_emisor
, xmldata.value('(/cfdi:Comprobante/cfdi:Complemento/tfd:TimbreFiscalDigital/#UUID)[1]', 'varchar(100)') AS UUID
, xmldata.value('(/cfdi:Comprobante/cfdi:Complemento/tfd:TimbreFiscalDigital/#FechaTimbrado)[1]', 'varchar(50)') AS fecha_timbre
, xmldata.value('(/cfdi:Comprobante/cfdi:Complemento/nomina12:Nomina/#FechaPago)[1]', 'nvarchar(50)') AS fecha_pago
, xmldata.value('(/cfdi:Comprobante/cfdi:Complemento/nomina12:Nomina/#FechaInicialPago)[1]', 'nvarchar(50)') AS fecha_inicio
, xmldata.value('(/cfdi:Comprobante/cfdi:Complemento/nomina12:Nomina/#FechaFinalPago)[1]', 'nvarchar(50)') AS fecha_final
FROM
(SELECT #XML AS xmldata) AS xmldata

SQL xml merge first row values with rest of the rows

I would like to know in SQL Server how do I take the following XML and represent the following table.
<Family>
<Main Surname="Smith" />
<Person Name="Fred" />
<Person Name="Jane" />
</Family>
Row Name Surname
1 Fred Smith
2 Jane Smith
Please note I cannot change the XML schema.
Here is my current SQL query.
declare #Input XML
set #Input = '<Family> <Main Surname="Smith" /> <Person Name="Fred" /> <Person Name="Jane" /> </Family>'
SELECT
Test.value('(#Name)[1]', 'varchar(20)') as Name,
Test.value('(#Surname)[1]', 'varchar(20)') as Surname
FROM #Input.nodes('/Family/*') AS Tbl(Test)
OPTION ( OPTIMIZE FOR ( #Input = NULL ) )
DECLARE #xml XML = '
<Family>
<Main Surname="Smith" />
<Person Name="Fred" />
<Person Name="Jane" />
</Family>'
SELECT
RowNum = ROW_NUMBER() OVER (ORDER BY 1/0)
, t.c.value('#Name', 'VARCHAR(20)')
, t2.c2.value('#Surname', 'VARCHAR(20)')
FROM #xml.nodes('Family/Person') t(c)
CROSS APPLY t.c.nodes('../Main') t2(c2)
results -
-------------------- -------------------- --------------------
1 Fred Smith
2 Jane Smith

Compare two sets of XML data using XQuery in SQL Server

Suppose I store employee data in a xml column in my log table. Sometimes data is also updated in the xml column from a stored procedure.
Here is the sample example
DECLARE #XML1 XML
DECLARE #XML2 XML
SET #XML1 =
'<NewDataSet>
<Employee>
<EmpID>1005</EmpID>
<Name> keith </Name>
<DOB>12/02/1981</DOB>
<DeptID>ACC001</DeptID>
<Salary>10,500</Salary>
</Employee>
</NewDataSet>'
SET #XML2 =
'<NewDataSet>
<Employee>
<EmpID>1006</EmpID>
<Name> keith </Name>
<DOB>05/02/1981</DOB>
<DeptID>ACC002</DeptID>
<Salary>10,900</Salary>
</Employee>
</NewDataSet>'
There is some difference in two the xml data which I need to show like old value & new value as a output of sql
Old Value New Value
--------- ---------
1005 1006
12/02/1981 05/02/1981
ACC001 ACC002
10,500 10,900
I just need to show the difference like above. So please guide me how to compare two xml data using XQuery and show the difference only in the above fashion in SQL Server. Please guide me with code snippet. thanks
;with XML1 as
(
select T.N.value('local-name(.)', 'nvarchar(100)') as NodeName,
T.N.value('.', 'nvarchar(100)') as Value
from #XML1.nodes('/NewDataSet/Employee/*') as T(N)
),
XML2 as
(
select T.N.value('local-name(.)', 'nvarchar(100)') as NodeName,
T.N.value('.', 'nvarchar(100)') as Value
from #XML2.nodes('/NewDataSet/Employee/*') as T(N)
)
select coalesce(XML1.NodeName, XML2.NodeName) as NodeName,
XML1.Value as Value1,
XML2.Value as Value2
from XML1
full outer join XML2
on XML1.NodeName = XML2.NodeName
where coalesce(XML1.Value, '') <> coalesce(XML2.Value, '')
Result:
NodeName Value1 Value2
-------------------- -------------------- --------------------
EmpID 1005 1006
DOB 12/02/1981 05/02/1981
DeptID ACC001 ACC002
Salary 10,500 10,900
I don't have the exact output you wanted - but at least you get a good comparison of old and new values:
;WITH OldData AS
(
SELECT
#XML1.value('(/NewDataSet/Employee/EmpID)[1]', 'int') AS 'EmpID',
#XML1.value('(/NewDataSet/Employee/Name)[1]', 'varchar(50)') AS 'Name',
#XML1.value('(/NewDataSet/Employee/DOB)[1]', 'datetime') AS 'DOB',
#XML1.value('(/NewDataSet/Employee/DeptID)[1]', 'varchar(50)') AS 'DeptID',
#XML1.value('(/NewDataSet/Employee/Salary)[1]', 'varchar(25)') AS 'Salary'
),
NewData AS
(
SELECT
#XML2.value('(/NewDataSet/Employee/EmpID)[1]', 'int') AS 'EmpID',
#XML2.value('(/NewDataSet/Employee/Name)[1]', 'varchar(50)') AS 'Name',
#XML2.value('(/NewDataSet/Employee/DOB)[1]', 'datetime') AS 'DOB',
#XML2.value('(/NewDataSet/Employee/DeptID)[1]', 'varchar(50)') AS 'DeptID',
#XML2.value('(/NewDataSet/Employee/Salary)[1]', 'varchar(25)') AS 'Salary'
)
SELECT
'Old values', od.*
FROM OldData od
UNION
SELECT 'New values', nd.*
FROM NewData nd
Gives you an output of:
EmpID Name DOB DeptID Salary
Old values 1005 keith 1981-12-02 00:00:00.000 ACC001 10,500
New values 1006 keith 1981-05-02 00:00:00.000 ACC002 10,900
SQL Server is great for storing and manipulating data - but presentation like this should be done in a front-end application (like an ASP.NET application) - not in T-SQL....
I am too late here !!! However I found that if employees XML as shown above has multiple records then the JOIN query with CTE returns incorrect results.
I have below XML input
DECLARE #XML1 XML
DECLARE #XML2 XML
SET #XML1 =
'<NewDataSet>
<Employees>
<Employee>
<Name> keith </Name>
<EmpID> 1005 </EmpID>
<DOB>12/02/1981</DOB>
<DeptID>ACC001</DeptID>
<Salary>10,500</Salary>
</Employee>
<Employee>
<Name> keith </Name>
<EmpID> 1004 </EmpID>
<DOB>12/02/1981</DOB>
<DeptID>ACC001</DeptID>
<Salary>10,500</Salary>
</Employee>
</Employees>
</NewDataSet>'
SET #XML2 =
'<NewDataSet>
<Employees>
<Employee>
<Name> keith </Name>
<EmpID> 1005 </EmpID>
<DOB>12/02/1981</DOB>
<DeptID>ACC001</DeptID>
<Salary>10,500</Salary>
</Employee>
<Employee>
<Name> keith </Name>
<EmpID> 1004 </EmpID>
<DOB>12/02/1981</DOB>
<DeptID>ACC001</DeptID>
<Salary>10,501</Salary>
</Employee>
<Employee>
<Name> keith1 </Name>
<EmpID> 10040 </EmpID>
<DOB>12/02/1981</DOB>
<DeptID>ACC001</DeptID>
<Salary>10,501</Salary>
</Employee>
</Employees>
</NewDataSet>'
I will use below query to find the difference
select T.N.value('local-name(.)', 'nvarchar(100)') as NodeName,
T.N.value('.', 'nvarchar(100)') as Value
from #XML2.nodes('/NewDataSet/Employees/Employee/*') as T(N)
EXCEPT
select T.N.value('local-name(.)', 'nvarchar(100)') as NodeName,
T.N.value('.', 'nvarchar(100)') as Value
from #XML1.nodes('/NewDataSet/Employees/Employee/*') as T(N)
Hope this helps !!!

Transform XML to Table data using XQuery in SQL Server

Is it possible to achieve the following table from of output using XQuery in SQL Server
Edit: A change in my requirement, Consider i have a table which stores the below xml and also UserID
DECLARE #XML xml
set #XML = '<Security>
<FiscalYear Name="2012">
<Country Id="204">
<State Id="1">
<City Id="10"></City>
</State>
<State Id="2">
<City Id="20"></City>
<City Id="30"></City>
</State>
<State Id ="3"></State>
</Country >
</FiscalYear>
</Security>'
CREATE TABLE #tmp_user
(UserID INT,SecurityXML XML)
INSERT INTO #tmp_user
( UserID, SecurityXML )
VALUES ( 1,
#XML
)
Now how can i get a o/p like
Output:
UserID StateID CityID
1 1 10
1 2 20
1 2 30
1 3 0
Is it possible to achieve?
I modified your XML a bit because it was invalid. Change the end tag </Subsidiary> to </Country>.
declare #XML xml
set #XML =
'<Security>
<FiscalYear Name="2012">
<Country Id="204">
<State Id="1">
<City Id="10"></City>
</State>
<State Id="2">
<City Id="20"></City>
<City Id="30"></City>
</State>
<State Id ="3"></State>
</Country>
</FiscalYear>
</Security>'
select S.N.value('#Id', 'int') as StateID,
coalesce(C.N.value('#Id', 'int'), 0) as CityID
from #XML.nodes('/Security/FiscalYear/Country/State') as S(N)
outer apply S.N.nodes('City') as C(N)
A version using a table instead of XML variable
select T.UserID,
S.N.value('#Id', 'int') as StateID,
coalesce(C.N.value('#Id', 'int'), 0) as CityID
from #tmp_user as T
cross apply T.SecurityXML.nodes('/Security/FiscalYear/Country/State') as S(N)
outer apply S.N.nodes('City') as C(N)