Calling a Scalar-valued Function in SSIS
-
13-09-2019 - |
Question
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
Solution
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.