XML data in ntext to xml data type - sql

I'm a rookie when it comes to XML data.
I currently have a database with xml data being stored in a ntext column and this database appears to be taking up too much space. I'm trying to improve the way the data is stored, to reduce the overall size of the DB.
The way I see it, I have two options:
nvarchar(max)
xml
I will need to test the two options above by importing some data into those columns.
The problem I am having is, the XML data in the ntext column is currently stored as utf-8. In order for me to import it into the XML data type column, I will need to CAST/CONVERT the data to UTF-16?
Is this right?

Storing you data as datatype XML has two major benefits:
since it's stored as a native XML, you can do things like XQuery on top of it
since it's stored as XML it's stored in an optimized (tokenized) way, taking up less space than what the equivalent nvarchar(max) column would use up.
To convert your existing NTEXT column: just do a CAST on it.
What results do you get frmo this:
SELECT
id, ntextColumn, CAST(ntextColumn AS XML)
FROM
dbo.YourTable
I am almost sure this will work - just like that. SQL Server doesn't support UTF-8 - so your data even in the ntext column is most likely not really stored as UTF-8 (it's already been converted to SQL Server's Unicode - UCS-2/UTF-16) so I don't see any issue with converting this to datatype XML, really.

Related

How to get a XML with very large data into a varchar(MAX) variable in MS SQL

In MS SQL, I have large XML data which i want to cast and store it in a varchar(MAX) variable but its not working. when i try to capture the data in varchar variable it is missing some part of the data.I want to cast the data because want to do some string manipulations which i cant do it if the data is in XML format.Please suggest some alternative solutions.

Server 2012 R2 SQL Database Change Column From ntext to nvarchar

I have a predefined SQL data base that we have to work correctly with a reporting software we have purchased. When ever we pull a column of data with the reporting software we get system.indexoutofrangeexception error. On the first table we replaced all Semi Colons ';' with space within the data and this corrected the issue. This column does not have any other special characters within the data only semi colons.
However the data in the second column we need to query contains all different kinds of characters that are probably invalid. The column type is ntext and would like to either change the data directly in the sql database everytime there is a new entry or would changing the format to nvarchar(max) or nvarchar(1024) be suffice?
Thanks for the support I am beyond green at sql.
Your problem is most likely not related to the datatype in your database but the data itself.
Your reporting software seems to have specific requirements that your data does not meet.

How to parse a text attribute into a xml atribute in SQL server 2005?

I have a table which contains an attribute called custom_fields that stores a well-formed xml:
<Root>
...
<TotalMontoSoles></TotalMontoSoles>
...
</Root>
But this attribute is not stored as a xml data type, instead it's stored as a text. What I need to do is set the TotalMontoSoles value and I was trying to accomplish that by using the modify method from XML-DML but I keep getting a
Error SQL: Explicit conversion from data type xml to text is not allowed.
error when I try to cast the column into a xml type:
DECLARE #custom_fields xml
SET #custom_fields = (SELECT CAST(custom_fields as XML) FROM UPLOAD_HEADER_TEMPORAL
#custom_fields.modify('...')
What am I doing wrong? Is there any other way I could accomplish this?
UPDATE:
Maybe it's important to point out that what I'm trying to do here is to create a procedure and I'm getting this error during compilation time.
Text column data types cannot be converted (cast) to XML. You can (should) use one of the varchar types though. Microsoft will be removing the text (and image) data type at some point in the future.
http://msdn.microsoft.com/en-us/library/ms187993%28v=sql.90%29.aspx
ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

XML data type in SQL Server?

I have some unstructured data that describe setting for each devices. For e.g. for Device 1:
<ChartSetting xmlns="ChartSetting1">
<ChartSizeX>132.6</ChartSizeX>
<ChartSizeY>132.6</ChartSizeY>
<ChartType>Native</ChartType>
<BarSizeX>90</BarSizeX>
<BarSizeY>6</BarSizeY>
<DistToBar>34.8</DistToBar>
<DistToFirstLineY>17.5</DistToFirstLineY>
<MarkerDistance>120</MarkerDistance>
<DistToFirstField>18.5</DistToFirstField>
<PatchSizeX>7.5</PatchSizeX>
<PatchSizeY>9</PatchSizeY>
</ChartSetting>
However for Device 2 setting is different
<ChartSetting xmlns="ChartSetting2">
<PatchGap>1</PatchGap>
<PatchSize>5</PatchSize>
</ChartSetting>
xml data is not used for query purpose, it will be passed to the .net application that eventually send it to the devicedriver (through C++ code)
We only have four types device. So possibility of different settings is limited.
Can this kind of data be stored as typed XML that adhere to some schema? Storing as varchar will be nightmare if there are invalid settings stored.
I know xml schema collection can have multiple schema but can it confirms to only one schema.
Is there an easy way to create a schema?
Should I be using untyped XML instead?
You could create a DTD for the four different device types and validate your XML fragments with those DTDs, but I'd suggest you do that processing OUTSIDE of SQL Server.
If you are not going to query the XML, there isn't any reason to store it as the XML data type.
Answers to your questions:
Typed XML: you'll have to use a specific type per column (see http://msdn.microsoft.com/en-us/library/ms184277.aspx)
Create a
schema - check out (http://msdn.microsoft.com/en-us/library/ms176009.aspx) and try
something like XMLSpy if it gets too complex. Your snippets looked
small so I should think you can manage it with Notepad/++
I'd use untyped XML and validate it when it gets stored and/or retrieved
1.I think it's possible, if your schema collection contains 4 different schemas, the related column can contain xml that satisfies one of the schemas in collection.
2.I believe the easiest way is to let sql server create a schema for you (first create tables ChartSetting1,ChartSetting2 with desired columns - it's not necessarily, you can create SELECT that returns all columns, but using tables is easier), then
DECLARE #mySchema NVARCHAR(MAX);
SET #mySchema = N'';
SET #mySchema =#mySchema +
(
SELECT * FROM ChartSetting1
FOR XML AUTO, ELEMENTS, XMLSCHEMA('ChartSetting1')
);
SET #mySchema =#mySchema +
(
SELECT * FROM ChartSetting2
FOR XML AUTO, ELEMENTS, XMLSCHEMA('ChartSetting2')
);
CREATE XML SCHEMA COLLECTION [name] AS #mySchema;
-- we don't need these tables , drop them
DROP TABLE ChartSetting1,ChartSetting2
3.It depends. I'd say if you need constrain the XML data beyond schema validation, then it makes sense to use untyped XML. If schema validation covers all aspects of data validation, why not use it?
I think it is impossible create schema that conforms both XML.
Many XML editors can create XSD from XML, e.g.: Visual Studio
Typed XML vs. Untyped XML
XML shouldent really be stored in a databse as XML. XML is more of a transport medium than data. As you say its big and hard to query. You should store your data as data (varchar int ect) then use PATH MODE to return it back to you in the format you want

Data Type Mapping

I need to store XML data to database(MS SQL Server). The data type defined in the column is text.
I need to know the the equalent datatype for text. I have tried with adLongVarChar but it does not works. Also I tried with adLongVarWChar(nText). But both are not working.
Need help.
Thanks.
In case your are using SQL Server 2005 or higher then you might prefer going for the XML data type. Read more of it here http://www.codeproject.com/KB/database/XMLdDataType.aspx
Also going forward avoid using ntext and text data types as they would be removed from future versions of SQL Server. Instead go for nvarchar(max) or varchar(max). Read on this here http://msdn.microsoft.com/en-us/library/ms187993.aspx
cheers
TSQL datatype for a string is varchar or nvarchar (unicode). To specify the length of the string, varchar(50).
Also note there is an XML datatype in SS 2008 (2005?).