Querying XML tag in SQL server - sql

I have a table Student with a column studentStateinfo which consist of XML value as below.
<params xmlns="">
<OldStudentID>1aedghe1d8ef</OldStudentID>
</params>
Now when I query this table Student I only want to check whether studentStateinfo column have an XML data with tag <OldStudentID>

Use the exist() Method (xml Data Type)
Example using a variable, you should change that to a column instead.
declare #X xml = '
<params xmlns="">
<OldStudentID>1aedghe1d8ef</OldStudentID>
</params>';
select #X.exist('/params/OldStudentID');

Related

How to extract multiple xml element into individual tables?

I have XML data like this
DECLARE #input XML =
'<LicensingReportProcessResult>
<LicensingReport>
<Address key="3845HoopaLnLasVegasNV89169-3350U.S.A.">
<LineOne>3845 Hoopa Ln</LineOne>
<CityName>Las Vegas</CityName>
<StateOrProvinceCode>NV</StateOrProvinceCode>
<PostalCode>89169-3350</PostalCode>
<CountryCode>U.S.A.</CountryCode>
</Address>
<Person key="PersonPRI711284842">
<ExternalIdentifier>
<TypeCode>NAICProducerCode</TypeCode>
<Id>8001585</Id>
</ExternalIdentifier>
<BirthDate>1961-07-29</BirthDate>
</Person>
</LicensingReport>
</LicensingReportProcessResult>'
My T-SQL code to extract one specific set of elements:
-- extract into temp table
INSERT INTO #Address
SELECT
Tbl.Col.value('#Address', 'NVARCHAR(100)'),
Tbl.Col.value('#City', 'NVARCHAR(100)'),
Tbl.Col.value('#State', 'NVARCHAR(100)'),
Tbl.Col.value('#PostalCode', 'NVARCHAR(100)'),
Tbl.Col.value('#CountryCode', 'NVARCHAR(100)')
FROM
#xml.nodes('//LicensingReportProcessResult/LicensingReport/Address') Tbl(Col)
-- verify results
SELECT * FROM #Address
I want to insert different element data into separate tables. Like Address data into an Address table and Person data into a Person table. As new elements are added I want to save data into separate tables.
Can someone help?
Are you asking how to dynamically define new tables for top level xml elements in a document? You can do that with any Xml Serialization library that reads a document and returns the elements and attributes as a tree, and from that metadata create a table definition that you then execute in sql.
Also consider simply storing your data as xml, perhaps with a defined schema, and then writing queries or views that extract the various elements using XPath or the xml data type methods as you already do, instead of extracting into physical tables.

Is it possible to fetch the name attribute value from this XML using SQL query

I have a table named Person having column name Message which has XML data.
"<Name name="George", Id="123" />"
Is it possible to fetch the name attribute value from this XML using SQL query.

Extracting XML data from NCLOB from XML with attributes in Oracle db

I am trying to extract values from XML data stored as NCLOB column in Oracle db table.
xml structure
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://example.com/FAS/DOC/2011/v3.0b" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<record xmlns="http://example.com/FAS/DOC/2011/v3.0b">
<pdate xmlns="http://example.com/FAS/DOC/2011/v3.0b">2014-05-15</pdate>
</record>
</root>
Query
select EXTRACTVALUE(XMLTYPE(nclob_column),'/root/record/pdate','xmlns="http://example.com/FAS/DOC/2011/v3.0b"') pdate1,
EXTRACTVALUE(XMLTYPE(nclob_column),'/root/record/pdate') pdate2
from nclob_table
Problem
The pdate1 does return the value, but pdate2 returns null. I
cannot use the third parameter of EXTRACTVALUE() to specify the xmlns
attribute value as that changes on every row/record. So I get the
value for one row but null for all others.
How do I extract the value without specifying the attribute?
Thanks.
If you can guarantee that the namespace doesn't matter for selecting an element in this case, you can use local-name() to match element only by it's local name, ignoring the namespace :
select EXTRACTVALUE(
XMLTYPE(nclob_column),
'/*[local-name()="root"]/*[local-name()="record"]/*[local-name()="pdate"]'
) pdate
from nclob_table
SQL Fiddle

FOR XML AUTO and sql server 2005

SELECT * FROM emp FOR XML AUTO , ROOT ('Employees') , ELEMENTS
this query return value in xml format but total xml is showing in one column and column name is showing dynamically. if i want that i will specify the column name like data etc. so xml value will show as single row & column but header or output column name will be "DATA"
is it possible if yes the please show me the way.thanks
You need to check out the new FOR XML PATH feature in SQL Server 2005 and up - read all about it in SQL Server Books Online.
Basically, with FOR XML PATH, you can define the shape of your XML very easily, e.g.
SELECT
EmployeeID AS '#EmployeeID',
FirstName,
LastName
FROM dbo.Employees
FOR XML PATH('Employee'), ROOT('AllEmployees')
will generate XML something like:
<AllEmployees>
<Employee EmployeeID="12345">
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Employee>
</AllEmployees>
FOR XML PATH is very flexible and allows you to do a lot of things quite easily and intuitively.

How to select a particular Node name in XML using Oracle SQL query?

I need to select a particular Node name in the XML file using sql string?
Sample XML structure
<Root>
<Body>
<Car> // This can be "Bike", "ship", "Train"... ect
<name value="figo"/>
</Car>
</Body>
</Root>
I want to run a query which which will fetch what Node name is present in XML "car" or "Train" or "Bike".. etc.
Select * from TableA where.....?
TableA has column "Message" of type CLOB which stores the XML.
-Praveen
You can cast the CLOB type to XMLTYPE (or consider using an XMLTYPE column on the table). When dealing with XMLTYPEs you can then run XPATHs e.g:
SELECT extractvalue(xml_col, '/*/Body/*/name/#value')
FROM TableA
or extract for xml fragments.
EDIT: changed the 'Car' to * in XPath, having re-read the question.