Getting SQL view output as xml ( is it even possible ? ) - sql

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;

Related

MSSQL query with too many paramers in IN clause is very slow

I'm having a query with IN clause with 2000 parameters of varchar2 type. I have applied required index as per execution plan.
It is working very slowly. I'm looking for an alternative solution. One way I found is creating a temporary table with these value and fetch using join. Is there any way other than this? I'm using spring data JPA/Criteria for queries in Java. Thanks in advance.
Bulk Load the values into a temprary table, either using SqlServerBulkCopy directly or using the useBulkCopyForBatchInsert.
Use a Table-Valued Parameter
Or (SQL 2016+) send the values as a JSON array. Just create a long string of the form
["Value1","Value2","Value3"]
and pass it as a parameter to a query like:
select *
from SomeTable
where SomeColumn in ( select value from openjson(#jsonValues) with (value varchar(200) '$') )
Or do the same thing with XML.
You can also use string_split, but that's also only available on SQL 2016+ and JSON is more robust.

table results to xml in oracle

Experts, I want to send some results via email in the form of table, for which i need to convert the results into xml format. I want to store the results in a variable and attach to the xml body. The xml result gives me in the form of rows and i am unable to capture the xml results in a single row and convert that into varchar2 so that i can attach the variable.
Need help... I am using Oracle Sql Developer 11g.
Below is the script i am using
select (XMLElement("tr"
,XMLFOREST(d.tablespace_name as "td",
d.file_name as "td", d.bytes/1024/1024 as "td",
h.CREATION_TIME as "td",
d.blocks as "td")))
from dba_data_files d
inner join v$datafile_header h
on d.TABLESPACE_NAME = h.TABLESPACE_NAME
order by d.TABLESPACE_NAME;
If I understood the question correctly - please have a look at XMLAGG:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions215.htm
This function allows creating one xml from multiple records.

SQL xpath for level 1 node values

I'm trying to create a stored procedure in sql 2008 to select the id values from some xml like this.
DECLARE #idPolygonXML XML
SET #idPolygonXML =
'<polygons>
<id>35</id>
<id>36</id>
<id>37</id>
<id>38</id>
<id>39</id>
<id>40</id>
</polygons>'
I can get the id for a specific index but I need all of them and I keep getting 'value() requires a singleton'.
Does anyone know how I can get all these values without changing the xml?
Take a look at this: http://msdn.microsoft.com/en-us/library/ms188282.aspx
This isn't tested, but your query would look something like this
SELECT T2.ID.query('.')
CROSS APPLY #idPolygonXML.nodes('/polygons/id') as T2(ID)

How to append data from one table in another where one column is xml with sql?

Actually both tables are the same, and I just need to merge data. Problem is that one column is defined with XML shema, which is same in both tables, and for my query I am getting this error from sql server studio:
"Implicit conversion between XML types constrained by different XML schema collections is not allowed. Use the CONVERT function to run this query."
Help me writedown this query.
I have something like this:
INSERT INTO table1
SELECT * FROM table2
WHERE id NOT IN (select id from table1);
Without more info on your table structure and the xml schemas I'm not sure how much assistance I can be. That said there's an article that discusses this exact problem here
http://sqlblogcasts.com/blogs/martinbell/archive/2010/11/08/Using-XML-Schemas.aspx
And his example of using the convert statement to overcome exactly this problem is as follows.
INSERT INTO [dbo].[Test_ProductModel_Content]( [CatalogDescription] )
SELECT CONVERT(XML, [CatalogDescription] )
FROM AdventureWorks2008.Production.ProductModel
WHERE [CatalogDescription] IS NOT NULL ;
GO
Hope that helps, if not post more information and I'm sure someone can help you out.

xml generation using sql 2000

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