Xquery to return rows with restricted nodes - sql

I have a table where a column contains XML data. Now i want to retrieve those xml data with restriction of nodes. Kindly see the following example for more explanation on my scenario,
declare #table table (id int, xmlfield xml) insert into #table select 1,'<Root xmlns="">
<Sample>
<Issue>
<Level>one</Level>
<Descp>First Example</Descp>
</Issue>
<Issue>
<Level>two</Level>
<Descp>Second Example</Descp>
</Issue>
</Sample> </Root>'
select * from #table
Now i need the following result set
Id XMLfield
1 first example
ie, for the selected level,i need the decription for it. More clearly, the node should be restricted for <level>one</level>
(need: What is the description for level one ?)
thanks in advance

Have a look at the xml Data Type Methods
select id,
xmlfield.value('(//Issue[Level = "one"]/Descp/text())[1]', 'varchar(100)') as XMLField
from #table

The XQuery you're looking for is
//Issue[Level = "one"]/Descp/data()

Related

Query to XML Node is giving me NULL Value

Trying to achieve what stored procedure used for each report in Report Server.
INSERT INTO #ReportRawXML
SELECT
ItemID, RPT.[Name] AS ReportName,
CONVERT(XML, CONVERT(VARBINARY(MAX), RPT.Content)) AS XMLdata
FROM
ReportServer.dbo.[Catalog] AS RPT
WHERE
RPT.[Name] LIKE '%catalog%'
XML sample:
<Report>
<DataSets>
<DataSet Name="DSET_ReportRepository">
<Query>
<DataSourceName>CCA_PROD</DataSourceName>
</Query>
</DataSets>
</Report>
I have a table with a XML column which I want to query, but I'm getting NULL back; I tried all possible ways, please someone advice.
SELECT
b.ItemID, b.ReportName,
n.c.value('#DataSourceName', 'VARCHAR(MAX)') AS id,
n.c.value('/DataSourceName[1]', 'VARCHAR(500)') AS DataSourceName,
n.c.value('/CommandType[1]', 'VARCHAR(100)') AS CommandType,
n.c.value('/CommandText[1]', 'VARCHAR(100)') AS CommandText
FROM
#ReportRawXML b
OUTER APPLY
b.XMLdata.nodes('/Report/DataSets/DataSet/Query') AS n(c)
Question: getting NULL in column 3 above
Some hints for your next question:
Please try to add a MCVE (a stand-alone-sample as I provide it here for you) right from the start. You know all your details, but we don't...
Try to add a sample, where the sample includes everything (e.g. your sample does not show any CommandType or CommandText
Please read about the internal formatting tools on SO how to add code, normal text, how to highlight or cite...
Please run your sample yourself. Doing so, you would have found, that the XML provided is not well-formed (missing </DataSet>).
But now to your question:
DECLARE #mockupTable TABLE(ID INT IDENTITY, XMLdata XML);
INSERT INTO #mockupTable VALUES
(N'<Report>
<DataSets>
<DataSet Name="DSET_ReportRepository">
<Query>
<DataSourceName>CCA_PROD</DataSourceName>
</Query>
</DataSet>
</DataSets>
</Report>');
--The query
SELECT b.ID
,ds.value('(Query/DataSourceName/text())[1]', 'varchar(max)') as id
,ds.value('#Name', 'varchar(max)') as id
FROM #mockupTable b
OUTER APPLY b.XMLdata.nodes('/Report/DataSets/DataSet') as n(ds);
Reading from XML you must know, that the # before the name indicates an attribute. To read the DataSet's Name attribute, you need this, but not before DataSourceName as in your own attempt.

T-SQL XML: How do I get my results in the format listed below?

This is my first time here. I need to get my XML formatted in the following format:
<Summary Year="2018" QtrNum="1" WeekNum="4" Date="1/22/2018" Code="101">
Once I get it to this format, then I need this XML generated for each row of my table. Here is a screenshot of the expected result set.
Expected Result Set
Thank you in advance.
Although your question is rather unclear, my magig crystal ball tells me, that you might be looking for something like this:
DECLARE #someTable TABLE(SomeDate DATE, SomeQrt INT, SomeWeek INT, SomeCode INT);
INSERT INTO #someTable VALUES('20180122',1,4,101);
DECLARE #SomeRelatedTable TABLE(SomeText VARCHAR(100),SomeCode INT);
INSERT INTO #SomeRelatedTable VALUES('Row 1',101)
,('Row 2',101)
,('Other code',999);
SELECT YEAR(t.SomeDate) AS [#Year]
,t.SomeQrt AS [#QrtNum]
,t.SomeWeek AS [#WeekNum]
,t.SomeDate AS [#Date]
,t.SomeCode AS [#Code]
,(
SELECT SomeText AS [#InnerText]
FROM #SomeRelatedTable rt
WHERE rt.SomeCode=t.SomeCode
FOR XML PATH('InnerNode'),TYPE
)
FROM #someTable t
FOR XML PATH('Summary');
The result
<Summary Year="2018" QrtNum="1" WeekNum="4" Date="2018-01-22" Code="101">
<InnerNode InnerText="Row 1" />
<InnerNode InnerText="Row 2" />
</Summary>
Some Explanation
FOR XML PATH allows full control over the XML generation through the column's alias and PATH / ROOT extension.
The related (inner) nodes are created with a correlated sub-query.
I was able to solve my problem. Look at the scenario below.
drop table #test
create table #test(id int identity(1,1),locationcode varchar(6),businessdate date, quarternum int, weeknum int)
insert into #test(locationcode,businessdate, quarternum, weeknum)
values('000001',GETDATE(),2,2),
('000001',GETDATE(),2,2),
('000002',GETDATE(),2,2),
('000003',GETDATE(),2,2),
('000004',GETDATE(),2,2),
('000005',GETDATE(),2,2),
('000006',GETDATE(),2,2),
('000007',GETDATE(),2,2)
SELECT
(SELECT YEAR(b.businessdate) AS '#Year',b.QuarterNum AS '#QtrNum',b.WeekNum AS '#WeekNum',FORMAT(b.BusinessDate,'MM/DD/YYYY') AS '#Date'
,b.LocationCode AS '#Code'
FROM #test c where b.id = c.id FOR XML PATH('DailySummary'),TYPE) AS Row
FROM #test b

Select SQL Query to get xml node values from ntext column?

I want to get one xml node value from NTEXT column which contains xml based on where clause quering on another xml node value.
RDBMS Type: Microsoft SQL Server T-SQL
Here: I want to get Code node value based on StoreId where clause value. How do I get it?
Input: 100
Output:ABCDE
For example:
<root>
<StoreProfile>
<General>
<StoreId>100</StoreId>
<Code>ABCDE</Code>
</General>
</StoreProfile>
</root>
If you are using SQL Server 2005 or 2008 you can use XQuery like so:
For more on XQuery see XQuery Language Reference
DECLARE #storeId INT
SET #storeId = 100
CREATE TABLE #TestTable
(
xmlColumn NTEXT
)
INSERT INTO #TestTable (xmlColumn) Values('<root><StoreProfile><General><StoreId>100</StoreId><Code>ABCDE</Code></General></StoreProfile></root>')
INSERT INTO #TestTable (xmlColumn) Values('<root><StoreProfile><General><StoreId>200</StoreId><Code>FGHIJ</Code></General></StoreProfile></root>')
SELECT
StoreProfile.value('Code[1]', 'nvarchar(10)') as Code
FROM #TestTable
CROSS APPLY (SELECT CAST(xmlColumn AS XML)) AS A(B)
CROSS APPLY A.B.nodes('//root/StoreProfile/General[StoreId = sql:variable("#storeId")]') AS StoreProfiles(StoreProfile)
DROP TABLE #TestTable

selecting from sql

<suggestions>
<suggestion>
<description>plate.</description>
</suggestion>
<suggestion>
<description>snack.</description>
</suggestion>
</suggestions>
DECLARE #Suggestions TABLE (
[Description] VARCHAR(800)
)
I have above xml in a XML type varible in my stored procedure
how can i insert each text between description tags
in to the table #Suggestions
Try this:
DECLARE #input XML = '<suggestions>
<suggestion>
<description>plate.</description>
</suggestion>
<suggestion>
<description>snack.</description>
</suggestion>
</suggestions>'
DECLARE #Suggestions TABLE ([Description] VARCHAR(800))
INSERT INTO #Suggestions
SELECT
Sugg.value('(description)[1]', 'varchar(800)')
FROM
#input.nodes('/suggestions/suggestion') AS Tbl(Sugg)
SELECT * FROM #Suggestions
The #input.nodes() expression basically turns each <suggestion> node into a row in the "pseudo" table called Tbl(Sugg). From those "rows" (each basically another XML fragment), you then select out the value you're interested in - here the contents of the <description> XML element inside that XML fragment.
You can use LINQ-to-XML to get all suggestions, then you can insert that data into the table.

how to get values inside an xml column, when it's of type nvarchar

My question is similar to this one: Choose a XML node in SQL Server based on max value of a child element
except that my column is NOT of type XML, it's of type nvarchar(max).
I want to extract the XML node values from a column that looks like this:
<Data>
<el1>1234</el1>
<el2>Something</el2>
</Data>
How can I extract the values '1234' and 'Something' ?
doing a convert and using the col.nodes is not working.
CONVERT(XML, table1.col1).value('(/Data/el1)[1]','int') as 'xcol1',
After that, I would like to do a compare value of el1 (1234) with another column, and update update el1 as is. Right now I'm trying to just rebuild the XML when passing the update:
ie
Update table set col1 ='<Data><el1>'+#col2+'</el1><el2>???</el2>
You've got to tell SQL Server the number of the node you're after, like:
(/Data/el1)[1]
^^^
Full example:
declare #t table (id int, col1 varchar(max))
insert #t values (1, '<Data><el1>1234</el1><el2>Something</el2></Data>')
select CAST(col1 as xml).value('(/Data/el1)[1]', 'int')
from #t
-->
1234
SQL Server provides a modify function to change XML columns. But I think you can only use it on columns with the xml type. Here's an example:
declare #q table (id int, col1 xml)
insert #q values (1, '<Data><el1>1234</el1><el2>Something</el2></Data>')
update #q
set col1.modify('replace value of (/Data/el1/text())[1] with "5678"')
select *
from #q
-->
<Data><el1>5678</el1><el2>Something</el2></Data>
At the end of the day, SQL Server's XML support makes simple things very hard. If you value maintainability, you're better off processing XML on the client side.