Sql-Server

SSMS Oracle 使用 OpenQuery over LinkedServer 導入(OLEDB 的 Oracle 提供程序)-XML 解析:無法切換編碼

  • March 6, 2013

我正在嘗試將數據從 OracleDB 遷移到 SQL Server Express 數據庫。我通過打開查詢連結伺服器(基於 Oracle Provider for OLEDB 的數據提供程序)來做到這一點。對於包含簡單數據類型的表,一切正常。但是當我到達包含 XML 列的表時,它返回一個錯誤:

Msg 9402, Level 16, State 1, Line 24
XML parsing: line 1, character 60, unable to switch the encoding

為此我…

INSERT INTO [datab].[dbo].[tr] (ID, CREATED, UPDATED, COMPANY, INTERNAL_MESSAGE, EXCHANGE_MESSAGE) 
SELECT * FROM OPENQUERY(DATAB_ORACLE, 
'SELECT id, CAST(created AS DATE) AS created, CAST(updated AS DATE) AS updated, company, XMLSerialize(DOCUMENT internal_message AS CLOB) AS internal_message, exchange_message FROM datab.tra')

我不知道 Oracle 列有什麼編碼,MSSQL 列也沒有(可能是預設值)。

有什麼建議?

LE:請注意,依賴於 Oracle CONVERT() 函式(具有相應的編碼類型)的解決方案似乎不起作用。

我找到了一個解決方案,讓大家知道:

假設我們將 xml 欄位放入 varchar(max) 類型的變數中,然後將其轉換為 XML 類型的變數(但不是通過使用 CONVERT 函式,而是通過簡單的賦值),最後新的值創建的 XML 變數將包含在插入查詢中,方法是將其轉換回 varchar(max)。

DECLARE @id numeric(19, 0)
DECLARE @xmlmsg varchar(max)
SELECT @id = id, @xmlmsg = internal_message FROM 
   OPENQUERY(DATAB_ORACLE, 'SELECT id, CAST(created AS DATE) AS created, CAST(updated AS DATE) AS updated, company, 
       XMLSerialize(DOCUMENT internal_message AS CLOB) AS internal_message, exchange_message FROM datab.tra')

DECLARE @xml xml
SET @xml = @xmlmsg

DECLARE @tsql varchar(8000) 
SELECT @tsql = 'INSERT INTO [datab].[dbo].[tr] (ID, CREATED, UPDATED, COMPANY, INTERNAL_MESSAGE, EXCHANGE_MESSAGE) 
   SELECT id, created, updated, company, ''' + CONVERT(varchar(max), @xml) + ''', exchange_message FROM OPENQUERY(DATAB_ORACLE, 
   ''SELECT id, CAST(created AS DATE) AS created, CAST(updated AS DATE) AS updated, company, XMLSerialize(DOCUMENT internal_message AS CLOB) AS internal_message, 
   exchange_message FROM datab.tra where id = ' + CONVERT(VARCHAR(max), @id) + ''')'

EXEC (@tsql)

您應該注意,在這種情況下,在插入行後看到 XML 文件聲明(例如“”)失去是正常的,因為 SQL Server 刪除了該部分(按設計)。在此處了解更多資訊:http: //msdn.microsoft.com/en-us/library/ms187107 (v=sql.90).aspx

引用自:https://dba.stackexchange.com/questions/35905