Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

while select Statements

while select statements are used to handle data. They are the most widely used form of the select statement in X++.

while select loops over many records (meeting certain criteria) and can execute a statement on each record.

When you perform data manipulation by using the while select statement, you would typically do this in a transaction to ensure data integrity.

In a while select, the select statement itself is executed only one time, immediately before the first iteration of the statements in the loop. Also, any Boolean expressions (such as && iCounter < 4) added to the while select are tested only one time. This differs from how the while statement behaves in languages such as C++ and C#. For example, in X++ the following loop could iterate more than four times.

int iCounter = 0;
LedgerTable myLedgerTable;
;
while select myLedgerTable where myLedgerTable.AccountNum
    && iCounter < 4
{
    iCounter++;
}

The results of a while select statement are returned in a table buffer variable. If you use a field list in the select statement, only those fields are available in the table variable. If you use aggregate functions such as sum or count, the results are returned in the fields you perform the sum or count over. You can only count, average, or sum the integer and real fields.

The syntax of a while select statement resembles that of a select statement except that it is preceded by while select instead of select.

Printing a Telephone List (in Name Order) for Some Customers

void printTel()
{
    CustTable custTable;
    ;
 
    while select custTable order by Name
        where custTable.AccountNum > "1000" && custTable.AccountNum <= "5000"
    {
        print custTable.Name," ", custTable.Phone;
    }
}

This prints the name reference and telephone number of customers in CustTable who have an account number between 1001 and 5000.

deleteTransFromVoucher Method in the LedgerJournalTrans Table

static void deleteTransFromVoucher(JournalNum _journalNum,
Voucher _voucher)
{
    LedgerJournalTrans ledgerJournalTrans;
    LedgerJournalTable ledgerJournalTable = LedgerJournalTable::find(_journalNum);
    Counter            counter;
    ;
    ttsBegin;
 
    while select forUpdate ledgerJournalTrans
        index hint NumVoucherIdx
            where ledgerJournalTrans.journalNum == _journalNum 
               && ledgerJournalTrans.voucher == _voucher
    {
        ledgerJournalTrans.doDelete();
        counter++;
    }
    if (counter && ledgerJournalTable.journalType != LedgerJournalType::Periodic)
    {
    NumberSeq::release(ledgerJournalTable.voucherSeries, _voucher);
    }
    ttsCommit;
}

Use a while select statement to loop over a set of records that meet some criteria and perform an action on each record. One such action is to delete.

{

    record r;

    while select r

    where Conditions

    {

        r.delete();

    }

}

You can economize your X++ statements and achieve the same effect using delete_from.

{

    record r;

    ;

     delete_from r where Conditions ;

}

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.