Wednesday, January 30, 2019

View computed column to get OR for two flags

Nothing new, just to reiterate the basics.

Say, we need to add a computed column as a result of OR function over two other columns of Boolean type:myPPOFirmDisableForSalesLine and myPPOFirmDisableForTransferLine.





Create a new field myPPOFirmDisable as Enum Computed field




and point it to the following method.



private static server str myPPOFirmDisable(int _tableNum)
{
    DictView    ctView      = new DictView(tableNum(myPeggedRefTransOnHoldUnionView));
    str         sTransType1 = SysComputedColumn::comparisonField(identifierStr(myPeggedRefTransOnHoldUnionView), identifierStr(mySalesTableOnHoldView),  fieldStr(mySalesTableOnHoldView, myPPOFirmDisableForSalesLine));
    str         sTransType2 = SysComputedColumn::comparisonField(identifierStr(myPeggedRefTransOnHoldUnionView), identifierStr(mySalesTableOnHoldView),  fieldStr(mySalesTableOnHoldView, myPPOFirmDisableForTransferLine));
    str     sRet        = '';

    sRet =
            SysComputedColumn::or2
            (
                SysComputedColumn::equalExpression(sTransType1, SysComputedColumn::returnLiteral(NoYes::Yes)),
                SysComputedColumn::equalExpression(sTransType2, SysComputedColumn::returnLiteral(NoYes::Yes))
            );
    sRet =
        SysComputedColumn::if(sRet, SysComputedColumn::returnLiteral(NoYes::Yes), SysComputedColumn::returnLiteral(NoYes::No));

    return sRet;
}

The whole point is to create a string with an SQL statement as a result in static method. There are a lot of standard functions in SysComputedColumn class aiming to help with syntax.

As you can see in this example it is not enough to get a logical comparison result at the first sRet assigning. The latter must be converted explicitly to an SQL string as it goes at the second step.

Finally we get the following string as a part of the whole SQL statement.

CASE WHEN (T1.MYPPOFIRMDISABLEFORSALESLINE = 1) OR (T1.MYPPOFIRMDISABLEFORTRANSFERLINE = 1) THEN 1 ELSE 0 END

No comments: