FOR XML AUTO and sql server 2005 - 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.

Related

Saving all nodes from a big XML to SQL Database table in XMLType column using SSIS efficiently

I have a XML with many records as nodes in it. I need to save each record in xml format a SQL server table in column of XML datatype .
I can perform this task in SSIS using "XML Task Editor" to count all the nodes and using "For Loop Container" and read Node value using "XML Task Editor" and save it database.
Another option is using Script task, reading the XML file and save each node in a loop.
Please suggest a better approach which is efficient with big files.
Below is sample of Input XML File. I need to save each (3 records in below example) "RECORD" full node in XML form in SQL Server database table which has a column with xml datatype.
I would suggest 2 step approach.
Use SSIS Import Column Transformation in a Data Flow Task to load entire XML file into a staging table single row column.
Use stored procedure to produce individual RECORD XML fragments as separate rows and INSERT them into a final destination table.
SQL
DECLARE #staging_tbl TABLE (id INT IDENTITY PRIMARY KEY, xmldata XML);
INSERT INTO #staging_tbl (xmldata) VALUES
(N'<root>
<RECORD UI="F298AF1F"></RECORD>
<RECORD UI="4C6AAA65"></RECORD>
</root>');
-- INSERT INTO destination_table (ID, xml_record)
SELECT id
, c.query('.') AS xml_record
FROM #staging_tbl
CROSS APPLY xmldata.nodes('/root/RECORD') AS t(c);
Output
id
xml_record
1
<RECORD UI="F298AF1F" />
1
<RECORD UI="4C6AAA65" />
You can use the nodes() method to return a rowset of nodes in the xml document. This is the simplest example:
select node_table.xml_node_column.query('.') node
from xmldocument
cross apply xmldocument.nodes('/root/RECORD') node_table(xml_node_column)
https://learn.microsoft.com/en-us/sql/t-sql/xml/nodes-method-xml-data-type?view=sql-server-ver16

Querying XML tag in SQL server

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');

extract value from xml data column oracle sql

From the database there is a column which contains xml data in the format like:
<old_template_code> something </old_template_code><old_template_name> new
code</old_template_name><new_template_code>BEVA24M</new_template_code>
How can I extract the values from the xml and make a column for each of the different xml values? For example I would like to do something like:
select EXTRACTVALUE(table.column, table.column)
but the latter doesn't work for me.

Parsing multiple xml elements from an xmlstring in SQL server

My input xml string is
<users><id>p1</id><id>p2</id><id>p3</id></users>
i want to write an update query to update the column 'Request_Status' in my table if the id matches to the ones in the xml string
I tried this statement:
Update login set
Request_Status='A'
where
EmpId in
(
SELECT Pers.value('(id)[1]', 'nchar(10)') as 'ID'
FROM
#xmlUserId.nodes('/users') as EMP(Pers)
)
This only updates for ID p1 and not for the other 2.
I referred to this,
How to loop and parse xml parameter in sql server stored procedure
(P.S my first question here, so please excuse mistakes, unconventional elements in the post)
Try to change the XML selection query to be as follow :
SELECT Pers.value('self::*', 'nchar(10)') as 'ID'
FROM
#xmlUserId.nodes('/users/id') as EMP(Pers)
Above query should return all <id> element within <users> element
[SQL fiddle demo]

Querying by a value in XML in SQL Server 2005

Let's say I have a column in a table where the datatype is XML. I have a specific value I want query for in an xml tag that is unique(no repeating) in the XML. How do I do this?
Something like:
select * from MyTable
where XMLColumn.TagImLookingAt.Value = #QueryValue
Use:
WHERE xmlcolumn.value('(/path/to/tag)[1]', 'int') = #QueryValue
Change the data type to whatever is appropriate.
For more info, see the documentation - specifically the methods available when dealing with the XML data type...