jgrammer42
asked on
Help on SQL statement to read & extract XML column data
I have a pretty large XML column in a MS SQL database and I want to parse the XML node data out to a report from MS-SQL.
Here is the script I am using, which executes without errors, but returns no data. The XML node data I want are just those in the SELECT statement but I have about 7,000 data that I need to run a report against.
Can someone please provide direction on what I am missing in the script?
Thank you so very much in advance;
Jeff
SELECT
upro.primaryKey,
xmldata.XmlCol.value('(Emp loyeePerso n/CompanyC ode)[1]',' VARCHAR(10 )') AS CompanyCode,
xmldata.XmlCol.value('(Emp loyeePerso n/Employee Number)[1] ','INT') AS EmployeeNumber,
xmldata.XmlCol.value('(Emp loyeePerso n/FirstNam e)[1]','VA RCHAR(50)' ) AS FirstName,
xmldata.XmlCol.value('(Emp loyeePerso n/LastName )[1]','VAR CHAR(50)') AS LastName,
xmldata.XmlCol.value('(Emp loyeePerso n/People/P erson/Emai lAddress)[ 1]','VARCH AR(100)') AS EmailAddress
FROM dbo.UproEmpRawXmlData upro
CROSS APPLY upro.XMLData.nodes('/Envel ope/Body/F indPeopleR esponse/Fi ndPeopleRe sult/Resul ts') xmldata(XmlCol);
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.somewebapi.com/services/employeeperson/IEmployeePerson/FindPeopleResponse</a:A ction>
</s:Header>
<s:Body>
<FindPeopleResponse xmlns="http://www.somewebapi.com/services/employeeperson">
<FindPeopleResult xmlns:b="http://www.somewebapi.com/contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<b:OperationResult>
<b:HasErrors>false</b:HasE rrors>
<b:HasWarnings>false</b:Ha sWarnings>
<b:Messages>
<b:OperationMessage>
<b:Code>OK</b:Code>
<b:LogEntryId i:nil="true"/>
<b:Message>OK</b:Message>
<b:PropertyName i:nil="true"/>
<b:Severity>Information</b :Severity>
</b:OperationMessage>
</b:Messages>
<b:PagingInfo>
<b:CurrentPage>1</b:Curren tPage>
<b:PageSize>1000</b:PageSi ze>
<b:PageTotal>8</b:PageTota l>
<b:TotalItems>7432</b:Tota lItems>
</b:PagingInfo>
<b:RequestNumber i:nil="true"/>
<b:Success>true</b:Success >
</b:OperationResult>
<b:Results>
<b:EmployeePerson>
<b:CompanyCode>WORKS</b:Co mpanyCode>
<b:EmployeeNumber>210547</ b:Employee Number>
<b:FirstName>Walter</b:Fir stName>
<b:LastName>Reed</b:LastNa me>
<b:People>
<b:Person>
<b:AlternateEmailAddress i:nil="true"/>
<b:EmailAddress>walter.ree d@gmailhos pital.com< /b:EmailAd dress>
<b:EmployeeIdentifier i:type="b:EmployeeNumberId entifier">
<b:CompanyCode>WORKS</b:Co mpanyCode>
<b:EmployeeNumber>200547</ b:Employee Number>
</b:EmployeeIdentifier>
<b:FirstName>Walter</b:Fir stName>
<b:FormerLastName i:nil="true"/>
<b:LastName>Reed</b:LastNa me>
<b:MiddleName/>
<b:PreferredFirstName i:nil="true"/>
<b:Prefix i:nil="true"/>
<b:SelfServiceProperties i:nil="true" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<b:Suffix i:nil="true"/>
<b:SuppressSSN>true</b:Sup pressSSN>
</b:Person>
</b:People>
</b:EmployeePerson>
<b:EmployeePerson>
<b:CompanyCode>WORKS</b:Co mpanyCode>
<b:EmployeeNumber>210586</ b:Employee Number>
<b:FirstName>John</b:First Name>
<b:LastName>Hopkins</b:Las tName>
<b:People>
<b:Person>
<b:AlternateEmailAddress i:nil="true"/>
<b:EmailAddress i:nil="true"/>
<b:EmployeeIdentifier i:type="b:EmployeeNumberId entifier">
<b:CompanyCode>WORKS</b:Co mpanyCode>
<b:EmployeeNumber>210586</ b:Employee Number>
</b:EmployeeIdentifier>
<b:FirstName>John</b:First Name>
<b:FormerLastName i:nil="true"/>
<b:LastName>Hopkins</b:Las tName>
<b:MiddleName/>
<b:PreferredFirstName i:nil="true"/>
<b:Prefix i:nil="true"/>
<b:SelfServiceProperties i:nil="true" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<b:Suffix i:nil="true"/>
<b:SuppressSSN>true</b:Sup pressSSN>
</b:Person>
</b:People>
</b:EmployeePerson>
Here is the script I am using, which executes without errors, but returns no data. The XML node data I want are just those in the SELECT statement but I have about 7,000 data that I need to run a report against.
Can someone please provide direction on what I am missing in the script?
Thank you so very much in advance;
Jeff
SELECT
upro.primaryKey,
xmldata.XmlCol.value('(Emp
xmldata.XmlCol.value('(Emp
xmldata.XmlCol.value('(Emp
xmldata.XmlCol.value('(Emp
xmldata.XmlCol.value('(Emp
FROM dbo.UproEmpRawXmlData upro
CROSS APPLY upro.XMLData.nodes('/Envel
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.somewebapi.com/services/employeeperson/IEmployeePerson/FindPeopleResponse</a:A
</s:Header>
<s:Body>
<FindPeopleResponse xmlns="http://www.somewebapi.com/services/employeeperson">
<FindPeopleResult xmlns:b="http://www.somewebapi.com/contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<b:OperationResult>
<b:HasErrors>false</b:HasE
<b:HasWarnings>false</b:Ha
<b:Messages>
<b:OperationMessage>
<b:Code>OK</b:Code>
<b:LogEntryId i:nil="true"/>
<b:Message>OK</b:Message>
<b:PropertyName i:nil="true"/>
<b:Severity>Information</b
</b:OperationMessage>
</b:Messages>
<b:PagingInfo>
<b:CurrentPage>1</b:Curren
<b:PageSize>1000</b:PageSi
<b:PageTotal>8</b:PageTota
<b:TotalItems>7432</b:Tota
</b:PagingInfo>
<b:RequestNumber i:nil="true"/>
<b:Success>true</b:Success
</b:OperationResult>
<b:Results>
<b:EmployeePerson>
<b:CompanyCode>WORKS</b:Co
<b:EmployeeNumber>210547</
<b:FirstName>Walter</b:Fir
<b:LastName>Reed</b:LastNa
<b:People>
<b:Person>
<b:AlternateEmailAddress i:nil="true"/>
<b:EmailAddress>walter.ree
<b:EmployeeIdentifier i:type="b:EmployeeNumberId
<b:CompanyCode>WORKS</b:Co
<b:EmployeeNumber>200547</
</b:EmployeeIdentifier>
<b:FirstName>Walter</b:Fir
<b:FormerLastName i:nil="true"/>
<b:LastName>Reed</b:LastNa
<b:MiddleName/>
<b:PreferredFirstName i:nil="true"/>
<b:Prefix i:nil="true"/>
<b:SelfServiceProperties i:nil="true" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<b:Suffix i:nil="true"/>
<b:SuppressSSN>true</b:Sup
</b:Person>
</b:People>
</b:EmployeePerson>
<b:EmployeePerson>
<b:CompanyCode>WORKS</b:Co
<b:EmployeeNumber>210586</
<b:FirstName>John</b:First
<b:LastName>Hopkins</b:Las
<b:People>
<b:Person>
<b:AlternateEmailAddress i:nil="true"/>
<b:EmailAddress i:nil="true"/>
<b:EmployeeIdentifier i:type="b:EmployeeNumberId
<b:CompanyCode>WORKS</b:Co
<b:EmployeeNumber>210586</
</b:EmployeeIdentifier>
<b:FirstName>John</b:First
<b:FormerLastName i:nil="true"/>
<b:LastName>Hopkins</b:Las
<b:MiddleName/>
<b:PreferredFirstName i:nil="true"/>
<b:Prefix i:nil="true"/>
<b:SelfServiceProperties i:nil="true" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<b:Suffix i:nil="true"/>
<b:SuppressSSN>true</b:Sup
</b:Person>
</b:People>
</b:EmployeePerson>
ASKER
Thank you Ste5an,
That helped me quite a bit, using the OUTER APPLY and the NVARCHAR. Using the following script, I do return a row, but it only returns the primaryKey value and everything else is 'NULL'.
I am very novice when it comes to parsing of XML data. I just wanted to thoroughly understand how to pull information out before doing so from a client. Do you think I would understand better doing from the client-side instead?
Here is what Returned from the script:
1 NULL NULL NULL NULL NULL
Here is the SQL script:
SELECT
upro.primaryKey,
xmldata.XmlCol.value('(Emp loyeePerso n/CompanyC ode)[1]',' NVARCHAR(1 0)') AS CompanyCode,
xmldata.XmlCol.value('(Emp loyeePerso n/Employee Number)[1] ','INT') AS EmployeeNumber,
xmldata.XmlCol.value('(Emp loyeePerso n/FirstNam e)[1]','NV ARCHAR(50) ') AS FirstName,
xmldata.XmlCol.value('(Emp loyeePerso n/LastName )[1]','NVA RCHAR(50)' ) AS LastName,
xmldata.XmlCol.value('(Emp loyeePerso n/People/P erson/Emai lAddress)[ 1]','VARCH AR(100)') AS EmailAddress
FROM dbo.UproEmpRawXmlData upro
OUTER APPLY upro.XMLData.nodes('/Envel ope/Body/F indPeopleR esponse/Fi ndPeopleRe sult/Resul ts') xmldata(XmlCol);
Finally, my apologies for not posting the full XML. It was really just those two entries that I was testing with.
Here is the XML with the closing tags
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.somewebapi.com/services/employeeperson/IEmployeePerson/FindPeopleResponse</a:A ction>
</s:Header>
<s:Body>
<FindPeopleResponse xmlns="http://www.somewebapi.com/services/employeeperson">
<FindPeopleResult xmlns:b="http://www.somewebapi.com/contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<b:OperationResult>
<b:HasErrors>false</b:HasE rrors>
<b:HasWarnings>false</b:Ha sWarnings>
<b:Messages>
<b:OperationMessage>
<b:Code>OK</b:Code>
<b:LogEntryId i:nil="true"/>
<b:Message>OK</b:Message>
<b:PropertyName i:nil="true"/>
<b:Severity>Information</b :Severity>
</b:OperationMessage>
</b:Messages>
<b:PagingInfo>
<b:CurrentPage>1</b:Curren tPage>
<b:PageSize>1000</b:PageSi ze>
<b:PageTotal>8</b:PageTota l>
<b:TotalItems>7432</b:Tota lItems>
</b:PagingInfo>
<b:RequestNumber i:nil="true"/>
<b:Success>true</b:Success >
</b:OperationResult>
<b:Results>
<b:EmployeePerson>
<b:CompanyCode>WORKS</b:Co mpanyCode>
<b:EmployeeNumber>210547</ b:Employee Number>
<b:FirstName>Walter</b:Fir stName>
<b:LastName>Reed</b:LastNa me>
<b:People>
<b:Person>
<b:AlternateEmailAddress i:nil="true"/>
<b:EmailAddress>walter.ree d@gmailhos pital.com< /b:EmailAd dress>
<b:EmployeeIdentifier i:type="b:EmployeeNumberId entifier">
<b:CompanyCode>WORKS</b:Co mpanyCode>
<b:EmployeeNumber>200547</ b:Employee Number>
</b:EmployeeIdentifier>
<b:FirstName>Walter</b:Fir stName>
<b:FormerLastName i:nil="true"/>
<b:LastName>Reed</b:LastNa me>
<b:MiddleName/>
<b:PreferredFirstName i:nil="true"/>
<b:Prefix i:nil="true"/>
<b:SelfServiceProperties i:nil="true" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<b:Suffix i:nil="true"/>
<b:SuppressSSN>true</b:Sup pressSSN>
</b:Person>
</b:People>
</b:EmployeePerson>
<b:EmployeePerson>
<b:CompanyCode>WORKS</b:Co mpanyCode>
<b:EmployeeNumber>210586</ b:Employee Number>
<b:FirstName>John</b:First Name>
<b:LastName>Hopkins</b:Las tName>
<b:People>
<b:Person>
<b:AlternateEmailAddress i:nil="true"/>
<b:EmailAddress i:nil="true"/>
<b:EmployeeIdentifier i:type="b:EmployeeNumberId entifier">
<b:CompanyCode>WORKS</b:Co mpanyCode>
<b:EmployeeNumber>210586</ b:Employee Number>
</b:EmployeeIdentifier>
<b:FirstName>John</b:First Name>
<b:FormerLastName i:nil="true"/>
<b:LastName>Hopkins</b:Las tName>
<b:MiddleName/>
<b:PreferredFirstName i:nil="true"/>
<b:Prefix i:nil="true"/>
<b:SelfServiceProperties i:nil="true" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<b:Suffix i:nil="true"/>
<b:SuppressSSN>true</b:Sup pressSSN>
</b:Person>
</b:People>
</b:EmployeePerson>
</b:Results>
</FindPeopleResult>
</FindPeopleResponse>
</s:Body>
</s:Envelope>
That helped me quite a bit, using the OUTER APPLY and the NVARCHAR. Using the following script, I do return a row, but it only returns the primaryKey value and everything else is 'NULL'.
I am very novice when it comes to parsing of XML data. I just wanted to thoroughly understand how to pull information out before doing so from a client. Do you think I would understand better doing from the client-side instead?
Here is what Returned from the script:
1 NULL NULL NULL NULL NULL
Here is the SQL script:
SELECT
upro.primaryKey,
xmldata.XmlCol.value('(Emp
xmldata.XmlCol.value('(Emp
xmldata.XmlCol.value('(Emp
xmldata.XmlCol.value('(Emp
xmldata.XmlCol.value('(Emp
FROM dbo.UproEmpRawXmlData upro
OUTER APPLY upro.XMLData.nodes('/Envel
Finally, my apologies for not posting the full XML. It was really just those two entries that I was testing with.
Here is the XML with the closing tags
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.somewebapi.com/services/employeeperson/IEmployeePerson/FindPeopleResponse</a:A
</s:Header>
<s:Body>
<FindPeopleResponse xmlns="http://www.somewebapi.com/services/employeeperson">
<FindPeopleResult xmlns:b="http://www.somewebapi.com/contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<b:OperationResult>
<b:HasErrors>false</b:HasE
<b:HasWarnings>false</b:Ha
<b:Messages>
<b:OperationMessage>
<b:Code>OK</b:Code>
<b:LogEntryId i:nil="true"/>
<b:Message>OK</b:Message>
<b:PropertyName i:nil="true"/>
<b:Severity>Information</b
</b:OperationMessage>
</b:Messages>
<b:PagingInfo>
<b:CurrentPage>1</b:Curren
<b:PageSize>1000</b:PageSi
<b:PageTotal>8</b:PageTota
<b:TotalItems>7432</b:Tota
</b:PagingInfo>
<b:RequestNumber i:nil="true"/>
<b:Success>true</b:Success
</b:OperationResult>
<b:Results>
<b:EmployeePerson>
<b:CompanyCode>WORKS</b:Co
<b:EmployeeNumber>210547</
<b:FirstName>Walter</b:Fir
<b:LastName>Reed</b:LastNa
<b:People>
<b:Person>
<b:AlternateEmailAddress i:nil="true"/>
<b:EmailAddress>walter.ree
<b:EmployeeIdentifier i:type="b:EmployeeNumberId
<b:CompanyCode>WORKS</b:Co
<b:EmployeeNumber>200547</
</b:EmployeeIdentifier>
<b:FirstName>Walter</b:Fir
<b:FormerLastName i:nil="true"/>
<b:LastName>Reed</b:LastNa
<b:MiddleName/>
<b:PreferredFirstName i:nil="true"/>
<b:Prefix i:nil="true"/>
<b:SelfServiceProperties i:nil="true" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<b:Suffix i:nil="true"/>
<b:SuppressSSN>true</b:Sup
</b:Person>
</b:People>
</b:EmployeePerson>
<b:EmployeePerson>
<b:CompanyCode>WORKS</b:Co
<b:EmployeeNumber>210586</
<b:FirstName>John</b:First
<b:LastName>Hopkins</b:Las
<b:People>
<b:Person>
<b:AlternateEmailAddress i:nil="true"/>
<b:EmailAddress i:nil="true"/>
<b:EmployeeIdentifier i:type="b:EmployeeNumberId
<b:CompanyCode>WORKS</b:Co
<b:EmployeeNumber>210586</
</b:EmployeeIdentifier>
<b:FirstName>John</b:First
<b:FormerLastName i:nil="true"/>
<b:LastName>Hopkins</b:Las
<b:MiddleName/>
<b:PreferredFirstName i:nil="true"/>
<b:Prefix i:nil="true"/>
<b:SelfServiceProperties i:nil="true" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<b:Suffix i:nil="true"/>
<b:SuppressSSN>true</b:Sup
</b:Person>
</b:People>
</b:EmployeePerson>
</b:Results>
</FindPeopleResult>
</FindPeopleResponse>
</s:Body>
</s:Envelope>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Ste5an
Thank you so very much for your help. I truly appreciate your time and patience. This was very helpful.
Thank you so very much for your help. I truly appreciate your time and patience. This was very helpful.
Then: Posting complete XML is necessary, otherwise our samples must be incomplete.
And finally to your question: you're missing the namespaces. And using CROSS APPLY requires the elements to be mandatory. When elements are optional, use OUTER APPLY instead.
E.g.
Open in new window
p.s. as most XML is Unicode, you should use NVARCHAR instead of VARCHAR.