# Select Statement Examples

Dynamics AX 4.0

All of the following examples use the CustTable.

To illustrate how the select statements work, assume that the customer table has only five records with the following contents.

AccountNum

NameRef

Balance

Blocked

100

Coho Vineyard

\$10,000

No

200

Coho Winery

\$20,000

No

300

Coho Vineyard & Winery

\$30,000

No

4000

Fabrikam, Inc.

\$5,000

Invoice

5000

Fourth Coffee

\$1,000,000

No

```void selectRecordExamples()
{
CustTable custTable;
;

// A customer is found and returned in custTable
select * from custTable;

// A customer with account number > "100" is found
select * from custTable
where custTable.AccountNum > "100";

/* Customer with the lowest account number > "100" found:
Coho Winery with number 200. */
select * from custTable order by accountNum
where custTable.AccountNum > "100";

// The next customer is read (Coho Vineyard & Winery)
next custTable;

// Customer with higest account number
// (greater than 100) found: Fourth Coffee
select * from custTable order by accountNum desc
where custTable.accountNum > "100";

// The next record is read (DESC): Fabrikam, Inc.
next custTable;

// Customer with highest account number found: Fourth Coffee
select reverse custTable order by accountNum;

// Customer with "lowest" name and account number
// in the interval 100 to 1000 is found. This is Coho Winery.
select * from custTable order by name
where custTable.accountNum > "100"
&& custTable.accountNum < "1000";

// The count select returns the number of customer
//  account numbers (5)
select count(AccountNum) from custTable;
// Prints the result of the count
print custTable.accountNum;

// Returns the sum of balances for non-blocked customers.
// The result is: SUM: \$1,060,000, stored in the Balance
//   field
select sum(balance) from custTable
where custTable.blocked == DebCreBlocked::No;
}
```