domingo, 6 de noviembre de 2011

Select Statement with an Outer Join (note)

The join statement is a source of initial head scratching for most developers familiar with the ANSI sql script.  MSDN has a useful example on a standard join statement, where the order by clause is in a strange place for most of us - after the from keyword but before the join.  We have no on in the psuedo sql used in X++.

It's a similar issue for outer join, but I was caught out with the where keyword placement as well.  The Ax 2012 example of the outer join is below, translated to Ax 2009 and I've added a second where keyword in there as well.  The reason that I've highlighted this was originally I had the salesTable.PaymSpec == '' clause at the bottom of the query and therefore returning all rows from SalesTable. To me it seemed so wierd at first to see two where keywords in there! 

static void Job10(Args _args)
{
    SalesTable      salesTable;
    SalesLine       salesTableLine;
    struct struct4;
    ;
    struct4 = new struct
        ("str SalesID;"
        + "date DeliveryDate;"
        + "real LineNum;"
        + "real SalesQty"
        );

    while
    SELECT
            *
        FROM
            salesTable
        WHERE
            salesTable.PaymSpec == ''
    OUTER JOIN salesTableLine
        WHERE
            salesTable.SalesID == salesTableLine.SalesID
            && salesTableLine.SalesQty < 0
    {
        struct4.value("SalesID", salesTable.SalesID);
        struct4.value("DeliveryDate", salesTable.DeliveryDate);
        struct4.value("LineNum", salesTableLine.LineNum);
        struct4.value("SalesQty", salesTableLine.SalesQty);
        info(struct4.toString());
    }
}

Now all you people need to do is think about where an order by will go...  Yep, above the where clause.

No hay comentarios:

Publicar un comentario