1

I have a problem with creating an XML file from inside a SQL PROCEDURE. The XMLs get created and placed in the correct folder. Although when I open the file in XMLSpy, it says the following error:

Your file contains 3 character(s) that should not be present in a file using the Unicode UTF-8 encoding... The offending characters are è (0xE8), ü (0xFC), é (0xE9)

When I open the XML file in Notepad++ and check for encoding (via menu Encoding on top), it says it is in ANSI and not in UTF-8.

So my question is: How do I get it to turn into UTF-8? Any suggestions? The code of my procedure can be found below:

@File        VARCHAR(2000) // parameter
AS 

BEGIN 
DECLARE @OLE            INT 
DECLARE @FileID         INT 

EXECUTE sp_OACreate 'Scripting.FileSystemObject', @OLE OUT 

EXECUTE sp_OAMethod @OLE, 'OpenTextFile', @FileID OUT, @File, 8, 1 

DECLARE @Text XML
SET @Text = (SELECT TOP(1) [xml] from VW_WARP_LEVERANCIERS_XML)     

EXECUTE sp_OAMethod @FileID, 'WriteLine', Null, @Text

EXECUTE sp_OADestroy @FileID 
EXECUTE sp_OADestroy @OLE 

END

EDIT 1

Changed my code, exporting to ASCII instead of Unicode (as suggested below by Wolf), but this didn't help too. But here is that part of code:

DECLARE @OLE            INT 
DECLARE @FileID         INT

EXECUTE sp_OACreate 'Scripting.FileSystemObject', @OLE OUT 

EXECUTE sp_OAMethod @OLE, 'CreateTextFile', @FileID  OUT, @File, 2, False

DECLARE @Text XML
SET @Text = (SELECT TOP(1) [xml] from VW_WARP_LEVERANCIERS_XML)     

EXECUTE sp_OAMethod  @FileID , 'Write', Null, @Text
EXECUTE sp_OAMethod  @FileID , 'Close'


EXECUTE sp_OADestroy @FileID 
EXECUTE sp_OADestroy @OLE 

END 

2 Answers 2

2

I found a solution via another way. (don't remember which website I found it) See code below. This code will export my file in UTF-8-BOM encoding.

@File        VARCHAR(2000)
AS 

BEGIN 

DECLARE @OLE            INT 

EXECUTE sp_OACreate 'ADODB.Stream',  @OLE OUTPUT

DECLARE @Text XML
SET @Text = (SELECT TOP(1) [xml] from VW_WARP_LEVERANCIERS_XML)     
DECLARE @Converted NVARCHAR(MAX)
SET @Converted = CONVERT(nvarchar(MAX), @Text)

EXECUTE sp_OASetProperty             @OLE,    'Type',             2                           --1 = binary, 2 = text
EXECUTE sp_OASetProperty             @OLE,    'Mode',             3                           --0 = not set, 1 read, 2 write, 3 read/write
EXECUTE sp_OASetProperty             @OLE,    'Charset',          'UTF-8'                     --'ISO-8859-1'
EXECUTE sp_OASetProperty             @OLE,    'LineSeparator',    'adLF'
EXECUTE sp_OAMethod                  @OLE,    'Open'  
EXECUTE sp_OAMethod                  @OLE,    'WriteText',        NULL,       @Converted      --text method

--Commit data and close text stream
EXECUTE sp_OAMethod                  @OLE,    'SaveToFile',       NULL,       @File, 2   --1 = notexist 2 = overwrite
EXECUTE sp_OAMethod                  @OLE,    'Close'
EXECUTE sp_OADestroy                 @OLE

EXECUTE sp_OADestroy @OLE 

END 
Sign up to request clarification or add additional context in comments.

Comments

0

Generally, SQL Server does not support UTF-8 by itself. However, I'm not sure about FSO, maybe it does.

The OpenTextFile documentation states that you can determine the file format, i.e. ASCII or Unicode. Try:

EXECUTE sp_OAMethod @OLE, 'OpenTextFile', @FileID OUT, @File, 8, 0;

This should give you an ASCII file format.

1 Comment

Thanks for the suggestion! Writing to ASCII gives me the same error it seems. I did it a bit different though now to write to ASCII, since just adding "0" instead of "1" didn't help me (it didn't export anything a file, without errors). The code to write to ascii that did work is shown above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.