문제

I have a combo-box widget. I want to create a WHERE statement that depends of the combo box.

If the value of the combo-box is 0 all customers should be shown. If the value differs from 0 only the customer matching the combo-box value should be shown.

Example :

Assign CB-Customer.
For each Customer WHERE /* after that*/
/*  something like this
IF CB-CUstomer = 0 THEN ASSIGN Customer.CustNum >= 0 .
ELSE                    ASSIGN Customer.CustNum = CB-Customer .
*/

I've seen a code like this before but I couldn't figure out how it works.

도움이 되었습니까?

해결책

Instead of combining both conditions in the WHERE phrase (which will probably kill any chance of using an index), why not define a query and open it depending on the CB-Customer.

DEFINE QUERY q-Customer FOR Customer.

IF CB-Customer = 0 THEN DO:
    OPEN QUERY q-Customer
    FOR EACH Customer
        WHERE Customer.CustNum >= 0
        NO-LOCK
        .
END.
ELSE DO:
    OPEN QUERY q-Customer
    FOR EACH Customer
        WHERE Customer.CustNum = CB-Customer
        NO-LOCK
        .
END.

GET FIRST q-Customer.
REPEAT WHILE AVAILABLE Customer:
    /* Whatever you want to do with the Customer record, for example: */
    DISPLAY Customer.

    GET NEXT q-Customer.
END.

You can do a lot of fancy tricks with queries, especially dynamic ones, but this should work in pretty much every recent version of OpenEdge.

다른 팁

FOR EACH Customer
    WHERE (CB-CUstomer = 0 AND Customer.CustNum >= 0)
       OR  Customer.CustNum = CB-CUstomer:

END.

This should do what you want, if I understand your question correctly.

I understand your question like this:

If the combo-box has the value zero you want to display all customers. Otherwise you want to display only the customers matching the customer number?

You can do a WHERE clause with a condition in it, but I would avoid it. In this case the query is very basic but when they get more complicated (and they do) where clauses with conditions in them are quite hard to read and understand. But I guess it's also about personal choices.

FOR EACH Customer NO-LOCK WHERE 
         Customer.CustNum = (IF CB-Customer = 0 THEN Customer.CustNum ELSE CB-Customer ):

    MESSAGE Customer.CustNum.
END.

I would prefer doing either two separate FOR EACH'es:

IF CB-Customer = 0 THEN DO:
    /* TABLE-SCAN is a quite new thing, might not work in your version */
    FOR EACH Customer NO-LOCK TABLE-SCAN:

        MESSAGE Customer.CustNum.
    END.
END.
ELSE DO:
    FOR EACH Customer NO-LOCK WHERE Customer.CustNum = CB-Customer:

        MESSAGE Customer.CustNum.
    END.
END.

Or a QUERY with separate QUERY-PREPARE statements:

DEFINE QUERY q FOR Customer.
DEFINE VARIABLE cQuery AS CHARACTER   NO-UNDO.

ASSIGN CB-Customer.

IF CB-Customer = 0 THEN DO:
    cQuery = "FOR EACH Customer NO-LOCK". 
END.
ELSE DO:
    cQuery = "FOR EACH Customer NO-LOCK WHERE Customer.CustNum = " + QUOTER(cb-customer).
END.

MESSAGE cQuery.

QUERY q:QUERY-PREPARE(cQuery).

QUERY q:QUERY-OPEN().

GET FIRST q.
REPEAT WHILE AVAILABLE Customer:
    MESSAGE Customer.CustNum.
    GET NEXT q.
END.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top