For eg:
Consider a customer table, any function is there to find the number of records in it ?
I found the number of records using the below code, but i need in a function.
DEFINE VARIABLE iCount AS INTEGER INITIAL 1 NO-UNDO.
FOR EACH customer NO-LOCK:
iCount = iCount + 1.
END.
The code that you have in your sample returns the number of records + 1 . You should use INITIAL 0 with your variable definition.
You can make your own function in the ABL. If you want to pass in the table name as an argument, you'll need to DEFINE a dynamic buffer and query however.
venkat_suv wrote:
For eg:
Consider a customer table, any function is there to find the number of records in it ?
I found the number of records using the below code, but i need in a function.
DEFINE VARIABLE iCount AS INTEGER INITIAL 1 NO-UNDO.
FOR EACH customer NO-LOCK:
iCount = iCount + 1.
END.
You can use a dynamic query with the PRESELECT option, similar to the below. This may take some time (relatively-speaking), depending on the number of records in the table.
FUNCTION RETURN INTEGER GetNumRows(INPUT pcTableName AS CHAR):
DEF VAR hQry AS HANDLE.
CREATE QUERY hQry.
hQry:QUERY-PREPARE('PRESELECT EACH ' + pcTableName + ' NO-LOCK ').
hQry:QUERY-OPEN().
RETURN hQry:NUM-RESULTS.
FINALLY:
hQry:QUERY-CLOSE() NO-ERROR.
DELETE OBJECT hQry.
END FINALLY.
END FUNCTION.
-- peter
PRESELECT EACH ' + pcTableName + ' USE-INDEX TABLE-SCAN NO-LOCK should be faster, provided that the table is in a type ii data area.
(version 11.0 and later).
pjudge wrote:
FINALLY:
hQry:QUERY-CLOSE() NO-ERROR.
DELETE OBJECT hQry.
END FINALLY.
-- peter
Does closing the query before deleting the query object do anything useful? I was under the impression that deleting the query object was sufficient.
Thanks,
Stefan
Funny you should say that ... as I was writing the snippet I asked myself the same question. I have it there somewhat out of habit, to be honest, but I would expect the DELETE code to perform a QUERY-CLOSE under the covers first.
-- peter
I'd call it a serious issue if not.
mikefe wrote:
I'd call it a serious issue if not.
The core/language does close the query when the query is deleted (as expected/hoped ).
-- peter
The core/language does close the query when the query is deleted (as expected/hoped ).
We know why we love this 4GL!
If you have a SQL broker running against your database, counting records that way will perform a lot better for big tables than using ABL.
E.g., in sqlexp or your SQL client of choice: select count(*) from PUB."mytable";
Thanks to everyone here, you made my understanding regardin this clear