/*  List DB2 tables with more than a user-specified number of rows
    using the dynamic fetch interface */

  arg db user password nrows

  /* Connect the target database. Exit if connect fails. */
  r = REXXSQL("CONNECT" user "IDENTIFIED BY" password "TO" db)
  if sqlcode < 0 then exit 8

  /* Prepare and execute a SELECT on SYSCATALOG. */
  s = "SELECT CREATOR,TNAME,ROWCOUNT FROM SYSTEM.SYSCATALOG" ,
      "WHERE ROWCOUNT >=" nrows "ORDER BY ROWCOUNT DESC"
  r = REXXSQL("OPEN" s)

  /* Check for SQL errors. */
  if SQLCODE <> 0 then signal SQL_error

  /* Show the result stems CREATOR, TNAME and ROWCOUNT */
  r = REXXSQL("FETCH")
  do while sqlcode = 0
    tablename = strip(creator)"."strip(tname)
    say "Table" tablename "has" rowcount "rows"
    r = REXXSQL("FETCH")
  end
  if SQLCODE < 0 then signal SQL_error

  /* Terminate */
  r=REXXSQL("CLOSE")
  r=REXXSQL("COMMIT")
  exit

  /* Show SQLCODE, SQLERRM and related help text */
  SQL_error:
  call REXXSQLH sqlcode sqlerrm
  exit 8