|
How Tos
How to work with record-variables (version 2)?
Author: Alain Krikilion (alias kriki)
EXAMPLES for reading data WITHOUT locking the records
- You need to check if there are ANY records in your (filtered) table, but you don't need any of
them to work on. There are a lot of possibilities that all work, but performance can be a big
problem
- (VERY) WRONG: because it counts ALL records
IF TheTable.COUNT > 0 THEN ... // or COUNTAPPROX
- WRONG: It returns a SET of records to the client
IF TheTable.FINDSET THEN ...
- (LESS) WRONG: Until some versions ago (I don't remember which), you would use this
because there was no alternative. This opens a cursor on SQL.
IF TheTable.FIND('-') THEN ... // or FIND('+')
- (STILL) WRONG: It doesn't open a cursor in SQL, but it still returns 1 record (if there is at
least 1 available)
IF TheTable.FINDFIRST THEN ... // or FINDLAST
- CORRECT: it NEVER returns a record. It just returns a boolean to say there are/are not
records.
IF NOT TheTable.ISEMPTY THEN ...
- You want ONY the first/last record (if it exists), but NEVER more.
- VERY WRONG: It returns a set of records to the client
If TheTable.FINDSET THEN ... // to get the last record , you would need
to put ASCENDING(FALSE) before calling FINDSET (later more on this
because it is even MORE WRONG than VERY WRONG)
- WRONG: This opens a cursor on SQL.
If TheTable.FIND('-') THEN ... // or FIND('+')
- CORRECT: it doesn't open a cursor and it returns only 1 record
If TheTable.FINDFIRST THEN ... // or FINDLAST
- You want ALL records in ASCENDING order
- VERY WRONG: The FINDFIRST does not open a cursor and returns ONLY the first record.
The first time NEXT is called, NAV sees it hasn't a cursor and creates it in that moment.
IF TheTable.FINDFIRST THEN
REPEAT
...
UNTIL TheTable.NEXT = 0;
- WRONG: This will give you 1 record at a time.
IF TheTable.FIND('-') THEN
REPEAT
...
UNTIL TheTable.NEXT = 0;
- CORRECT: This will give you the first N records in one go, and after that 1 by 1.
IF TheTable.FINDSET THEN
REPEAT
...
UNTIL TheTable.NEXT = 0;
- You want ALL records in DESCENDING order
- MORE WRONG than VERY WRONG: It will generate a run-time error (try it out to know
the error-message)
TheTable.ASCENDING(FALSE);
IF TheTable.FINDSET THEN
REPEAT
...
UNTIL TheTable.NEXT = 0;
- CORRECT: version 1. Use this version if possible. It uses a FINDSET to get the records in a
faster way and save them in a temptable. Then it loops the temptable in DESCENDING
order. This can be useful if the number of records is <= N (the number of records
returned by FINDSET in one go). It is also best NOT to use this if there are a lot of records
because all those records will be first saved in a temptable and thus on the client.
IF TheTable.FINDSET THEN
REPEAT
// save the records in a temptable
tmpTheTable := TheTable;
tmpTheTable.INSERT(FALSE);
UNTIL TheTable.NEXT = 0;
// now loop the temptable
tmpTheTable.RESET;
tmpTheTable.ASCENDING(FALSE);
IF tmpTheTable.FIND('-') THEN
REPEAT
...
UNTIL tmpTheTable.NEXT = 0;
- CORRECT: version 2
TheTable.ASCENDING(FALSE);
IF TheTable.FIND('-') THEN
REPEAT
...
UNTIL TheTable.NEXT = 0;
EXAMPLES for modifying records.
| Author: |
Alain Krikilion (alias kriki) |
| Date: |
01/08/2009 |
| First release: |
26/07/2009 |
| Views: |
17163 |
| |
| Avg. rating: |
(rated 143 times) |
| |
| Discuss this How To in the forum (55 reactions) |
| |
| Rate this HowTo |
| |
- Please do not vote for the same HowTo more than once.
- Please be objective.
|
|
|
|
 |
|