No return value on using OpenXML command in SQL Server 2008 - sql

This is my XML parameter that I set to #XMLSave parameter and send to a stored procedure
<ROOT>
<P>
<ID>123456789</ID>
<Name>admin</Name>
</P>
<Group>
<GroupCardID>14</GroupCardID>
</Group>
</ROOT>
and I try to get ID value with this command
EXEC sp_xml_preparedocument #idoc OUTPUT, #XMLSave
but when I select values return no value
select *
from OPENXML (#idoc,'/Root/P',2) With(ID int)

Try this:
DECLARE #XmlParameter XML = '<ROOT>
<P>
<ID>123456789</ID>
<Name>admin</Name>
</P>
<Group>
<GroupCardID>14</GroupCardID>
</Group>
</ROOT>'
SELECT
#XmlParameter.value('(/ROOT/P/ID)[1]', 'int')
I always prefer the native XQuery support over the clunky old OPENXML stuff.....

I Found the answer finally:
OpenXML parameter is case sensitive :
my XML value start with "ROOT" and openxml parameter was "Root"

Related

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

XML Query with a variable not working

declare #xml xml =
'<ChangeSet DBSchemaVersion="2.0.0.88" DBSyncVersion="15" ScopeId="1">
<C op="I" PK="44">
<User UserId="44" Role="3" UserName="Dummy1" />
</C>
<C op="I" PK="45">
<User UserId="45" Role="3" UserName="Dummy2" />
</C>
<C op="I" PK="46">
<User UserId="46" Role="3" UserName="Dummy3" />
</C>
</ChangeSet>'
-- this works fine
SELECT [SyncTable].Col.query('User')
FROM #xml.nodes('/ChangeSet/C[#op=''I'']') as [SyncTable](Col)
FOR XML AUTO
-- this does not work
DECLARE #tableName NVARCHAR(MAX) = 'User'
SELECT [SyncTable].Col.query('sql:variable(''#tableName'')')
FROM #xml.nodes('/ChangeSet/C[#op=''I'']') as [SyncTable](Col)
FOR XML AUTO
The first returns:
<SyncTable><User UserId="44" Role="3" UserName="Dummy1"/></SyncTable><SyncTable><User UserId="45" Role="3" UserName="Dummy2"/></SyncTable><SyncTable><User UserId="46" Role="3" UserName="Dummy3"/></SyncTable>
The second returns:
<SyncTable>User</SyncTable><SyncTable>User</SyncTable><SyncTable>User</SyncTable>
I'm trying to do this in a procedure where the table name is a parameter. Is there a way to do what I'm trying to achieve?
SQL Server 2008 R2
TIA
SQL variable value passed to the xquery expression as literal string, and AFAICS there is no way to inject literal string as xquery/xpath step expression.
However, there is a way around this, namely, filtering element having name equals specific string. Here, specific string is the literal string from SQL variable, for example :
DECLARE #tableName NVARCHAR(MAX) = 'User'
SELECT [SyncTable].Col.query('*[local-name()=sql:variable("#tableName")]')
FROM #xml.nodes('/ChangeSet/C[#op="I"]') as [SyncTable](Col)
FOR XML AUTO
SQL Fiddle Demo
This part *[local-name()=sql:variable("#tableName")] reads : select any child of current context element, having local-name equals value of #tableName variable.

Parse the XML in SQL Server

<ROOT>
<arn>arn001</arn>
<arn>arn002</arn>
</ROOT>
Tried the following code though
SELECT
ARN.value('(//arn/text())[1]','VARCHAR(100)') AS arns --TAG
FROM
#xml.nodes('/ROOT')AS TEMPTABLE(ARN)
It returns only first value
Try this way :
declare #xml xml = '<ROOT>
<arn>arn001</arn>
<arn>arn002</arn>
</ROOT>'
SELECT
X.value('.','VARCHAR(100)') AS arns
FROM
#xml.nodes('/ROOT/arn')AS T(X)
SQL Fiddle

T-SQL Delete a node from XML

I need help, deleting the Error node where the TabID ="Claim", I'm, using Sql server 2012
<ClaimErrors ClaimID="73">
<Error ErrorMessage="offer timeframe" TabID="Offer" FieldName="PostMarkDate" ErrorType="FrontEnd" NoteID="2" />
<Error ErrorMessage=" code is not valid" TabID="Claim" FieldName="None" NoteID="3811257" ErrorType="BackEnd Error" />
</ClaimErrors>
declare #XML table (data xml)
INsert into #XML select '<ClaimErrors ClaimID="73"><Error ErrorMessage="offer timeframe" TabID="Offer" FieldName="PostMarkDate" ErrorType="FrontEnd" NoteID="2" />
<Error ErrorMessage="code is not valid" TabID="Claim" FieldName="None" NoteID="3811257" ErrorType="BackEnd" /></ClaimErrors>'
update #XML set data.modify('delete //ClaimErrors/Error[text()][contains(.,"TabID=Claim")]')
I'm using data.modify('delete //ClaimErrors/Error/ but with no hope.
any help will be appreciated..
Thanks
declare #XML table (data xml)
INsert into #XML select '<ClaimErrors ClaimID="73"><Error ErrorMessage="offer timeframe" TabID="Offer" FieldName="PostMarkDate" ErrorType="FrontEnd" NoteID="2" />
<Error ErrorMessage="code is not valid" TabID="Claim" FieldName="None" NoteID="3811257" ErrorType="BackEnd" /></ClaimErrors>'
declare #var nvarchar(256)= 'Claim'
update #XML set data.modify('delete (/ClaimErrors/Error[#TabID=sql:variable("#var")])[1]')
select data from #XML

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