I am working with some XML and I have an issue.
The xml looks like this:
<tagvalues>
<tagvalue>
<tag>Data.Barcode</tag>
<value>True</value>
</tagvalue>
<tagvalue>
<tag>Data.DateStampFirstScale</tag>
<value>20180111</value>
</tagvalue>
<tagvalue>
<tag>Data.DateStampLastScale</tag>
<value>20180111</value>
</tagvalue>
<tagvalue>
<tag>Data.Dot</tag>
<value>False</value>
</tagvalue>
<tagvalue>
<tag>Data.Hangtab</tag>
<value>False</value>
</tagvalue>
<tagvalue>
<tag>Data.Scale_x.Scale_0.Deviation</tag>
<value>0</value>
</tagvalue>
<tagvalue>
<tag>Data.Scale_x.Scale_0.DeviationHigh</tag>
<value>False</value>
</tagvalue>
<tagvalue>
<tag>Data.Scale_x.Scale_0.DeviationLimitHigh</tag>
<value>0</value>
</tagvalue>
</tagvalues>
and currently I'm getting the data with this code:
#data.value('/tagvalues[1]/tagvalue[3]/value[1]', 'nvarchar(100)')
The problem I'm having is that sometimes the order is changed, which means that I have to reconfigure it again.
Is it not possible to convert the XML text into an temp table like so:
Tag Value
--------------------------------------------------
Data.Barcode TRUE
Data.DateStampFirstScale 20180111
Data.DateStampLastScale 20180111
Data.Dot FALSE
Data.Hangtab FALSE
Data.Scale_x.Scale_0.Deviation 0
Data.Scale_x.Scale_0.DeviationHigh FALSE
Data.Scale_x.Scale_0.DeviationLimitHigh 0
Try the XQuery.. .nodes()
select n.value('tag[1]', 'varchar(max)') [Tag],
n.value('value[1]', 'varchar(max)') [Value]
from #xml.nodes('tagvalues/tagvalue') as p(n)
You mean like this?
DECLARE #XML xml;
SET #XML =
'<tagvalues>
<tagvalue>
<tag>Data.Barcode</tag>
<value>True</value>
</tagvalue>
<tagvalue>
<tag>Data.DateStampFirstScale</tag>
<value>20180111</value>
</tagvalue>
<tagvalue>
<tag>Data.DateStampLastScale</tag>
<value>20180111</value>
</tagvalue>
<tagvalue>
<tag>Data.Dot</tag>
<value>False</value>
</tagvalue>
<tagvalue>
<tag>Data.Hangtab</tag>
<value>False</value>
</tagvalue>
<tagvalue>
<tag>Data.Scale_x.Scale_0.Deviation</tag>
<value>0</value>
</tagvalue>
<tagvalue>
<tag>Data.Scale_x.Scale_0.DeviationHigh</tag>
<value>False</value>
</tagvalue>
<tagvalue>
<tag>Data.Scale_x.Scale_0.DeviationLimitHigh</tag>
<value>0</value>
</tagvalue>
</tagvalues>'
SELECT X.N.value('(tag/text())[1]','varchar(50)') AS Tag,
X.N.value('(value/text())[1]','varchar(50)') AS [Value]
FROM #XML.nodes('/tagvalues/tagvalue') X(N);
Related
Basically I have an XML file which resembles this:
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<Cube>
<Cube time="2018-06-15">
<Cube currency="USD" rate="1.2345"/>
<Cube currency="ZAR" rate="10.1"/>
</Cube>
<Cube time="2018-06-16">
<Cube currency="USD" rate="1.1596"/>
<Cube currency="ZAR" rate="9.546"/>
</Cube>
</Cube>
</gesmes:Envelope>
And I insert it like this:
INSERT INTO
fxRatesXml(xmlData, updatedOn)
SELECT
CONVERT(XML, bulkColumn),
GETDATE()
FROM
OPENROWSET(BULK 'I:\Downloads\eurofxref-hist-90d.xml', SINGLE_BLOB) as fxRateXmlData;
I then try to read the time, currency and rate like so:
DECLARE #xml AS XML, #hDoc AS INT, #sql NVARCHAR (MAX)
SELECT
#xml = xmlData
FROM
fxRatesXml
EXEC sp_xml_preparedocument #hDoc OUTPUT, #xml, '<root xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref"/>'
SELECT
*
FROM
OPENXML(#hDoc, 'gesmes:Cube')
WITH
(
[time] nvarchar(max) 'Cube/time',
currency nvarchar(max) 'Cube/Cube/currency',
rate nvarchar(max) 'Cube/Cube/rate'
)
EXEC sp_xml_removedocument #hDoc
but no luck.
I am not sure where I'm going wrong any help would be appreciated.
UPDATED
Updated XML to be more specific. It can include more then just 2 cubes of time and more then 2 currencies per cube of time.
How would I iterate/select them all?
Once you have populated the #xml variable, instead of using EXEC sp_xml_preparedocument, try to define the namespaces you need using ;WITH XMLNAMESPACES:
I added the missing closing tag </gesmes:Envelope> at the end of the xml, so I tested my query with the following xml:
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<Cube>
<Cube time="2018-06-15">
<Cube currency="USD" rate="1.1596"/>
</Cube>
</Cube>
</gesmes:Envelope>
This is the query to retrieve tha data from xml variable #xml:
declare #xml xml = '<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref"> <Cube> <Cube time="2018-06-15"> <Cube currency="USD" rate="1.1596"/> </Cube> </Cube> </gesmes:Envelope>'
;WITH XMLNAMESPACES('http://www.gesmes.org/xml/2002-08-01' AS gesmes,
'http://www.ecb.int/vocabulary/2002-08-01/eurofxref' as ns)
SELECT
T.X.value('ns:Cube[1]/#currency','varchar(500)') AS [currency],
T.X.value('ns:Cube[1]/#rate','varchar(500)') AS [rate],
T.X.value('./#time','varchar(500)') AS [time]
FROM
#xml.nodes('/gesmes:Envelope/ns:Cube/ns:Cube') AS T(X)
Results:
To handle multiple <Cube> tags:
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<Cube>
<Cube time="2018-06-15">
<Cube currency="USD" rate="1.2345"/>
<Cube currency="ZAR" rate="10.1"/>
</Cube>
<Cube time="2018-06-16">
<Cube currency="USD" rate="1.1596"/>
<Cube currency="ZAR" rate="9.546"/>
</Cube>
</Cube>
</gesmes:Envelope>
you can use this query:
declare #xml xml ='<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref"> <Cube> <Cube time="2018-06-15"> <Cube currency="USD" rate="1.2345"/> <Cube currency="ZAR" rate="10.1"/> </Cube> <Cube time="2018-06-16"> <Cube currency="USD" rate="1.1596"/> <Cube currency="ZAR" rate="9.546"/> </Cube> </Cube> </gesmes:Envelope>'
;WITH XMLNAMESPACES('http://www.gesmes.org/xml/2002-08-01' AS gesmes,
'http://www.ecb.int/vocabulary/2002-08-01/eurofxref' as ns)
SELECT T.X.value('(.)[1]/#currency','varchar(500)') AS [currency]
,T.X.value('(.)[1]/#rate','varchar(500)') AS [rate]
,T.X.value('(..)[1]/#time','varchar(500)') AS [time]
FROM #xml.nodes('/gesmes:Envelope/ns:Cube/ns:Cube/ns:Cube') AS T(X)
Results:
I have the following XML:
<Report>
<Accounts>
<Account>
<Currency>USD</Currency>
<AccountBalance>45555</AccountBalance>
<Payments>
<PaymentData>
<PaymentCode>502</PaymentCode>
<PaymentAmount currCode="GBP">7000.00000000</PaymentAmount>
</PaymentData>
<PaymentData>
<PaymentCode>501</PaymentCode>
<PaymentAmount currCode="USD">5000.00000000</PaymentAmount>
</PaymentData>
</Payments>
</Account>
<Account>
<Currency>USD</Currency>
<AccountBalance>50000</AccountBalance>
<Payments>
<PaymentData>
<PaymentCode>501</PaymentCode>
<PaymentAmount currCode="USD">5000.00000000</PaymentAmount>
</PaymentData>
</Payments>
</Account>
</Accounts>
</Report>
My SQL Code is parsing this with the following code:
SELECT
[currCode] AS [Currency],
[AccountBalance] AS [AccountBalance],
[PaymentCode] AS [PaymentCode],
[PaymentCurrCode] AS [PaymentCurrCode],
[PaymentAmount] AS [PaymentAmount]
FROM OPENXML(#hDoc, 'Report/Accounts/Account',2)
WITH
(
[currCode] [nchar](3) 'currCode',
[AccountBalance] [decimal](18, 0) 'AccountBalance',
[PaymentCode] [nchar](10) 'Payments/PaymentData/PaymentCode',
[PaymentCurrCode] [nchar](3) 'Payments/PaymentData/PaymentAmount/#currCode',
[PaymentAmount] [decimal](18, 0) 'Payments/PaymentData/PaymentAmount'
)
I am getting the following result:
currCode | AccountBalance | PaymentCode | PaymentCurrCode | PaymentAmount
————————————————————————————————————————————————————————————————————————————————
USD | 45555 | 502 | GBP |7000.00000000
USD | 50000 | 501 | USD |5000.00000000
I am trying to get the multiple paymentdata and multiple account with the same openXml query. How Can is get all the data with the following result:
currCode | AccountBalance | PaymentCode | PaymentCurrCode | PaymentAmount
————————————————————————————————————————————————————————————————————————————————
USD | 45555 | 502 | GBP |7000.00000000
USD | 45555 | 501 | USD |5000.00000000
USD | 50000 | 501 | USD |5000.00000000
This is an up-to-date and state-of-the-art approach with XQuery/XPath methods. The result is the same, just faster and better to read:
DECLARE #XML XML=
'<Report>
<Accounts>
<Account>
<Currency>USD</Currency>
<AccountBalance>45555</AccountBalance>
<Payments>
<PaymentData>
<PaymentCode>502</PaymentCode>
<PaymentAmount currCode="GBP">7000.00000000</PaymentAmount>
</PaymentData>
<PaymentData>
<PaymentCode>501</PaymentCode>
<PaymentAmount currCode="USD">5000.00000000</PaymentAmount>
</PaymentData>
</Payments>
</Account>
<Account>
<Currency>USD</Currency>
<AccountBalance>50000</AccountBalance>
<Payments>
<PaymentData>
<PaymentCode>501</PaymentCode>
<PaymentAmount currCode="USD">5000.00000000</PaymentAmount>
</PaymentData>
</Payments>
</Account>
</Accounts>
</Report>';
SELECT Payment.value('(../../Currency)[1]','nchar(3)') AS currCode
,Payment.value('(../../AccountBalance)[1]','decimal(18,0)') AS AccountBalance
,Payment.value('PaymentCode[1]','nchar(10)') AS PaymentCode
,Payment.value('PaymentAmount[1]/#currCode','nchar(3)') AS PaymentCurrCode
,Payment.value('PaymentAmount[1]','decimal(18,0)') AS PaymentCurrCode
FROM #XML.nodes('Report/Accounts/Account/Payments/PaymentData') AS One(Payment)
This should work for you:
DECLARE #XML XML=
'<Report>
<Accounts>
<Account>
<Currency>USD</Currency>
<AccountBalance>45555</AccountBalance>
<Payments>
<PaymentData>
<PaymentCode>502</PaymentCode>
<PaymentAmount currCode="GBP">7000.00000000</PaymentAmount>
</PaymentData>
<PaymentData>
<PaymentCode>501</PaymentCode>
<PaymentAmount currCode="USD">5000.00000000</PaymentAmount>
</PaymentData>
</Payments>
</Account>
<Account>
<Currency>USD</Currency>
<AccountBalance>50000</AccountBalance>
<Payments>
<PaymentData>
<PaymentCode>501</PaymentCode>
<PaymentAmount currCode="USD">5000.00000000</PaymentAmount>
</PaymentData>
</Payments>
</Account>
</Accounts>
</Report>';
DECLARE #hDoc INT;
EXEC sp_xml_preparedocument #hDoc OUTPUT, #XML;
SELECT
[currCode] AS [Currency],
[AccountBalance] AS [AccountBalance],
[PaymentCode] AS [PaymentCode],
[PaymentCurrCode] AS [PaymentCurrCode],
[PaymentAmount] AS [PaymentAmount]
FROM OPENXML(#hDoc, 'Report/Accounts/Account/Payments/PaymentData',2)
WITH
(
[currCode] [nchar](3) '../../Currency',
[AccountBalance] [decimal](18, 0) '../../AccountBalance',
[PaymentCode] [nchar](10) 'PaymentCode',
[PaymentCurrCode] [nchar](3) 'PaymentAmount/#currCode',
[PaymentAmount] [decimal](18, 0) 'PaymentAmount'
)
EXEC sp_xml_removedocument #hDoc;
I have a TEXT datatype field called "XMLText" in SQL Server 2012. What I'd like to do is remove any SageID fields. So this long string field contains something that looks like this:
<pair n="priorinstitution2" v="Yale School of Medicine" />
<pair n="priorinstitution3" v="" />
<pair n="sageid" v="20668528" />
<pair n="priorinstitution1" v="University of Chicago" />
What I'd like to do is remove everything for the SageID tag so that the final result is this:
<pair n="priorinstitution2" v="Yale School of Medicine" />
<pair n="priorinstitution3" v="" />
<pair n="priorinstitution1" v="University of Chicago" />
Obviously, it's not in a fixed position in the field and the v= could be any numbers or length. What's the SQL string manipulation to do this?
TEXT is deprecated. Store your XML chunks as XML or NVARCHAR(MAX).
You can use xml.modify and delete to remove multiple occurences at once:
CREATE TABLE #tab(id INT, col TEXT);
INSERT INTO #tab(id, col)
VALUES
(1, '<pair n="priorinstitution2" v="Yale School of Medicine" />
<pair n="priorinstitution3" v="" />
<pair n="sageid" v="20668528" />
<pair n="priorinstitution1" v="University of Chicago" />')
,(2, '<pair n="sageid" v="2" y="adsadasdasd"/>
<pair n="priorinstitution2" v="Yale School of Medicine" />
<pair n="priorinstitution3" v="" />
<pair n="sageid" v="20668528" />
<pair n="priorinstitution1" v="University of Chicago" />
<pair n="sageid" v="2066852832421432" z="aaaa" />');
SELECT *, xml_col = CAST(col AS XML)
INTO #temp
FROM #tab;
UPDATE #temp
SET xml_col.modify('delete /pair[#n="sageid"]');
UPDATE t1
SET col = CAST(t2.xml_col AS NVARCHAR(MAX))
FROM #tab t1
JOIN #temp t2
ON t1.id = t2.id;
SELECT *
FROM #tab;
LiveDemo
Keep in mind that your XML data is not well-formed (no root element).
EDIT:
If your XML Text has different structure and you want to find all pair element with attribute n="sageid" use:
UPDATE #temp
SET xml_col.modify('delete //pair[#n="sageid"]');
LiveDemo2
Not a great solution but to find positions of start and end of tag and replace it with a blank string
UPDATE YourTable
SET yourColumn = REPLACE(yourColumn, SUBSTRING(yourColumn, CHARINDEX('<pair n="sageid"', yourColumn), CHARINDEX('/>', yourColumn, CHARINDEX('<pair n="sageid"', yourColumn)) - CHARINDEX('<pair n="sageid"', yourColumn) + 2), '')
Adding below script for debugging
DECLARE #str AS VARCHAR(255) = '<pair n="priorinstitution2" v="Yale School of Medicine" /><pair n="priorinstitution3" v="" /><pair n="sageid" v="20668528" /><pair n="priorinstitution1" v="University of Chicago" />'
SELECT REPLACE(#str, SUBSTRING(#str, CHARINDEX('<pair n="sageid"', #str), CHARINDEX('/>', #str, CHARINDEX('<pair n="sageid"', #str)) - CHARINDEX('<pair n="sageid"', #str) + 2), '')
I Have the below XML and want to extract the values for the following Nodes
1. result
2. documentNumber
3. costElementCode
<commitmentsResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<result xmlns="http://response.cim.its.test.edu.au/">SUCCESS</result>
<value xmlns="http://finance.response.cim.its.test.edu.au/">
<documentNumber xmlns="http://finance.cim.its.test.edu.au/">12345</documentNumber>
<lineNumber>2</lineNumber>
<costElementCode>costElementCode</costElementCode>
<internalOrderNumber>1000002</internalOrderNumber>
<costCentreCode>9999</costCentreCode>
<wbsCode>3000</wbsCode>
<lineDescription>2 packets of pencils</lineDescription>
<accountNumber>100000</accountNumber>
<itemAmount>105.5</itemAmount>
<fundsDueDate>2015-06-15</fundsDueDate>
</commitmentLine>
<commitmentLine xmlns="http://finance.cim.its.test.edu.au/">
<lineNumber>2</lineNumber>
<costElementCode>costElementCode</costElementCode>
<internalOrderNumber>1000002</internalOrderNumber>
<costCentreCode>9999</costCentreCode>
<wbsCode>3000</wbsCode>
<lineDescription>2 packets of pencils</lineDescription>
<accountNumber>100000</accountNumber>
<itemAmount>105.5</itemAmount>
<fundsDueDate>2015-06-15</fundsDueDate>
</commitmentLine>
</value>
</commitmentsResponse>
Without using Namespaces:
DECLARE #myXML xml =
N'<commitmentsResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<result>SUCCESS</result>
<value>
<documentNumber>12345</documentNumber>
<commitmentLine>
<lineNumber>2</lineNumber>
<costElementCode>costElementCode</costElementCode>
<internalOrderNumber>1000002</internalOrderNumber>
<costCentreCode>9999</costCentreCode>
<wbsCode>3000</wbsCode>
<lineDescription>2 packets of pencils</lineDescription>
<accountNumber>100000</accountNumber>
<itemAmount>105.5</itemAmount>
<fundsDueDate>2015-06-15</fundsDueDate>
</commitmentLine>
<commitmentLine xmlns="http://finance.cim.its.test.edu.au/">
<lineNumber>2</lineNumber>
<costElementCode>costElementCode</costElementCode>
<internalOrderNumber>1000002</internalOrderNumber>
<costCentreCode>9999</costCentreCode>
<wbsCode>3000</wbsCode>
<lineDescription>2 packets of pencils</lineDescription>
<accountNumber>100000</accountNumber>
<itemAmount>105.5</itemAmount>
<fundsDueDate>2015-06-15</fundsDueDate>
</commitmentLine>
</value>
</commitmentsResponse>'
DECLARE #DocumentNumber INT
SELECT #DocumentNumber = [Table].[Column].value('documentNumber[1]', 'INT')
FROM #myXML.nodes('/commitmentsResponse/value') AS [Table]([Column])
DECLARE #Result VARCHAR(256)
SELECT #Result = [Table].[Column].value('result[1]', 'varchar(256)')
FROM #myXML.nodes('/commitmentsResponse') AS [Table]([Column])
DECLARE #CostElementCode VARCHAR(256)
SELECT #CostElementCode = [Table].[Column].value('costElementCode[1]', 'varchar(256)')
FROM #myXML.nodes('/commitmentsResponse/value/commitmentLine') AS [Table]([Column])
SELECT #Result
SELECT #DocumentNumber
SELECT #CostElementCode
With using namespaces:
DECLARE #myXML xml =
N'<commitmentsResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<result xmlns="http://response.cim.its.test.edu.au/">SUCCESS</result>
<value>
<documentNumber xmlns="http://finance.cim.its.test.edu.au/">12345</documentNumber>
<commitmentLine>
<lineNumber>2</lineNumber>
<costElementCode>costElementCode</costElementCode>
<internalOrderNumber>1000002</internalOrderNumber>
<costCentreCode>9999</costCentreCode>
<wbsCode>3000</wbsCode>
<lineDescription>2 packets of pencils</lineDescription>
<accountNumber>100000</accountNumber>
<itemAmount>105.5</itemAmount>
<fundsDueDate>2015-06-15</fundsDueDate>
</commitmentLine>
<commitmentLine xmlns="http://finance.cim.its.test.edu.au/">
<lineNumber>2</lineNumber>
<costElementCode>costElementCode</costElementCode>
<internalOrderNumber>1000002</internalOrderNumber>
<costCentreCode>9999</costCentreCode>
<wbsCode>3000</wbsCode>
<lineDescription>2 packets of pencils</lineDescription>
<accountNumber>100000</accountNumber>
<itemAmount>105.5</itemAmount>
<fundsDueDate>2015-06-15</fundsDueDate>
</commitmentLine>
</value>
</commitmentsResponse>'
DECLARE #DocumentNumber INT
;WITH XMLNAMESPACES (N'http://finance.cim.its.test.edu.au/' as DYN)
SELECT #DocumentNumber = c.value('(DYN:documentNumber)[1]', 'INT')
FROM #myXML.nodes('/commitmentsResponse/value') t(c)
DECLARE #Result VARCHAR(256)
;WITH XMLNAMESPACES (N'http://response.cim.its.test.edu.au/' as DYN)
SELECT #Result = c.value('(DYN:result)[1]', 'VARCHAR(256)')
FROM #myXML.nodes('/commitmentsResponse') t(c)
DECLARE #CostElementCode VARCHAR(256)
SELECT #CostElementCode = c.value('(costElementCode)[1]', 'VARCHAR(256)')
FROM #myXML.nodes('/commitmentsResponse/value/commitmentLine') t(c)
SELECT #Result
SELECT #DocumentNumber
SELECT #CostElementCode
im having this kind of xml:
<?xml version="1.0"?>
-<recordedData>
<machine>ZSK40-2</machine>
<date>2013/09/21</date>
<hour>05:32</hour>-<CollectedData>-<variable>
<Name>PRODUCT</Name>
<Value>FILLER 580</Value>
</variable>-<variable>
<Name>LOT_NUMBER</Name>
<Value>CG 00063 0</Value>
</variable>-<variable>
<Name>SHIFT_SUPERVISOR</Name>
<Value> covaliu l</Value>
</variable>-<variable>
<Name>KGH_ALL_SET</Name>
<Value>0</Value>
</variable>-<variable>
<Name>KGH_ALL_REAL</Name>
<Value>0</Value>
</variable>-<variable>
<Name>KGH_F1_SET</Name>
<Value>0</Value>
</variable>-<variable>
<Name>KGH_F1_REAL</Name>
<Value>0</Value>
</variable>-<variable>
<Name>K_F1</Name>
<Value>43</Value>
</variable>-<variable>
<Name>SCREW_RPM_SET</Name>
<Value>550</Value>
</variable>-<variable>
<Name>SCREW_RPM_REAL</Name>
<Value>550.085388183594</Value>
</variable>-<variable>
<Name>TORQUE</Name>
<Value>1.21340000629425</Value>
</variable>-<variable>
<Name>CURRENT</Name>
<Value>60.1959991455078</Value>
</variable>-<variable>
<Name>KW_KG</Name>
<Value>0</Value>
</variable>-<variable>
<Name>KW</Name>
<Value>-0.990000009536743</Value>
</variable>-<variable>
<Name>MELT_PRESSURE</Name>
<Value>0</Value>
</variable>-<variable>
<Name>MELT_TEMPERATURE</Name>
<Value>214</Value>
</variable>-<variable>
<Name>PV1</Name>
<Value>216</Value>
</variable>-<variable>
<Name>SP1</Name>
<Value>210</Value>
</variable>-<variable>
<Name>PV2</Name>
<Value>239</Value>
</variable>-<variable>
<Name>SP2</Name>
<Value>220</Value>
</variable>-<variable>
<Name>PV3</Name>
<Value>220</Value>
</variable>-<variable>
<Name>SP3</Name>
<Value>220</Value>
</variable>-<variable>
<Name>PV4</Name>
<Value>220</Value>
</variable>-<variable>
<Name>SP4</Name>
<Value>220</Value>
</variable>-<variable>
<Name>PV5</Name>
<Value>209</Value>
</variable>-<variable>
<Name>SP5</Name>
<Value>210</Value>
</variable>-<variable>
<Name>PV6</Name>
<Value>210</Value>
</variable>-<variable>
<Name>SP6</Name>
<Value>210</Value>
</variable>-<variable>
<Name>PV7</Name>
<Value>210</Value>
</variable>-<variable>
<Name>SP7</Name>
<Value>210</Value>
</variable>-<variable>
<Name>PV8</Name>
<Value>210</Value>
</variable>-<variable>
<Name>SP8</Name>
<Value>210</Value>
</variable>-<variable>
<Name>PV9</Name>
<Value>210</Value>
</variable>-<variable>
<Name>SP9</Name>
<Value>210</Value>
</variable>-<variable>
<Name>PV10</Name>
<Value>210</Value>
</variable>-<variable>
<Name>SP10</Name>
<Value>210</Value>
</variable>-<variable>
<Name>PV11</Name>
<Value>220</Value>
</variable>-<variable>
<Name>SP11</Name>
<Value>220</Value>
</variable>
</CollectedData>
</recordedData>
Can anyone provide a sample sql script for extracting all the data from it please.
i would really apreciate this since im new to xml.
Thanks in advance.
If you have your data in a table already, you can use something like this:
DECLARE #Tmp TABLE (ID INT NOT NULL, XmlContent XML)
INSERT INTO #TMP VALUES(1, '......(your entire XML here).......)
SELECT
ID,
MACHINE = XmlContent.value('(/recordedData/machine)[1]', 'varchar(50)'),
RecordingDate = XmlContent.value('(/recordedData/date)[1]', 'varchar(50)'),
RecordingTime = XmlContent.value('(/recordedData/hour)[1]', 'varchar(50)'),
VariableName = XVar.value('(Name)[1]', 'varchar(50)'),
VariableValue = XVar.value('(Value)[1]', 'varchar(50)')
FROM
#Tmp
CROSS APPLY
XmlContent.nodes('/recordedData/CollectedData/variable') AS XTbl(XVar)
This gives you an output something like:
.... and so on - listing all the variables with their name and value.