Issue with derived column transformation

2020-05-03 01:38发布

问题:

I have a column which is int and want to load the data based on condition for example say:

  • if the value is 1 then load column with inserted
  • value is 2 then load column with DELETED
  • value is 3 then load column with UPDATED

But when I tried doing this I am getting the following error:

Error at Data Flow Task [Derived Column [1666]]: Attempt to parse the expression "[Copy of operation]== "1" ? "INSERTED"" failed. The expression might contain an invalid token, an incomplete token, or an invalid element. It might not be well-formed, or might be missing part of a required element such as a parenthesis.    
Error at Data Flow Task [Derived Column [1666]]: Cannot parse the expression "[Copy of operation]== "1" ? "INSERTED"". The expression was not valid, or there is an out-of-memory error.    
Error at Data Flow Task [Derived Column [1666]]: The expression "[Copy of operation]== "1" ? "INSERTED"" on "Derived Column.Outputs[Derived Column Output].Columns[Derived Column 1]" is not valid.    
Error at Data Flow Task [Derived Column [1666]]: Failed to set property "Expression" on "Derived Column.Outputs[Derived Column Output].Columns[Derived Column 1]".

 (Microsoft Visual Studio)

===================================

Exception from HRESULT: 0xC0204006 (Microsoft.SqlServer.DTSPipelineWrap)

------------------------------
Program Location:

回答1:

I assume the issue is that you have an incomplete expression. The ternary operator ? : has three parts to it (boolean expression) ? True bits : False bits

[Copy of operation]== "1" ? "INSERTED" : [Copy of operation]== "2" ? "DELETED" : [Copy of operation]== "3"? "UPDATED" : "UNKNOWN"

This expression would read

  • If the value of column copy of operation is 1, then return INSERTED
    • else If the value of column copy of operation is 2, then return DELETED
      • else If the value of column copy of operation is 3, then return UPDATED
        • else return UNKNOWN

This does assume the data type of the column Copy of operation is a string. If it's a whole number, then you'd remove the double quotes around the values 1,2,3.

In the comments, you've indicated the __$operation indicates the value of the operation as where 1 = delete, 2 = insert, 3 = update (before change), and 4 = update (after change)

Continue with the above pattern along with changing out the differences (1 is delete in comment whereas 1 is inserted in question) to generate values.

A different approach is to use a tiny lookup table. You could even define it with an inline query and use a Lookup Component to add your operation description into the data flow

SELECT
    OperationId
,   OperationName
FROM
(
    VALUES ('1', 'INSERTED')
    , ('2', 'DELETED')
   -- etc
)D(OperationId, OperationName);

Again, ensure you have your data types aligned