Is there any function to find the number of records in a tab

Posted by venkat_suv on 25-Feb-2013 04:33

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.

All Replies

Posted by Admin on 25-Feb-2013 04:41

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.

Posted by Peter Judge on 25-Feb-2013 08:42

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

Posted by gus on 25-Feb-2013 09:46

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).

Posted by Stefan Drissen on 25-Feb-2013 12:35

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

Posted by Peter Judge on 25-Feb-2013 12:41

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

Posted by Admin on 25-Feb-2013 13:01

I'd call it a serious issue if not.

Posted by Peter Judge on 25-Feb-2013 13:57

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

Posted by Admin on 25-Feb-2013 14:01

The core/language does close the query when the query is deleted (as expected/hoped ).

 

We know why we love this 4GL!

Posted by Rob Fitzpatrick on 25-Feb-2013 19:38

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";

Posted by venkat_suv on 26-Feb-2013 00:56

Thanks to everyone here, you made my understanding regardin this clear

This thread is closed