I am trying to set up an SQL job to import an XML file into an SQL Server table. Using OPENXML, I can't seem to select the specific data I need from the file. Here's my code and XML data. I am trying to select Facility and Entity_Code but when I run the code, these fields appear as blank.
I would like to transfer these fields into their own table.
Thanks in advance.
Declare #x xml
select #x=p
from OPENROWSET(Bulk'\\vmirsdh01\fast_data\Small.xml', SINGLE_BLOB) as T(P)
Select #x
Declare #hdoc int
EXEC sp_xml_preparedocument #hdoc OUTPUT, #x
Select *
FROM OPENXML (#hdoc,'/Report/Tablix1/Details_Collection/Details',0)
with(Facility nvarchar(255) '#Facility',
Entity_Code nvarchar(255) '#Entity_Code')
exec sp_xml_removedocument #hdoc
'************ XML
<?xml version="1.0" encoding="utf-8"?><Report xsi:schemaLocation="T-Report https://csre.xxx.com%2FDevelopment%20Folder%2FIand%2FT-Report&rs%3ACommand=Render&rs%3AFormat=XML&rs%3ASessionID=4keav12uayp33ve3uczpgmfr&rc %3ASchema=True" Name="T-Report" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="T_Report">
<Tablix1>
<Details_Collection><Details Facility="Fxx" Tool_Type="Base Build" Entity_Code="EquiP1" /></Details_Collection>
</Tablix1>
</Report>
Here is an executable version
Declare #x xml
select #x='<?xml version="1.0" encoding="utf-8"?><Report xsi:schemaLocation="T-Report https://csre.xxx.com%2FDevelopment%20Folder%2FIand%2FT-Report&rs%3ACommand=Render&rs%3AFormat=XML&rs%3ASessionID=4keav12uayp33ve3uczpgmfr&rc %3ASchema=True" Name="T-Report" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="T_Report">
<Tablix1>
<Details_Collection><Details Facility="Fxx" Tool_Type="Base Build" Entity_Code="EquiP1" /></Details_Collection>
</Tablix1>
</Report>'
Declare #hdoc int
EXEC sp_xml_preparedocument #hdoc OUTPUT, #x
Select *
FROM OPENXML (#hdoc,'/Report/Tablix1/Details_Collection/Details',0)
with(Facility nvarchar(255) '#Facility',
Entity_Code nvarchar(255) '#Entity_Code')
exec sp_xml_removedocument #hdoc
You have a default namespace that you need to take into consideration xmlns="T_Report".
Using the XML variable directly your query would look like
with xmlnamespaces(default 'T_Report')
select D.X.value('#Facility', 'nvarchar(255)'),
D.X.value('#Entity_Code', 'nvarchar(255)')
from #x.nodes('/Report/Tablix1/Details_Collection/Details') as D(X)
If you for some reason want to use openxml you need to declare the namespace in the third parameter to sp_xml_preparedocument.
EXEC sp_xml_preparedocument #hdoc OUTPUT, #x, '<root xmlns:xx="T_Report"/>'
Select *
FROM OPENXML (#hdoc,'/xx:Report/xx:Tablix1/xx:Details_Collection/xx:Details',0)
with(Facility nvarchar(255) '#Facility',
Entity_Code nvarchar(255) '#Entity_Code')
exec sp_xml_removedocument #hdoc
Your XML has an opening tag of <Report> but your query is for an opening tag called <Result>.
While I can't swear that everything will work after you fix that (I don't do much with OPENXML) I'm fairly confident that that is a problem.
Related
I would like to query the following XML-File using SQL:
<?xml version="1.0" encoding="UTF-8"?>
<GL_MarketDocument xmlns="urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0">
<mRID>2f6f8b82348440b1b121bca06311945d</mRID>
<time_Period.timeInterval>
<start>2020-03-02T23:00Z</start>
<end>2020-03-03T18:30Z</end>
</time_Period.timeInterval>
</GL_MarketDocument>
Using this code I would like to get the value for "mRID":
DECLARE #DocHandle int
DECLARE #XmlDocument varchar(MAX)
SELECT #XMLDocument=I
FROM OPENROWSET (BULK 'TP_10V1001C--00013H_ENTSOE-ETP__00a8f07d-95bd-4075-b1f7-3f54ce6162f3.xml', SINGLE_BLOB) as ImportFile(I)
EXEC sp_xml_preparedocument #DocHandle OUTPUT, #XmlDocument, N'<root xmlns:d="urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0"/>' ;
SELECT *
FROM OPENXML (#DocHandle, N'/d:GL_MarketDocument')
WITH ([mRID] varchar(50))
EXEC sp_xml_removedocument #DocHandle
However, the result is:
mRID
NULL
How to get the correct value for mRID ('2f6f8b82348440b1b121bca06311945d') instead of NULL?
Microsoft proprietary OPENXML and its companions sp_xml_preparedocument and sp_xml_removedocument are kept just for backward compatibility with the obsolete SQL Server 2000. Their use is diminished just to very few fringe cases.
Starting from SQL Server 2005 onwards, it is better to use XQuery language, based on the w3c standards, while dealing with the XML data type.
Your XML has a default namespace, so it should be taken into account.
SQL, from a variable
DECLARE #XMLDocument XML =
N'<GL_MarketDocument xmlns="urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0">
<mRID>2f6f8b82348440b1b121bca06311945d</mRID>
<time_Period.timeInterval>
<start>2020-03-02T23:00Z</start>
<end>2020-03-03T18:30Z</end>
</time_Period.timeInterval>
</GL_MarketDocument>';
WITH XMLNAMESPACES (DEFAULT 'urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0')
SELECT c.value('(mRID/text())[1]','NVARCHAR(100)') AS mRID
FROM #XMLDocument.nodes('/GL_MarketDocument') AS t(c);
SQL, directly from the XML file
WITH XMLNAMESPACES (DEFAULT 'urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0')
, rs (xmlData) AS
(
SELECT TRY_CAST(BulkColumn AS XML)
FROM OPENROWSET(BULK N'e:\Temp\TP_10V1001C--00013H_ENTSOE-ETP__00a8f07d-95bd-4075-b1f7-3f54ce6162f3.xml', SINGLE_BLOB) AS x
)
SELECT c.value('(mRID/text())[1]','NVARCHAR(100)') AS mRID
FROM rs
CROSS APPLY xmlData.nodes('/GL_MarketDocument') AS t(c);
Output
+----------------------------------+
| mRID |
+----------------------------------+
| 2f6f8b82348440b1b121bca06311945d |
+----------------------------------+
I've got problem with parsing informations from XML into SQL with double namespace.
Have a look at this code:
DECLARE #Handle AS INT; -- The handle of the XML data, passed to sp_xml_preparedocument
DECLARE #Xml AS NVARCHAR(1000); -- The XML document for this example
SET #Xml = N'
<SiBikNet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://www.ws.bik.pl/ws/ki/2v2/types">
<BIK_REQUEST>
<siBikNetResponse>
<consentDate>2018-07-29</consentDate>
<citizenshipStatus>citizen</citizenshipStatus>
<nationality>PL</nationality>
<pesel>123</pesel>
</siBikNetResponse>
</BIK_REQUEST>
</SiBikNet>';
EXEC sys.sp_xml_preparedocument #Handle OUTPUT , #Xml, N'<SiBikNet xmlns:t="https://www.ws.bik.pl/ws/ki/2v2/types"/>'; --Prepare a parsed document
SELECT *
FROM
OPENXML(#Handle,'/t:SiBikNet/t:BIK_REQUEST/t:siBikNetResponse', 2)
WITH ( nationality NVARCHAR(10) 't:nationality',
pesel NVARCHAR(10) 't:pesel '
);
EXEC sys.sp_xml_removedocument #Handle;
Which gives me proper output in forms of table with 2 columns.
But when I will add one row with double namespace: then I cannot parse this informations :
DECLARE #Handle AS INT; -- The handle of the XML data, passed to sp_xml_preparedocument
DECLARE #Xml AS NVARCHAR(1000); -- The XML document for this example
SET #Xml = N'
<SiBikNet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://www.ws.bik.pl/ws/ki/2v2/types">
<BIK_REQUEST xmlns="">
<siBikNetResponse>
<consentDate>2018-07-29</consentDate>
<citizenshipStatus>citizen</citizenshipStatus>
<nationality>PL</nationality>
<pesel>123</pesel>
</siBikNetResponse>
</BIK_REQUEST>
</SiBikNet>';
EXEC sys.sp_xml_preparedocument #Handle OUTPUT , #Xml, N'<SiBikNet xmlns:t="https://www.ws.bik.pl/ws/ki/2v2/types"/>'; --Prepare a parsed document
SELECT *
FROM
OPENXML(#Handle,'/t:SiBikNet/t:BIK_REQUEST/t:siBikNetResponse', 2)
WITH ( nationality NVARCHAR(10) 't:nationality',
pesel NVARCHAR(10) 't:pesel '
);
EXEC sys.sp_xml_removedocument #Handle;
Can anyone help ?
When I get stuck with anonymous namespaces or oddball namespace combinations, the simplest way is to just use the XPath function local-name(). Like this:
DECLARE #Handle AS INT; -- The handle of the XML data, passed to sp_xml_preparedocument
DECLARE #Xml AS NVARCHAR(1000); -- The XML document for this example
SET #Xml = N'
<SiBikNet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://www.ws.bik.pl/ws/ki/2v2/types">
<BIK_REQUEST xmlns="">
<siBikNetResponse>
<consentDate>2018-07-29</consentDate>
<citizenshipStatus>citizen</citizenshipStatus>
<nationality>PL</nationality>
<pesel>98070902702</pesel>
</siBikNetResponse>
</BIK_REQUEST>
</SiBikNet>';
EXEC sys.sp_xml_preparedocument #Handle OUTPUT , #Xml, N'<SiBikNet xmlns:t="https://www.ws.bik.pl/ws/ki/2v2/types"/>'; --Prepare a parsed document
SELECT *
FROM
OPENXML(#Handle,'/*[local-name()="SiBikNet"]/*[local-name()="BIK_REQUEST"]/*[local-name()="siBikNetResponse"]', 2)
WITH ( nationality NVARCHAR(10) ,--'t:nationality',
pesel NVARCHAR(10) --'t:pesel '
);
EXEC sys.sp_xml_removedocument #Handle;
I'm trying to import an XML file into a SQL table. I found a few examples of code to do this, but I can't seem to get it to work. I've tried a few variations in my code but at this point I'm not sure if the issue is the XML file structure or my SQL.
Below is the code I'm using as well as the XML file (truncated to one record).
CREATE TABLE workspace.dbo.tbt_SED_XMLwithOpenXML
(
Id INT IDENTITY PRIMARY KEY,
XMLData XML,
LoadedDateTime DATETIME
)
INSERT INTO workspace.dbo.tbt_SED_XMLwithOpenXML(XMLData, LoadedDateTime)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE()
FROM OPENROWSET(BULK 'File.xml', SINGLE_BLOB) AS x;
DECLARE #XML AS XML, #hDoc AS INT, #SQL NVARCHAR (MAX)
SELECT #XML = XMLData FROM workspace.dbo.tbt_SED_XMLwithOpenXML WHERE ID = '1' -- The row to process
EXEC sp_xml_preparedocument #hDoc OUTPUT, #XML
INSERT INTO workspace.dbo.tb_SED_Emails
SELECT email
FROM OPENXML(#hDoc, 'responseData/manifest/contact_data')
WITH
(
email [varchar](128) 'email'
)
EXEC sp_xml_removedocument #hDoc
GO
XML File Example:
<?xml version="1.0" encoding="utf-8"?>
<methodResponse>
<item>
<methodName>
<![CDATA[]]>
</methodName>
<responseData>
<manifest>
<contact_data>
<email>jason.kang#stanfordalumni.org</email>
</contact_data>
</manifest>
</responseData>
<responseNum>
<![CDATA[1]]>
</responseNum>
<responseCode>
<![CDATA[]]>
</responseCode>
</item>
</methodResponse>
Try to use the built-in, native XQuery support instead of the clunky old OPENXML stuff:
SELECT
Email = XC.value('(email)[1]', 'varchar(255)')
FROM
workspace.dbo.tbt_SED_XMLwithOpenXML
CROSS APPLY
XMLData.nodes('/methodResponse/item/responseData/manifest/contact_data') AS XT(XC)
That should output the desired e-mail address for you:
You are using the wrong xPath expression.
Change 'responseData/manifest/contact_data' to 'methodResponse/item/responseData/manifest/contact_data'.
I'm trying to parse an XML document with a query.
Here is a sample of my XML:
<export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://bbhgtm.gov.com/oos/export/1" xmlns:oos="http://bbhgtm.gov.com/oos/types/1">
<notificationOK>
<oos:id>8373125</oos:id>
<oos:notificationNumber>0173200001513000422</oos:notificationNumber>
Here is my query
declare #hdoc int
EXEC sp_xml_preparedocument #hdoc OUTPUT, #x,
'
<export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:q="http://bbhgtm.gov.com/oos/export/1"
xmlns:oos="http://bbhgtm.gov.com/oos/types/1"/>
'
select *
from openxml(#hdoc, '/notificationOK/oos:id/oos:notificationNumber/', 1)
WITH(
versionNumber int 'oos:versionNumber'
,createDate datetime 'oos:createDate'
)
EXEC sp_xml_removedocument #hdoc
But I'm getting NULL in my SQL table.
What to do?
You're ignoring the XML namespaces on your XML document!
<export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://bbhgtm.gov.com/oos/export/1"
xmlns:oos="http://bbhgtm.gov.com/oos/types/1">
See those xmlns=..... and xmlns:oos=...... attributes? Those define XML namespaces that need to be taken into account when querying!
Also, I'd recommend to use the built-in, native XQuery support rather than the clumsy OPENXML code.
Try this code here:
DECLARE #input XML =
'<export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://bbhgtm.gov.com/oos/export/1"
xmlns:oos="http://bbhgtm.gov.com/oos/types/1">
<notificationOK>
<oos:id>8373125</oos:id>
<oos:notificationNumber>0173200001513000422</oos:notificationNumber>
</notificationOK>
</export>'
;WITH XMLNAMESPACES('http://bbhgtm.gov.com/oos/types/1' AS oos,
DEFAULT 'http://bbhgtm.gov.com/oos/export/1')
SELECT
id = XC.value('(oos:id)[1]', 'int'),
NotificationNumber = XC.value('(oos:notificationNumber)[1]', 'bigint')
FROM
#input.nodes('/export/notificationOK') AS XT(XC)
This results in an output something like this:
I'm loading an XML in SQL using OpenXML while declaring the variable the max i can go up to is 8000 chars :
DECLARE #xml_text varchar(8000)
Since text, ntext is not allowed to be used with openXML what other alternatives do i have to load the entire XML (over 20000 chars) in SQL ?
You should be able to use varchar(max) (SQL 2005 and higher)
DECLARE #idoc int
DECLARE #doc varchar(max)
SET #doc = '
<myxml>
<node nodeid="1" nodevalue="value 1">
</node>
</myxml>'
EXEC sp_xml_preparedocument #idoc OUTPUT, #doc
SELECT
*
FROM
OPENXML (#idoc, '/myxml/node',1) WITH ( nodeid varchar(10), nodevalue varchar(20) )
If you're using SQL 2005 or better you could use the XML data type itself. This way you would be able to avoid using OPENXML:
DECLARE #XDoc XML
SET #XDoc = '<Customer>
<FirstName>Fred</FirstName>
<LastName>Flinstone</LastName>
</Customer>
<Customer>
<FirstName>Barney</FirstName>
<LastName>Rubble</LastName>
</Customer>'
SELECT
Tbl.Col.value('FirstName[1]', 'VARCHAR(MAX)'),
Tbl.Col.value('LastName[1]', 'VARCHAR(MAX)')
FROM #XDoc.nodes('/Customer') Tbl(Col)