在 Unicode 和非 Unicode 错误之间进行 SSIS 转换

我有一个 ssis 包,其中我使用 OLEDB 源链接到 SQLServer2005表。除了日期列之外的所有列都是 NVARCHAR (255)。我使用一个 Excel 目的地并使用一个 SQL 语句在 Excel 工作簿中创建工作表,SQL 在 Excel 连接管理器中(实际上是一个创建表的语句,它创建了一个工作表) ,并且是从数据库中的列的映射派生出来的。

不管我做了什么,我总是在源和目标之间出现 Unicode-> non-unicode 转换错误。尝试在 S > D 之间转换为 string [ DT _ STR ] ,删除它,将 SQL Table VARCHAR 更改为 NVARCHAR,但仍然得到这个 flippin 错误。

因为我使用 SQL 语句在 Excel 中创建表,所以我没有看到任何实际预定义 Excel 表中列的数据类型的方法。我猜想这将是一个默认的元数据,但我不知道。

因此,在 SQL 表目标和使用 SSIS SQL 语句创建 Excel 表之间,如何才能停止出现这个错误?

我的错误是:

数据流任务错误[ OLEDBSource [1]] : 列“ MyColumn”无法在 unicode 和非 unicode 字符串数据类型之间进行转换。

以及所有 nvarchar 列。

感谢你的帮助

谢谢

安德鲁

393611 次浏览

Add Data Conversion transformations to convert string columns from non-Unicode (DT_STR) to Unicode (DT_WSTR) strings.

You need to do this for all the string columns...

  1. First, add a data conversion block into your data flow diagram.

  2. Open the data conversion block and tick the column for which the error is showing. Below change its data type to unicode string(DT_WSTR) or whatever datatype is expected and save.

  3. Go to the destination block. Go to mapping in it and map the newly created element to its corresponding address and save.

  4. Right click your project in the solution explorer.select properties. Select configuration properties and select debugging in it. In this, set the Run64BitRunTime option to false (as excel does not handle the 64 bit application very well).

Below Steps worked for me:

  1. right click on source task.

  2. click on "Show Advanced editor".

advanced edit option for source task in ssis

  1. Go to "Input and Output Properties" tab.

  1. select the output column for which you are getting the error.

  2. Its data type will be "String[DT_STR]".

  3. Change that data type to "Unicode String[DT_WSTR]".

Changing the data type to unicode string

  1. save and close.

Hope this helps!

On the above example I kept losing the values, I think that delaying the Validation will allow the new data types to be saved as part of the meta data.

On the connection Manager for 'Excel Connection Manager' set the Delay Validation to False from the Properties.

Then on the data flow Destination task for Excel set the ValidationExternalMetaData to False, again from the properties.

This will now allow you to right click on the Excel Destination Task and go to Advanced Editor for Excel Destination --> far right tab - Input and Output Properties. In the External Columns folder section you will be able to now change the Data Types and Length values of the problematic columns and this can now be saved.

Good Luck!

I have been having the same issue and tried everything written here but it was still giving me the same error. Turned out to be NULL value in the column which I was trying to convert.

Removing the NULL value solved my issue.

Cheers, Ahmed

I experienced this condition when I had installed Oracle version 12 client 32 bit client connected to an Oracle 12 Server running on windows. Although both of Oracle-source and SqlServer-destination are NOT Unicode, I kept getting this message, as if the oracle columns were Unicode. I solved the problem inserting a data conversion box, and selecting type DT-STR (not unicode) for varchar2 fields and DT-WSTR (unicode) for numeric fields, then I've dropped the 'COPY OF' from the output field name. Note that I kept getting the error because I had connected the source box arrow with the conversion box BEFORE setting the convertion types. So I had to switch source box and this cleaned all the errors in the destination box.

Instead of adding an earlier suggested Data Conversion you can cast the nvarchar column to a varchar column. This prevents you from having an unnecessary step and has a higher performance then the alternative.

In the select of your SQL statement replace date with CAST(date AS varchar([size])). For some reason this does not yet change the output data type. To do this do the following:

  1. Right click your OLE DB Source step and open the advanced editor.
  2. Go to Input and Output Properties
  3. Select Output Columns
  4. Select your column
  5. Under Data Type Properties change DataType to string [DT_STR]
  6. Change Length to the length you specified in your CAST statement

After doing this your source data will be output as a varchar and your error will disappear.

Source

The missing piece here is Data Conversion object. It should be in between OLE DB Source and Destination object.

enter image description here

When creating table in SQL Server make your table columns NVARCHAR instead of VARCHAR.

No-one seems to mention this but, converting varchar to nvarchar in the source query also solves the issue.

I think people are missing this. In my case I had 100 character columns to convert between Oracle and MS Sql. All this stuff about Data Conversion and Advanced Editor is incredibly tedious if you have a 100 separate character columns to assign. Plus SSIS being SSIS, it will sometimes reset all your 100 advanced editor changes even if you set VALIDATEEXTERNALMETADATA to false, incredibly obnoxious. I wouldn't mind doing the Data Conversion if there was some value to it but 20 years ago ETL tools used to take oracle character to ms sql characters without fussing. What Bakalolo and Zafer say is the answer if you have a lot of character columns and you can live with nvarchar, just declare all your output ms sql columns (nvarchar) and your data task will automatically assign your oracle fields into ms sql fields with no manual overrides. I have also found that the new Oracle Source (2021) doesn't complain about a unicode conversion to varchar in ms sql. A colleague just told me that the ssis wizard (it may be only in vs 2019+) to assign oracle character to ms sql varchar will do the assignments automatically with no override, but I haven't tried that personally.

2022 update - I think this is just vs 2019 created packages and later. An ado.net task reading a varchar ms sql table going to oledb (and ado.net I think) ms sql varchar will throw the unicode error. If you switch the input task to oledb reading ms sql varchar table you won't have to do the advanced editor overrides for the varchar fields. If you don't want to do advanced editor overrides (who does?) try different tasks and more oledb tasks.

I just encounter same issue, I solve it in my SQL request : using convert directly

CONVERT(NVARCHAR(50),'') AS MyVarName

I need to put an empty (or fix size string) into excel file. Converting force type of MyVarName from DT-STR to DT-WSTR (unicode)