Sunday, October 14, 2018

Stairway to SQL Server Reporting Service: Decision Functions

As part of expressions, decision functions can be used in calculated fields, filters, formatting to dynamically control related properties. They can be used with SQL, MDX, DAX query to query data from database, cube, SSAS tabular model respectively. This article will give a brief introduction and comparison of three decision functions: IIf, Switch, Choose. Also examples of their usage are given at the end.

IIf Function

IIf function returns one of two values depending on whether the expression is true or not. The syntax is as below:
IIf( <Boolean Expression>, [Return if True], [Return if False])
It likes conditional logic in SSIS. The first parameter is the expression to be evaluated. If it is true, then return the value specified in [Return if True]; If it is false, then return the later value specified in [Return if False]. You can have nested multiple IIf functions and the later condition is dependent on previous conditions' evaluation.

Switch Function

Switch function returns the value associated with the first expression in a series that evaluates to true. The statements will be evaluated in the order in which they appear and the condition expression can be unrelated with each other. The syntax is as following:
=Switch(
       <Boolean Expression 1>, [Return if True 1],
       [Boolean Expression 2], [Return if True 2],
    ...
       [Boolean Expression n], [Return if True n],
      )

Choose Function

Choose function uses a single numeric evaluation to determine which of return values to return. The syntax is as below:
Choose(
     <Numeric Expression>, 
     [Return Expression 1],
     [Return Expression 2], 
     …, 
     [Return Expression n],
)

Comparisons of Three Decision Functions

As you can see, Choose function is quite different from the other two. Since the return only depends on the numeric evaluation specified. IIf and Switch function can be used interchangeably for a certain condition, as example of dynamic text font color below shown. For me, for case conditions are more than three, Switch function has clearer logic thus is easy to follow. However, Switch and Choose functions may have possible undefined results while IIf function won't since IIf function requires that a “return if true” value and a “return if false” value be provided in the expression.  One way to avoid this is to specify the last condition in Switch function to be TRUE as the following example.

 =Switch(Fields!OrderNumber.Value >= 10, "Blue", 
        Fields!OrderNumber.Value >= 5, "Yellow", 
 Fields!OrderNumber.Value >= 2, "Orange", 
 TRUE, "Red")
 

ProblemsExpressions
To set filter expression depending on Country Code from MDX query.Solution 1:
=IIF(Parameters!CountryCode.Value.ToString() <>
"[Location].[Country Code].&[USA]",
"4",
"6")
 

Solution 2:
=IIF(InStr(Parameters!CountryCode.Value
.ToString(),"USA")>0,
"6","4")
To format the report category based on input date parameters ReportDate.=IIF(
InStr("January, February, April, May,July,August,October,November",
Monthname(Datepart("m", Parameters!ReportDate.Label))
) > 0,
"Monthly","Quarterly"
)+ Space(1) + "Reports"
To dynamically change the color of a text box, go to properties, and set the following expression for font/Color Property by Switch function.=Switch(
 Fields!OrderNumber.Value >= 10, "Blue",
 Fields!OrderNumber.Value >= 5, "Yellow",
 Fields!OrderNumber.Value >= 2, "Orange",
 TRUE, "Red")
To dynamically change the color of a text box, go to properties, and set the following expression for font/Color Property by IIf function.=IIF(
Fields!OrderNumber.Value >= 10, "Blue",
IIF(Fields!OrderNumber.Value>= 5, "Yellow",
IIF(Fields!OrderNumber.Value>= 2,"Orange", "Red"
))))


Saturday, October 6, 2018

Decoding Errors with Error Column Names and in SQL Server 2016 Integration Service

Problem: When SSIS packages failed at the step of loading files, sometimes it doesn't tell which column is the one that caused failure. How to find out the troublemaker?

Solution: 

If you have SQL Server 2016 Integration Service or later version, you can retrieve error column names by using the new GetIdentificationStringByID method in SSIS 2016.

Below is an example showing how to use IDTSComponentMetaData130.GetIdentificationStringByID method to decode error column names and error descriptions by using Script Component.

Step 1: Choose script component and connect it with error output from upstream.
Step 2: Select Transformation as script component type.

Step 3: After selecting C# as script language to use, check ErrorCode and ErrorColumn as Input Columns as shown below.

Step 4: Add output columns ErrorColumnName and ErrorDesc to represent error column names and descriptions respectively, then specify the column type and length.


Step 5: Edit script and added this following to Input0_ProcessInputRow function:

public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        
        IDTSComponentMetaData130 componentMetaData = this.ComponentMetaData as IDTSComponentMetaData130;
        Row.ErrorColumnName = componentMetaData.GetIdentificationStringByID(Row.ErrorColumn);
        Row.ErrorDesc = ComponentMetaData.GetErrorDescription(Row.ErrorCode);
    };
Then you can add a flat file output to write the errors with detailed information about error column names and descriptions.

From the following output, you can tell that the loading failure is because input column "OLE DB Destination.Inputs[OLE DB Destination Input].Columns[OrderDate]". The error description is "The data value cannot be converted for reasons other than sign mismatch or data overflow."