Output of all database tables

Posted by Admin on 20-Sep-2010 09:36

I want to create a script that displays all tables per database with the following line:

proutil <databasename> -C dump <tablename> /data/data

I have the following code so far:

DEF VAR a AS CHAR.
DEF VAR i AS INTEGER.

DO i = 1 TO NUM-DBS:
   a = LDBNAME (i).
   CREATE ALIAS "DICTDB" FOR DATABASE VALUE (a).
   DISPLAY a LABEL "Database name: ". PAUSE.
   FOR EACH DICTDB._file NO-LOCK WHERE DICTDB._file._Tbl-Type = "T":
      PUT 'proutil ' a ' -C dump '  DICTDB._file._file-name FORMAT 'x(20)' ' /data/data' SKIP.
   END.
   DELETE ALIAS DICTDB.
END.

This isn't working correctly. All the databases will pass, but for all the databases the lines were created with the tables from the first database.

F.E. first database = DB1

proutil DB1 -C dump akk /data/data

proutil DB1 -C dump akm /data/data

proutil DB1 -C dump art /data/data

second database = DB2

proutil DB2 -C dump akk /data/data

proutil DB2 -C dump akm /data/data

proutil DB2 -C dump art /data/data

So for database DB2 also the tables from DB1 are displayed and not the tables from DB2.

I think it has something to do with the connection to the database.

What is wrong within the code? Can you help me?

Regards,

Michiel

All Replies

Posted by Håvard Danielsen on 20-Sep-2010 14:01

michieltimmermans wrote:

I want to create a script that displays all tables per database with the following line:

proutil -C dump /data/data

I have the following code so far:

DEF VAR a AS CHAR.
DEF VAR i AS INTEGER.

DO i = 1 TO NUM-DBS:
   a = LDBNAME (i).
   CREATE ALIAS "DICTDB" FOR DATABASE VALUE (a).
   DISPLAY a LABEL "Database name: ". PAUSE.
   FOR EACH DICTDB._file NO-LOCK WHERE DICTDB._file._Tbl-Type = "T":
      PUT 'proutil ' a ' -C dump '  DICTDB._file._file-name FORMAT 'x(20)' ' /data/data' SKIP.
   END.
   DELETE ALIAS DICTDB.
END.

This isn't working correctly. All the databases will pass, but for all the databases the lines were created with the tables from the first database.

This is because the created alias does not affect current running code. The alias must be defined before a procedure that uses the alias is called (or class is instantiated).

Move the 'for each' block to a separate procedure that you call after you have created the alias. 

Posted by Admin on 20-Sep-2010 14:06

Move the 'for each' block to a separate procedure that you call after you have created the alias. 

Just to be clear, it needs to be a separate external procedure, not an internal procedure.

Posted by bootcomp on 21-Sep-2010 08:14

You may also be able to do it in the same procedure, using dynamic queries.

DEF VAR qh AS HANDLE.
DEF VAR bh AS HANDLE.

    CREATE QUERY qh.
    DO z = 1 TO NUM-DBS:
        CREATE BUFFER bh FOR TABLE LDBNAME(z) + '._file' NO-ERROR.
        qh:SET-BUFFERS(bh).
        qh:QUERY-PREPARE('FOR EACH _file').
        qh:QUERY-OPEN.

       

        qh:QUERY-CLOSE.
        DELETE OBJECT bh.
    END.
    DELETE OBJECT qh.

(or similar)

This thread is closed