Return XML document from SQL Server 2008 - sql-server

I have a template of an Excel XML document. Is there anyway I can build the XML document is SQL and have SQL Server return the XML document to me via stored proc.?
I am trying to see if SQL Server can not just return the data for the spreadsheet but the actual Excel XML document with the data in it.
Something similar to this.
http://msdn.microsoft.com/en-us/library/office/aa191724%28v=office.11%29.aspx
I want to build the XML is SQL.

Yes, you can use the FOR XML clause.
select * from table for xml raw
You can also use XML AUTO or XML EXPLICIT

Related

Azure XML support

I am looking at vendor page, and see contradicting information (top of each page with green or gray checkboxes) wheter XML column data type is supported in Azure SQL Server or not.
Could anybody with access to Azure SQL Server databases may tell me if XML is fully supported or not?
https://learn.microsoft.com/en-us/sql/connect/oledb/features/using-xml-data-types?view=sql-server-2017
https://learn.microsoft.com/en-us/sql/relational-databases/xml/xml-data-type-and-columns-sql-server?view=sql-server-2017
https://learn.microsoft.com/en-us/sql/t-sql/xml/xml-transact-sql?view=sql-server-2017
Just executed following script in Azure database, works fine:
create table x(t xml)
insert into x(t) values('<zz>aaaabbbb</zz>');
select t.value('/zz[1]','varchar(100)') from x;
Azure SQL database supports XML Data Type,xml (Transact-SQL) , but not supports XML data.
The documents which involve xml data all show don't support Azure SQL database.
I think XML is not fully supported in Azrue SQL Database.
But we can create XML data type column in Azure SQL database, and do what Piotr showed for you. Not all functions are supported in Azure.
Here is blog I think you can reference: How to load local XML File to Azure SQL Database?
There is a xml file and needs to be load into a table in an Azure SQL Database. The table has a column as xml type.
And it also gives you the answer maybe can give you some ideas about save XML fragments into fields.
Hope this helps.

Best way to move XML data from a SQL Server 2016 DB to Cosmos DB

Have a SQL Server 2016 table which contains ~10 GB of data. I have to migrate the XML data stored within that table in a XML column to Cosmos DB.
It's a one time job and post migration such data would be directly written into Cosmos DB (from an API written on C#).
What is the best way to migrate this? I am aware there is a Azure Cosmos DB Migration tool but not aware how to migrate the XML column using that. Also is there any other better solutions for this?
Convert the Xml data into JSON within SQL Server 2016. It shouldn't be a very hard task.. batch it out.
pseudo-Code:
DECLARE #xml XML = '<?xml version="1.0"?>
<header>
<rdms id="1">
<sqlserver>MSSQL1</sqlserver>
</rdms>
<rdms id="1">
<sqlserver>MSSQL2</sqlserver>
</rdms>
<rdms id="2">
<oracle11g>cc11</oracle11g>
</rdms>
</header>''';
SELECT x.value('#id', 'nvarchar(MAX)') AS rdms
,x.value('sqlserver[1]', 'nvarchar(MAX)') AS [Server]
,x.value('oracle11g[1]', 'nvarchar(MAX)') AS [Oracle]
FROM #xml.nodes('/header/rdms') AS a(x)
FOR JSON AUTO
Then use the Cosmosdb Migration tool to load the JSON data into CosmosDB...
Read on the below..
https://learn.microsoft.com/en-us/sql/t-sql/xml/nodes-method-xml-data-type

Convert multiple Images to varbinary in SQL Server from vb.net

I just want to know the possible ways to convert like 2 or 3 images into a varbinary and save it in a single column named images in SQL Server. And retrieve it like the way to retrieve a single image.
If you simply need to store file data in the database, and the files are accessible to the SQL Server, then you can get SQL Server to do the heavy lifting using OPENROWSET:
SELECT o.BulkColumn
FROM OPENROWSET(BULK N'C:\Myfilepath.jpg', SINGLE_BLOB) o
SQL Server will return a single-column, single-row dataset that will contain the binary data for your file.
You can use the above statement stand-alone, in a stored procedure or as part of an INSERT statement.

extract xml from image column in sql server

I have some xml documents stored as an image data type in a sql server 2000 table. How can I extract the data back to xml and save to a file, or at least see the xml contents in the query window, using just the sql tools?
Look, here
Essentially you want to extract text from varbinary.

XML in SQL Server 2005? Better than JSON in Varchar?

What are the benefits of storing XML in SQL Server over storing JSON in a varchar field?
Any tutorial available for how to use the XML data type effectively?
Do I need to provide the dtd / xml schema somehow? I've heard it is optional, right?
Thank you.
UPDATE: here's the answer to the last part of the Q.
XML schema information is used in
storage and query optimizations. Typed
XML instances contain typed values in
the internal, binary representation as
well as in XML indexes. This provides
efficient processing of typed XML
data.
quoted from: http://msdn.microsoft.com/en-us/library/ms345117(SQL.90).aspx
XML in SQL Server 2005 and up allows you to directly manipulate the XML stored inside the database table using XQuery - you can't really do that with JSON in a VARCHAR field.
Check out XML Support in Microsoft SQL Server 2005 and Fundamentals of the XML Datatype in SQL Server 2005 for more info and more background.
Also, the XML stored in a XML column in SQL Server is "tokenized" and pre-parsed - it's not just stored as plain text. And you can even put indices on your XML - on its nodes, its values - whatever you need.
Storing it as XML allows you to leverage the SQL XML support: XPATH, XQUERY, XML Indexes and such. These allow for efficient search and manipulation of the content. I recommend you read XML Best Practices for Microsoft SQL Server 2005
JSON content in VARCHAR would be opaque to searches and manipulation.
XML can be indexed for more performant querying.
Data can be extracted from XML data usng XPath.
A schema can be provided to constrain the XML to a secifiation but this is optional.
Client libraries understan the XML data type and can send/receive it more carefully/easily.
None of the above is available for JSON stored in varchar.
you can query for individual xml property values if you use the xml data type. I don't believe the same functionality is available for json:
http://msdn.microsoft.com/en-us/library/ms191474.aspx
I don't believe that the xsd is required as I've used the query feature without having that defined before
Storing XML in SQL 2005 is great. You can put the whole XML file in a single field, then run SELECT commands to pull out certain attributes and elements. Putting a XSD is not neccesary. I don't believe it has any JSON support even in 2008 although I could be wrong. Here's a good starter article on it.

Resources