I have a stored procedure that returns a single recordset. is it possible to convert it into xml like below
<employee>
<firstname>x</name>
<lastname>x</name>
<dob>x</dob>
</employee>
using some simple options in sql 2000?
SELECT *
FROM MyTable
WHERE 1 = 1
FOR XML AUTO
Instead of the AUTO, you can use RAW, EXPLICIT or PATH (see here http://msdn.microsoft.com/en-us/library/ms178107(v=SQL.90).aspx), but AUTO generally does what you want.
I can only find notes on MSDN for SQL 2008 and 2005 but I'm sure I've used this on 2000 before...
Thanks Skrealin
the query below works
select * from Mytable employee for xml auto , elements
Related
I have a massive view and using a SQL query I want to produce output as XML.
So if I query SQL it gives me back XML.
I have no idea how to do it so please point me in right direction.
SADLY it's for SQL Server 2000
Use FOR XML RAW It returns XML.
Like:
SELECT * FROM table_name FOR XML RAW;
I store user preferences in an XML column which looks like this:
<tags>
<user>
<tag name="AB"/>
</user>
</tags/>
When I use the query below,
select *
from company
where CAST(tags.query('tags/user/tag[fn:contains(#name,"Ab")]') as varchar(2000) ) <> ''
it does not return any results, the attribute value is in different case then one in the xml column.
Any ideas on making the search by attribute name case insensitive?
Thanks
With SQL Server 2008 you can make use of the lower-case and upper-case functions like so:
select * from company where CAST(tags.query('tags/user/tag[fn:contains(lower-case(#name),"ab")]') as varchar(2000) ) <>''
see:
New XQuery functions introduced in SQL Server 2008: upper-case() and lower-case()
lower-case Function (XQuery) (MSDN)
Is there a way to predermine the length of returned XML Data from SQL Server.
Ex:
SELECT *
FROM Products
FOR XML RAW, ROOT('products')
I am using SQL Server 2005.
SELECT DATALENGTH (
(SELECT *
FROM products
FOR XML RAW, ROOT('products')
)
)
It will give to you size in bytes.
for details, see here:
http://msdn.microsoft.com/en-us/library/ms173486(v=SQL.90).aspx
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.
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...