Extract XML Node data SQL Query - sql-server

I am trying to extract XML node from SQL query. I have XML report specification in one of the column of SQL table.
I want to extract sqlText node from the XML using SQL query.
I have uploaded the xml to dropbox:
https://www.dropbox.com/sh/28xu7ifu78h6gm0/AACQS24NEjPFO0GXEI9vLuefa?dl=0
Any help would be greatly appreciated.

If you are using xml document
declare #xml xml ='<report ....</report>'
select #xml.value('(/report/queries/query/source/sqlQuery/sqlText)[1]', 'varchar(100)') as [SqlText]

Assuming that the XML data is stored in XML typed column named myXmlColumn in SQL table named myTable, you can specify the default namespace for the XPath using ;WITH XMLNAMESPACES(), for example :
;WITH XMLNAMESPACES(default 'http://developer.cognos.com/schemas/report/8.0/')
select myTable.myXmlColumn.value('(/report/queries/query/source/sqlQuery/sqlText)[1]'
, 'varchar(max)') as sqlText

If there are more than one sqlQuery node, following SQL XML query can be used
SELECT
[Query].value('.','varchar(100)') AS QueryText
FROM #xml.nodes('/report/queries/query/source/sqlQuery/sqlText') Queries([Query])

Related

Want to store XML in SQL Server 2014 table and apply select on same

Having data in the for of XML and I want to store that XML in a SQL Server table. I have created a column of datatype xml and tried to store my XML data in that newly created column, but I get an error on INSERT:
The data type 'xml' used in the VALUE method is invalid.
This is the Insert statement:
INSERT INTO UserDeatailtbl(RoleXml)
SELECT Y.ID.value('./RoleXml[1]', 'XML')
FROM #UserDetailsXML.nodes('UserDetails/UserDetailsRow') AS Y(ID)
Try to use the .query() operator instead of .value() - like this:
INSERT INTO UserDetailtbl(RoleXml)
SELECT ID.query('.')
FROM #UserDetailsXML.nodes('UserDetails/UserDetailsRow/RoleXml') AS Y(ID)
Maybe that'll work, since the .query() operator is returning an XML fragment by definition.
If it doesn't work, we'll need to know more about your XML structure - can you show us an example of your XML stored in the #UserDetailsXML variable? Which part of that do you want to insert into UserDetailTbl.RoleXml?

inline schema is not supported with for xml path xmlschema

I would like to write a stored procedure as below in SQL Server.
CREATE PROC [dbo].[Employee_delete]
AS
BEGIN
SELECT *
FROM #employee
FOR XML PATH(''), root ('EmployeeDelete'),xmlschema
END
But I'm getting the error--> 'inline schema is not supported with for xml path xmlschema'. May I know, how can I achieve XMLSchema with code "FOR XML PATH(''), root ('EmployeeDelete'),xmlschema"
To auto generate schemas in biztalk , I have to use word "xmlSchema"
Thanks in advance
If you are using BizTalk-2010 you should be using the WCF-SQL adapter for which you don't need to select as FOR XML to be able to generate schemas.
Try...
FOR XML AUTO,ROOT('EmployeeDelete'), TYPE, ELEMENTS XSINIL, XMLSCHEMA

What's mean of the "T(C)"? when I query xml in sql server 2008

What's mean of the "T(C)"?
DECLARE #xml XML ='<root><id>1</id><id>2</id><id>3</id><id>4</id></root>'
SELECT
T.C.value('(text())[1]','varchar(20)')
FROM #xml.nodes('/root/id') AS T(C)
Note:can you give me a perfessional url,I want to see carefully.
thx very much.
From MSDN Nodes() Function syntax is
nodes (XQuery) as Table(Column)
Table(Column)
Is the table name and the column name for the resulting rowset.

Querying XML data in sql server 2005

i got a snippet which show how to query xml data in sql server but few area still not clear to me.
DECLARE #xml XML
SET #xml = '<root>
<row>one</row>
<row>two</row>
<row>three</row>
</root>'
CREATE TABLE #Fields(Field varchar(MAX))
INSERT INTO #Fields
SELECT
x.y.value('text()[1]', 'varchar(5)')
FROM #xml.nodes('root/row') x(y)
SELECT * FROM #Fields
DROP TABLE #Fields
what is x.y i just do not understand and also what kind of syntax is it 'text()[1]', 'varchar(5)'
if text() is in-built function then does it work for any data type ?
please help me to visualize what x.y ?? thanks
The .nodes() call converts your XML into a list of XML fragments - and a "list" in SQL Server always is a table - so this is really a table alias (x) and a column alias (y) for an internally constructed tables of XML fragments (one "row" per match for .nodes())
If you would be using .nodes('/root)` then you'd get an "pseudo table" with these rows:
Table x
y
----------------
<row>one</row>
<row>two</row>
<row>three</row>
Since you're using .nodes('/root/row') instead, you're really getting just the values of those <row> elements:
Table x
y
----------------
one
two
three
The .value() now takes one of those XML fragments and returns something from it. It could be the name of a sub-element - but text() (yes, a built-in XQuery function) just converts the whole XML fragment into a textual representation. The second parameter of the .value() call just defines what that value should be treated as - what datatype.

select xml rows without column header in SQL Server FOR XML

In SQL Server 2008 R2, I have a table with rows of valid XML nodes (one column, xml type) that looks like this:
create table #t (x xml not null);
insert into #t
values ('<service><value1>stuff</value1><value2>more stuff</value2></service>')
, ('<service><value1>I am a different row</value1><value2>more stuff</value2></service>');
I would like to select these into one XML blob:
<services>
<service>. . .</service>
<service>. . .</service>
</services>
I am using FOR XML PATH to try this, but it embeds the column header "x" as a node around the "service" nodes.
select x
from #t
for XML PATH(''), ROOT('services')
This produces:
<services>
<x><service>. . .</service></x>
<x><service>. . .</service></x>
</services>
How can I get rid of the "x" node?
I tried select x as "" and select x as "." but these are reserved words, and the query errors out.
How about this?
select x.query('*')
from #t
for xml path(''), root('services')

Resources