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

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.

Related

[Excel Destination [28]] Error: An error occurred while setting up a binding for the column. The binding status was "DT_NTEXT"

I'm working on ssis package which exports data from SQL Server to Excel. I had a problem converting non-unicode to unicode string data types. So I created a derived Column task and converted to Unicode string [DT_WSTR] 4 columns which have a type Varchar(40) in SQL Server table. It worked with these columns. But I also have a Description column of type varchar(max) and I tried to convert it to Unicode text stream [DT_NTEXT]. It did not work.
If your source is SQL Server (as you said), you can convert it directly in your SQL Query
SELECT
CONVERT(NVARCHAR(40), 'att1')
,CONVERT(NTEXT, 'att2')
Convert your VARCHAR into NVARCHAR
Convert your TEXT into NTEXT
it's faster.
P.S. To test it (Do not forget to delete or reset your previous OLE DB Input component) -> It will be forced to reevaluate your datatype
Does it help you?
The only thing that worked was to cast a Description column in Stored Procedure as varchar(1000). I checked the max length of this field and it was about 300 characters. So I made it varchar(1000) and in Derived column Unicode string [DT_WSTR]. This was a workaround, but I still want to know how to make it in ssis package without converting data type in Stored Procedure.

What is the simplest way to convert table Rows into XML and viceversa

I need to develop a sql server procedure which will convert table rows into xml and will transfer it to another stored procedure(Different server) through link server. On that server I have to take this xml data as input and again convert it into table rows. What will be the simplest and time efficient way to do this.
It is not possible to use the XML datatype as a parameter in stored procedure on a linked server. You have to use nvarchar(max) and convert to XML in the stored procedure.
To create the XML you should use FOR XML (SQL Server). RAW and AUTO is easy and if you need more control you could use PATH. I would stay away from EXPLICIT.
To shred the XML on the receiving side you should use nodes() Method (xml Data Type) and value() Method (xml Data Type).
Given that you are using SQL Server, you need only append FOR XML to your query.

SQL xml output parameter throwing error?

I have a SQL Server stored procedure that has an output param of type xml. In my coldfusion code, I'm not sure which cfsqltype to use for an xml variable. I tried using a cf_sql_varchar in the following code:
<cfprocparam cfsqltype="cf_sql_varchar" type="out" variable="result">
But I get the following error:
Implicit conversion from data type xml to varchar is not allowed. Use the CONVERT function to run this query.
Is there any way to deal with the xml output param in coldfusion without having to change the stored procedure to return a varchar instead of xml?
Unfortunately the answer to your question is no. The driver does not support reading stored procedure output of the XML data type. If you have to use the XML data type as an output parameter, you must first convert the XML data type to a VARCHAR or LONGVARCHAR in the stored procedure before it can be read.

XML data in ntext to xml data type

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.

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