Extracting data from XML Nodes - sql

I am testing this simple script and just wondering why it didn't work.
DECLARE #myDoc xml
DECLARE #ProdID int
SET #myDoc =
'<Datatype xmlns="V8_0_1" id="113" name="PF_CleaningMode" hdl="47/4/SB8_3-910-8243-19/0/113" odobjid="915">
<Datatypevalue id="2" name="Intermittent" />
<Datatypevalue id="1" name="Continuous" />
<Datatypevalue id="0" name="Off" />
</Datatype>'
SET #ProdID = #myDoc.value('(Datatype/#id)[1]', 'int' )
SELECT #ProdID
Hope you can assist. Thanks.

You're just simply ignoring the default XML namespace that's defined on your <Datatype> node:
<Datatype xmlns="V8_0_1" id="113" name="PF_CleaningMode"
**************
You need to include that in your query!
Like this:
;WITH XMLNAMESPACES(DEFAULT 'V8_0_1')
SELECT #myDoc.value('(Datatype/#id)[1]', 'INT')
and you'll get the expected output of
113

Related

How read xml data from a xml file without tags in SQL Server?

I know how to read the xml data from a file where the info is organized in tags, I mean a file like this:
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<Administrador>
<id> 8 </id>
<nombre> Nelle </nombre>
<valorDocId> 8399335355 </valorDocId>
<contrasenna> Glenn </contrasenna>
</Administrador>
<Administrador>
<id> 9 </id>
<nombre> Gayler </nombre>
<valorDocId> 1310348693 </valorDocId>
<contrasenna> Madonna </contrasenna>
</Administrador>
</dataset>
The code I used to read it is:
use Proyecto1
declare #filedata XML
select #filedata=BulkColumn from OpenRowSet(Bulk'File directory', Single_blob) x;
insert into Table(id, nombre, valorDocId, clave)
select
xData.value('id[1]', 'int') id,
xData.value('nombre[1]','varchar(30)') nombre,
xData.value('valorDocId[1]','int') valorDocId,
xData.value('contrasenna[1]','varchar(20)') clave
from #fileData.nodes('/dataset/Administrador') as
x(xData)
But now I need to read a xml file that is not organized in tags, at least not like the last one, the xml is like this:
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<Administrador id="1" nombre="Nelle" valorDocId="8399335355" contrasenna="Glenn"/>
<Administrador id="2" nombre="Gayler" valorDocId="1310348693" contrasenna="Madonna"/>
</dataset>
But the code I used before doesn't works, it throws an error that says that I can't insert a NULL value in the column 'id', so what I supposed is that the data is not being read. So how can I read that second file?
Example
Declare #XML xml = '
<dataset>
<Administrador id="1" nombre="Nelle" valorDocId="8399335355" contrasenna="Glenn"/>
<Administrador id="2" nombre="Gayler" valorDocId="1310348693" contrasenna="Madonna"/>
</dataset>
'
Select id = x.v.value('#id','int')
,nombre = x.v.value('#nombre','varchar(50)')
,valorDocId = x.v.value('#valorDocId','varchar(50)')
,contrasenna = x.v.value('#contrasenna','varchar(50)')
From #Xml.nodes('dataset/Administrador') x(v)
Returns
id nombre valorDocId contrasenna
1 Nelle 8399335355 Glenn
2 Gayler 1310348693 Madonna
EDIT - To Get your XML from a File
Declare #XML xml
Select #XML = BulkColumn FROM OPENROWSET(BULK 'C:\Working\SomeXMLFile.xml', SINGLE_BLOB) x;
Select id = x.v.value('#id','int')
,nombre = x.v.value('#nombre','varchar(50)')
,valorDocId = x.v.value('#valorDocId','varchar(50)')
,contrasenna = x.v.value('#contrasenna','varchar(50)')
From #Xml.nodes('dataset/Administrador') x(v)

OpenXML returning NULL

I am trying to import xml into my database with the following query using OpenXML in Microsoft SQL Server:
DECLARE #xml XML;
DECLARE #y INT;
SET #xml
= '<ArrayOfArticle xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Article>
<ScriptId xmlns="https://test.com/">5135399</ScriptId>
<Title xmlns="https://test.com/">Stocks divided into two corners</Title>
<Mediatype xmlns="https://test.com/">News papeer</Mediatype>
<Abstract xmlns="https://test.com/">Foreign capital doubled this year.</Abstract>
<ScriptDate xmlns="https://test.com/">2017-12-30T00:00:00</ScriptDate>
<ScriptTypeId xmlns="https://test.com/">1</ScriptTypeId>
<ScriptType xmlns="https://test.com/">News general</ScriptType>
<Media xmlns="https://test.com/">Times</Media>
<ArticleUrl xmlns="https://test.com/">http://test.com</ArticleUrl>
<AnalysisResult xmlns="https://test.com/">
<Analysis>
<Regno>111</Regno>
<Name>New York Times</Name>
<Result>1</Result>
<ResultName>Positive</ResultName>
</Analysis>
<Analysis>
<Regno>222</Regno>
<Name>Washington Post</Name>
<Result>1</Result>
<ResultName>Negative</ResultName>
</Analysis>
</AnalysisResult>
<FacebookStats xmlns="https://test.com/">
<ShareCount xsi:nil="true" />
<LikeCount xsi:nil="true" />
<CommentCount xsi:nil="true" />
<TotalCount xsi:nil="true" />
</FacebookStats>
<MediaScore xmlns="https://test.com/">
<MediaScore>
<Regno>111</Regno>
<CompanyName>New York Times</CompanyName>
<MediaScoreID>2</MediaScoreID>
<Name>Neither</Name>
</MediaScore>
<MediaScore>
<Regno>222</Regno>
<CompanyName>Washington Post</CompanyName>
<MediaScoreID>2</MediaScoreID>
<Name>Neither</Name>
</MediaScore>
</MediaScore>
<Page xmlns="https://test.com/">26</Page>
<ProgramId xmlns="https://test.com/">0</ProgramId>
<ProgramTime xmlns="https://test.com/" xsi:nil="true" />
<ProgramLength xmlns="https://test.com/">0</ProgramLength>
<ProgramOrder xmlns="https://test.com/">0</ProgramOrder>
</Article>
</ArrayOfArticle>';
EXEC sp_xml_preparedocument #y OUTPUT, #xml;
SELECT *
FROM
OPENXML(#y, '/ArrayOfArticle/Article', 1)
WITH
(
ScriptId VARCHAR(20),
Title VARCHAR(30),
Mediatype VARCHAR(30)
);
The query however only returns NULL values. What am I missing here? Would it be optimal to import the XML using SSIS instead. Not sure how much more details I can give at the given hour.
Do not use FROM OPENXML. This approach (together with the corresponding SPs to prepare and to remove a document) is outdated and should not be used any more.
Try the XML type's native methods, in this case .value():
Your XML is rather weird - concerning namespaces. If its creation is under your control you should try to clean this namespace mess. The unusual thing is, that your XML declares default namespaces over and over.
You can use the deep search with // together with a namespace wildcard *:
--GetItEasyCheesy (not recommended)
SELECT #xml.value(N'(//*:ScriptId)[1]',N'int') AS ScriptId
,#xml.value(N'(//*:Title)[1]',N'nvarchar(max)') AS Title
,#xml.value(N'(//*:Mediatype )[1]',N'nvarchar(max)') AS Mediatype ;
You can declare the namespace as default, but in this case you must wildcard the outer elements, as they are not part of this namespace:
--Use a default namespace
WITH XMLNAMESPACES(DEFAULT 'https://test.com/')
SELECT #xml.value(N'(/*:ArrayOfArticle/*:Article/ScriptId/text())[1]',N'int') AS ScriptId
,#xml.value(N'(/*:ArrayOfArticle/*:Article/Title/text())[1]',N'nvarchar(max)') AS Title
,#xml.value(N'(/*:ArrayOfArticle/*:Article/Mediatype/text())[1]',N'nvarchar(max)') AS Mediatype;
The recommended approach is to bind the inner namespace to a prefix and use this
--Recommended
WITH XMLNAMESPACES('https://test.com/' AS ns)
SELECT #xml.value(N'(/ArrayOfArticle/Article/ns:ScriptId/text())[1]',N'int') AS ScriptId
,#xml.value(N'(/ArrayOfArticle/Article/ns:Title/text())[1]',N'nvarchar(max)') AS Title
,#xml.value(N'(/ArrayOfArticle/Article/ns:Mediatype/text())[1]',N'nvarchar(max)') AS Mediatype;
If your <ArrayOfArticles> contains more than one <Article> you can use .nodes() to get alle of them as derived table. In this case the query is
WITH XMLNAMESPACES('https://test.com/' AS ns)
SELECT art.value(N'(ns:ScriptId/text())[1]',N'int') AS Recommended
,art.value(N'(ns:Title/text())[1]',N'nvarchar(max)') AS Title
,art.value(N'(ns:Mediatype/text())[1]',N'nvarchar(max)') AS Mediatype
FROM #xml.nodes(N'/ArrayOfArticle/Article') AS A(art);
your XML contains namespaces, I'd use xquery in order to extract the data from your XML
UPDATE with additional elements extract
DECLARE #xml XML;
SET #xml
= '<ArrayOfArticle xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Article>
<ScriptId xmlns="https://test.com/">5135399</ScriptId>
<Title xmlns="https://test.com/">Stocks divided into two corners</Title>
<Mediatype xmlns="https://test.com/">News papeer</Mediatype>
<Abstract xmlns="https://test.com/">Foreign capital doubled this year.</Abstract>
<ScriptDate xmlns="https://test.com/">2017-12-30T00:00:00</ScriptDate>
<ScriptTypeId xmlns="https://test.com/">1</ScriptTypeId>
<ScriptType xmlns="https://test.com/">News general</ScriptType>
<Media xmlns="https://test.com/">Times</Media>
<ArticleUrl xmlns="https://test.com/">http://test.com</ArticleUrl>
<AnalysisResult xmlns="https://test.com/">
<Analysis>
<Regno>111</Regno>
<Name>New York Times</Name>
<Result>1</Result>
<ResultName>Positive</ResultName>
</Analysis>
<Analysis>
<Regno>222</Regno>
<Name>Washington Post</Name>
<Result>1</Result>
<ResultName>Negative</ResultName>
</Analysis>
</AnalysisResult>
<FacebookStats xmlns="https://test.com/">
<ShareCount xsi:nil="true" />
<LikeCount xsi:nil="true" />
<CommentCount xsi:nil="true" />
<TotalCount xsi:nil="true" />
</FacebookStats>
<MediaScore xmlns="https://test.com/">
<MediaScore>
<Regno>111</Regno>
<CompanyName>New York Times</CompanyName>
<MediaScoreID>2</MediaScoreID>
<Name>Neither</Name>
</MediaScore>
<MediaScore>
<Regno>222</Regno>
<CompanyName>Washington Post</CompanyName>
<MediaScoreID>2</MediaScoreID>
<Name>Neither</Name>
</MediaScore>
</MediaScore>
<Page xmlns="https://test.com/">26</Page>
<ProgramId xmlns="https://test.com/">0</ProgramId>
<ProgramTime xmlns="https://test.com/" xsi:nil="true" />
<ProgramLength xmlns="https://test.com/">0</ProgramLength>
<ProgramOrder xmlns="https://test.com/">0</ProgramOrder>
</Article>
</ArrayOfArticle>'
DECLARE #T TABLE (XmlCol XML)
INSERT INTO #T
SELECT #xml
;WITH XMLNAMESPACES ('https://test.com/' as p1)
SELECT z.t.value ('../../p1:ScriptId[1]',' varchar(100)') ScriptId,
z.t.value ('../../p1:Title[1]',' varchar(100)') Title,
z.t.value ('../../p1:Mediatype[1]',' varchar(100)') Mediatype,
z.t.value ('p1:CompanyName[1]', 'varchar(100)') CompanyName
FROM #T t
CROSS APPLY XmlCol.nodes ('/ArrayOfArticle/Article/p1:MediaScore/p1:MediaScore') z(t)
DECLARE #y INT
EXEC sp_xml_preparedocument #y OUTPUT, #xml,
'<ns xmlns:x="https://test.com/"/>'
SELECT *
FROM
OPENXML(#y, '/ArrayOfArticle/Article', 2)
WITH
(
[ScriptId] VARCHAR(20) 'x:ScriptId', --<< and so on
[Title] VARCHAR(30),
Mediatype VARCHAR(30)
)
EXEC sp_xml_removedocument #y --<< lost in your code

Replace XML Data in SQL Server 2012

I have a request to replace the XML property with another tables column value. Currently I'm confused on how to proceed further. I did some investigation but none of the seems to be working for my scenario. Could someone help me out with this. Thanks in advance.
I have the below query
DECLARE #XML XML
SET #XML = '<LookupSyndicationExportProfileData Id="-1" Name="">
<LookupExportScopes IncludeAllLookup="False" IncludeAllLocale="False" GroupBy="ColumnName" FileFormat="Excel10">
<Locales>
<Locale PK_Locale="1" ShortName="English World Wide" LongName="" FK_Culture="0" FK_Region="0" FK_Lang="0" />
</Locales>
<LookupExportScope Id="0" Name="Brand" />
<LookupExportScope Id="1" Name="BrandColor" />
<LookupExportScope Id="2" Name="BrandType" />
</LookupExportScopes>
</LookupSyndicationExportProfileData>'
SELECT
'tblk_' + N.C.value('#Name', 'nvarchar(100)') NAME,
N.C.value('#Id', 'nvarchar(100)') Id,
ro.Pk_RST_Object
FROM
#XML.nodes('//LookupExportScope') N(C)
INNER JOIN
tb_RST_Object ro ON 'tblk_' + N.C.value('#Name','nvarchar(100)') = ro.Shortname
Below is the result of the above query.
So, I need to update my #XML variable with Id property to the PK_RST_Object value. Please let me know how shall I do this.

Save XML with attribute to Table in SQL Server

Hi I have XML data with attribute as input for SQL, i need this to be inserted in my table.
XML Data is
<?xml version="1.0" encoding="ISO-8859-1"?>
<MESSAGEACK>
<GUID GUID="kfafb30" SUBMITDATE="2015-10-15 11:30:29" ID="1">
<ERROR SEQ="1" CODE="28681" />
</GUID>
<GUID GUID="kfafb3" SUBMITDATE="2015-10-15 11:30:29" ID="1">
<ERROR SEQ="2" CODE="286381" />
</GUID>
</MESSAGEACK>
I want this to be inserted in below Format
GUID SUBMIT DATE ID ERROR SEQ CODE
kfafb3 2015-10-15 11:30:29 1 1 28681
kfafb3 2015-10-15 11:30:29 1 1 2868
please help.
Look into XPath and xml Data Type Methods in MSDN. This is one possible way :
declare #xml As XML = '...you XML string here...'
INSERT INTO YourTable
SELECT
guid.value('#GUID', 'varchar(100)') as 'GUID'
,guid.value('#SUBMITDATE', 'datetime') as 'SUBMIT DATE'
,guid.value('#ID', 'int') as 'ID'
,guid.value('ERROR[1]/#SEQ', 'int') as 'SEQ'
,guid.value('ERROR[1]/#CODE', 'int') as 'CODE'
FROM #xml.nodes('/MESSAGEACK/GUID') as x(guid)
Result :
just paste this into an empty query window and execute. Adapt to your needs:
DECLARE #xml XML=
'<?xml version="1.0" encoding="ISO-8859-1"?>
<MESSAGEACK>
<GUID GUID="kfafb30" SUBMITDATE="2015-10-15 11:30:29" ID="1">
<ERROR SEQ="1" CODE="28681" />
</GUID>
<GUID GUID="kfafb3" SUBMITDATE="2015-10-15 11:30:29" ID="1">
<ERROR SEQ="2" CODE="286381" />
</GUID>
</MESSAGEACK>';
SELECT Msg.Node.value('#GUID','varchar(max)') AS [GUID] --The value is no GUID, if the original values are, you could use uniqueidentifier instead of varchar(max)
,Msg.Node.value('#SUBMITDATE','datetime') AS SUBMITDATE
,Msg.Node.value('#ID','int') AS ID
,Msg.Node.value('(ERROR/#SEQ)[1]','int') AS [ERROR SEQ]
,Msg.Node.value('(ERROR/#CODE)[1]','int') AS CODE
FROM #xml.nodes('/MESSAGEACK/GUID') AS Msg(Node)

Parsing XML using TSQL

I'm trying to parse out the following XML with TSQL:
<Response xmlns="http://data.fcc.gov/api" status="OK" executionTime="9">
<Block FIPS="181770103002004" />
<County FIPS="18177" name="Wayne" />
<State FIPS="18" code="IN" name="Indiana" />
</Response>
Using the following script:
SELECT x.i.value('#name', 'varchar(200)') AS county
FROM #xml.nodes('Response/County') AS x(i)
But I get no results, any help as to what I'm doing wrong would be greatly appreciated.
Thanks!
Your XML namespace is messing things up. Either remove the xmlns="http://data.fcc.gov/api" from the Response element, or prefix your query with WITH XMLNAMESPACES ( DEFAULT 'http://data.fcc.gov/api')
;WITH XMLNAMESPACES ( DEFAULT 'http://data.fcc.gov/api')
SELECT x.i.value('#name', 'varchar(200)') AS county
FROM #xml.nodes('Response/County') AS x(i)
Or you can use wildcard namespaces in the query:
SELECT x.i.value('#name', 'varchar(200)') AS county
FROM #xml.nodes('*:Response/*:County') AS x(i)
You can do it using OPENXML like this:
DECLARE #idoc INT
DECLARE #xml AS XML =
'<Response xmlns="http://data.fcc.gov/api" status="OK" executionTime="9">
<Block FIPS="181770103002004" />
<County FIPS="18177" name="Wayne" />
<State FIPS="18" code="IN" name="Indiana" />
</Response>'
EXEC sp_xml_preparedocument #idoc OUTPUT, #xml, N'<root xmlns:n="http://data.fcc.gov/api" />'
SELECT
Name AS County
FROM OPENXML (#idoc, '/n:Response/n:County', 1)
WITH
(
Name VARCHAR(255) '#name'
)
EXEC sp_xml_removedocument #idoc
GO