【问题标题】:Overflowing numerical data on SSIS from CSV fileCSV 文件中 SSIS 上的数值数据溢出
【发布时间】:2020-11-23 20:43:19
【问题描述】:

因此,我需要将 CSV 文件中的数据加载到 SQL Server 中,但我遇到了一个似乎无法克服的问题。问题是,CSV 上的一些数据点非常精确,有 38 个十进制数字 + 一个或两个整数(例如 89.95849436456480407115801673479014153261),我似乎无法加载它们而不溢出数据。数据以文本形式出现,我尝试在数据转换步骤将其转换为最大值 Numeric(38,38),但仍然不够,并且不断溢出。以下是错误消息的一部分:

Error: 0xC02020C5 at Data Flow Task, Data Conversion [2]: Data conversion failed while converting column "%Recycle Matl Week" (216) to column "%Recycle Matl Week" (61).  The conversion returned status value 2 and status text "The value could not be converted because of a potential loss of data.".
Error: 0xC0209029 at Data Flow Task, Data Conversion [2]: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR.  The "Data Conversion.Outputs[Data Conversion Output].Columns[%Recycle Matl Week]" failed because error code 0xC020907F occurred, and the error row disposition on "Data Conversion.Outputs[Data Conversion Output].Columns[%Recycle Matl Week]" specifies failure on error. An error occurred on the specified object of the specified component.  There may be error messages posted before this with more information about the failure.
Error: 0xC0047022 at Data Flow Task, SSIS.Pipeline: SSIS Error Code DTS_E_PROCESSINPUTFAILED.  The ProcessInput method on component "Data Conversion" (2) failed with error code 0xC0209029 while processing input "Data Conversion Input" (3). The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running.  There may be error messages posted before this with more information about the failure.

我真的被这个问题困住了,我总觉得这是一个简单的解决方案,但就是不明白。从源代码更改 CSV 是不行的,但我在这里真的不需要那么高的精度(8 个十进制数字就足够了)。非常感谢我能得到的任何帮助,谢谢!

【问题讨论】:

    标签: csv ssis type-conversion overflow


    【解决方案1】:

    使用 DECIMAL(38,38),您不会为小数点前的数字留下任何空格,因此您会溢出

    当使用 DECIMAL 数据类型时,第一个数字是字段中允许的数字数量,第二个数字是允许这些数字中有多少是十进制

    例如

    • 十进制 (5,4)
      • 1.2345 可以工作
      • 12.345 将失败,因为您只在前面留下一个数字,在后面留下 4 个数字,前面的 2 个数字会溢出
    • 十进制 (5,1)
      • 1.2345 可以,但会四舍五入为 1.2,小数点后溢出将四舍五入以适应
      • 12.345 可以使用,但要四舍五入到 12.3
      • 12345.6 会失败,因为小数点 1 占用一个空格,所以小数点前只有 4 位。

    在您的情况下,小数点前至少需要两位数字,这样 DECIMAL(38,36) 才能工作

    SELECT CONVERT(DECIMAL(38,36),'89.95849436456480407115801673479014153261')
    

    【讨论】:

      【解决方案2】:

      CONVERT 解决方案也不起作用,因为某些数据点中仍有 38 个十进制数字,加上整数,仍然溢出。我尝试使用双浮动,但它也没有工作。但我使用辅助表管理了一个解决方法:

      我从一个辅助步骤开始,在 SQL Server 上创建和填充一个类似的表,但所有数据都是 VARCHAR 而不是 NUMERICAL。然后,我更改了使用 LEFT 剪切数据并使用 CAST 转换为数字的 SQL 代码。例如:

      CAST(
          CAST(LEFT([Total Weight Var %],12) AS DECIMAL(13,8)) / 100 
      AS DECIMAL(13,8)) AS OVER_WEIGHT_PCT,
      

      这不是最优雅的解决方案,但它确实有效。

      【讨论】:

      • 您只需读取为 VARCHAR 并在数据流中的派生列中以相同的方式修剪,就可以在没有附加表的情况下实现这一点,
      猜你喜欢
      • 2023-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2023-04-04
      相关资源
      最近更新 更多