XML in SQL Server 2005? Better than JSON in Varchar? - sql-server

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.

Related

MS Azure DB: cannot find data type XML & Incorrect syntax near ' Identity'

I was trying to change the data type into XML in order to split one column into multiple columns. However, when I ran the syntax below, an error: cannot find data type XML was showed. I searched some of the answers online. It seems Azure is supported for XML. Is there a way to solve it?
cast('<m>'+replace(Employee_Name,#delimiter,'</m><m>')+'</m>' as XML)
Besides, I found that IDENTITY(1,1) is not supported either.
With SQL-Server 2016 there is native support for this: STRING_SPLIT()-function. But - according to the linked doumentation - this seems not to be offered for Azure Data Warehouse...
The string splitting via XML needs the XML-DataType, since you will need .nodes() and .value() to retrieve the values. According to this documentation this is supported with Azure Database, don't know of restriction with the Data Warehouse version...
There are many examples for string splitting functions using loops or recursive CTEs. This article compares some of them...
This error occurs when you try to run the query casting to XML while logged into a Microsoft Azure SQL Data Warehouse. Azure SQL Data Warehouse does not support the XML datatype. You can confirm your version with the following sql:
select ##version
https://learn.microsoft.com/en-us/azure/sql-data-warehouse/sql-data-warehouse-tables-data-types

SQL Server: XPATH Query failing

I am working in SQL Server Management Studio on a stored procedure. There is a column containing XML that I am trying to access with XPATH but I get this error:
Parsing XML with internal subset DTDs not allowed. Use CONVERT with style option 2 to enable limited internal subset DTD support`
I read the docs on convert and importing xml but no luck.
To be clear, I have XML stored in a column in a table, and now want to access it with xpath after querying it into memory in a stored procedure. Anyone know what I should do?
Your most likely getting this because somewhere inside of your XML you are using a document definition such as this: (MSDN example of DTD)
<!DOCTYPE DOC [<!ATTLIST elem1 attr1 CDATA "defVal1">]><elem1>January</elem1>
Microsoft blocks this by default as it creates a potential security hole. The convert with style option 2 message is telling you that you can enable partial support for DTD in order to process this XML through use of the CONVERT function as it relates to XML.
I don't have personal experience with doing that so I can't offer more than the link to MSDN.
The only other option is to enable full DTD support on the server which Microsoft does not recommend.

Import arbitrary XML to SQL Server

Is that possible to parse and import an arbitrary XML file to SQL Server tables using C#?
The XML file can be highly hierarchical.
I have 1 Gb XML-file, I do not know anything about it.
How can I determine what tables it contains, create them at SQL Server and bulk load the data to SQL Server?
Is that possible?
No.
XML and SQL are very, very different things. It is possible to take SQL data an save it into XML, and such SQL-like XML can be converted back to an SQL database, but arbitrary XML that did not originate in an SQL-like system is not likely to be easily convertible to an SQL database schema in any meaningful way.
You could work out an SQL schema that represents generic XML in a key-value type design but it would not be an SQL schema in the traditional meaning of the word.

Using SQL Server "FOR XML": Convert Result Datatype to Text/varchar/string whatever?

I'm struggling with this nearly one week now, and still haven't found any solution -.-
My goal is to receive an XML from a SQL Server by querying with the "FOR XML" statement, taking this XML as a string/stream to forward to a XSLT Transformer and presenting the result as HTML.
The poor thing about it is the webserver environment I should use: IBM Domino 8.5.2, which usually uses Lotus Script and Lotus Notes Databases to generate websites.
But now the performance of Notes databases have reached their dead end, when talking about big databases like our website hit statistics which store millions of hits as datasets. So we decided to migrate to an mssql2008 server that can provide custom filtered results more quickly.
I did actually manage to establish a connection to SQL Server, submit a query and have the result as xml. But ... now? :) It seems that SQL Server provides his "for xml"-queried results as a kind of own weird datatype of binary encoded chars that make no sense when printing them or saving to a file. And Lotus Script (pretty much VBScript) doesn't seem to have a possibility to handle this xml datatype.
I tried every way i could find getting the result - Lotus built in ODBC class, LCConnection via OleDB, ADO Connection via OleDB... but every try ends in a bunch of unreadable data.
I could imagine a webservice between domino and SQL Server, preparing the data as string for my domino script, but that's unnecessary overhead i'd like to avoid.
Can you tell in the sql statement what datatype you want the resulting xml to be? just like cast()/convert() for fields but for the whole xml result? So that i could just read the result as text/varchar field?
Thx, Chris
I knew I'd done something like this before, and I think this is what you're after:
SELECT CAST((SELECT [Columns] FROM [Tables] FOR XML PATH('')) AS VARCHAR(MAX)) AS XmlData
That will spit out whatever you've generated as XML as text data that Lotus Notes may well accept.
Edit: You could change FOR XML PATH('') to FOR XML AUTO if you need the XML to be generated in another style.

What are the advantages of having typed XML columns in SQL server?

From what I've read an XML column in SQL server can be typed (having a schema) or untyped. What are the advantages of using typed XML?
Is it just that the XML is validated?
First is validation.
Second you can formulate XML queries based on the XSD and know that the XML content is compatible with your queries.
It also helps with errors and debugging - see Silent XQuery Failures

Resources