在 SSIS 中调用标量值函数

发布于 2024-07-23 08:25:37 字数 2043 浏览 3 评论 0原文

有什么方法可以从 SSIS 中的派生列转换中执行标量值函数吗?

-场景-

我的源数据库中有一个函数,可以根据记录的 UOM 列中的 UOM 值转换权重。 我想在 ETL 过程中利用此功能来确保我的体重测量值始终为磅。 我可以从派生列中调用此函数吗? 如果没有,是否可以在数据流中使用另一个转换任务(尝试避免暂存列)?

dbo.Tasks 表

id |  Name  | netWeight | grossWeight | UOM
12   Task12     30000        50000      10

dbo.MeasurementUnits 表

id |   Name    | Shortname | Type | Precision
12  Kilogram    kg          3   10000       
14  Pound       lb          3   10000

dbo.GetConvertedWeight 函数

    ALTER  FUNCTION [dbo].[GetConvertedWeight](@iWeight money, @ifromUOM int, @iToUOM int) 
    RETURNS money
    AS  
    BEGIN 
    DECLARE @lConvertedWeight money, 
            @lKgToGrams money,
            @lLbToGrams money,
            @lOzToGrams money,
            @lWeightInGrams money

    --convert the weight to grams first.
    SELECT @lWeightInGrams = CASE WHEN @iFromUOM = 12 THEN (ISNULL(@iWeight,0) * 1000)
                         WHEN @iFromUOM = 14 THEN (ISNULL(@iWeight,0) * 453.5924)
                         WHEN @iFromUOM = 15 THEN (ISNULL(@iWeight,0) * 28.3495)
                         WHEN @iFromUOM = 13 THEN (ISNULL(@iWeight,0))
                         ELSE ISNULL(@iWeight,0)
                         END

    --Convert the converted weight to grams to the desired weight
    SELECT @lConvertedWeight = CASE WHEN @iToUOM = 12 THEN (ISNULL(@lWeightInGrams,0) / 1000)
                            WHEN @iToUOM = 13 THEN ISNULL(@lWeightInGrams,0)
                            WHEN @iToUOM = 14 THEN (ISNULL(@lWeightInGrams,0)/453.5924)
                            WHEN @iToUOM = 15 THEN (ISNULL(@lWeightInGrams,0) / 28.3495 )
                            ELSE (ISNULL(@lWeightInGrams,0)/453.5924)
                            END
RETURN @lConvertedWeight

函数调用示例

dbo.GetConvertedWeight(dbo.Tasks.netWeight, dbo.Tasks.weightUOM, 14) AS netWeight

Is there any way to execute a scalar-valued function from within a Derived Column transformation in SSIS?

-Scenario-

I have a function in my source DB that converts weights based on a UOM value in the record's UOM column. I want to utilize this function in the ETL process to ensure that my weight measures are always pounds. Can I call this function from within a Derived Column? If not, is there another transformation task I could utilize within the Data Flow (trying to avoid staging columns)?

dbo.Tasks table

id |  Name  | netWeight | grossWeight | UOM
12   Task12     30000        50000      10

dbo.MeasurementUnits table

id |   Name    | Shortname | Type | Precision
12  Kilogram    kg          3   10000       
14  Pound       lb          3   10000

dbo.GetConvertedWeight function

    ALTER  FUNCTION [dbo].[GetConvertedWeight](@iWeight money, @ifromUOM int, @iToUOM int) 
    RETURNS money
    AS  
    BEGIN 
    DECLARE @lConvertedWeight money, 
            @lKgToGrams money,
            @lLbToGrams money,
            @lOzToGrams money,
            @lWeightInGrams money

    --convert the weight to grams first.
    SELECT @lWeightInGrams = CASE WHEN @iFromUOM = 12 THEN (ISNULL(@iWeight,0) * 1000)
                         WHEN @iFromUOM = 14 THEN (ISNULL(@iWeight,0) * 453.5924)
                         WHEN @iFromUOM = 15 THEN (ISNULL(@iWeight,0) * 28.3495)
                         WHEN @iFromUOM = 13 THEN (ISNULL(@iWeight,0))
                         ELSE ISNULL(@iWeight,0)
                         END

    --Convert the converted weight to grams to the desired weight
    SELECT @lConvertedWeight = CASE WHEN @iToUOM = 12 THEN (ISNULL(@lWeightInGrams,0) / 1000)
                            WHEN @iToUOM = 13 THEN ISNULL(@lWeightInGrams,0)
                            WHEN @iToUOM = 14 THEN (ISNULL(@lWeightInGrams,0)/453.5924)
                            WHEN @iToUOM = 15 THEN (ISNULL(@lWeightInGrams,0) / 28.3495 )
                            ELSE (ISNULL(@lWeightInGrams,0)/453.5924)
                            END
RETURN @lConvertedWeight

Example function call

dbo.GetConvertedWeight(dbo.Tasks.netWeight, dbo.Tasks.weightUOM, 14) AS netWeight

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无敌元气妹 2024-07-30 08:25:37

没有。 您需要的是 OLE DB 命令 来执行此操作。 将结果发送到输出列,您的生活应该会很美好——至少对于 SSIS 来说是这样。

Nope. What you'll want is an OLE DB Command to do that. Send the results to an Output Column, and life should be peachy keen for you--at least with regards to SSIS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文