For any number of reasons, you may decide you need a copy (usually partial) of some ISPF table. Possibly, the subset of the table you wish to operate on may not be easily parsed on the basis of the table's data content. You will have to build a temporary table and load it from the original table, perhaps row-by-row. Careful, now...
TBQUERY will tell you what an existing table looks like. As soon as the table is opened, TBQUERY can be invoked to give a list of the keys and names. From this, you can TBCREATE a replica table.
"TBOPEN SAMPLE" "TBQUERY SAMPLE KEYS(knames) NAMES(nnames)" "TBCREATE NEWCOPY KEYS"knames "NAMES"nnames
The column names returned from TBQUERY are enclosed within parentheses. Unless you have other uses for the lists, it isn't even necessary to trim them off. At this point, you are ready to write a duplicate or subset table, save for one easily overlooked aspect.
Remember 'extension variables'? Unless you ask when reading each input row you will not know whether there are extension variables attached to the row, nor will you know their names. Not knowing their names means not being able to specify which names should be attached to each output row.
do forever "TBSKIP" intbl "SAVENAME(xvars)" if rc <> 0 then leave "TBMOD " outtbl "SAVE"xvars end
Notice that the extension variables themselves did not have to be operated on in any way. We did not have to assign them, for instance. Because the row was read (the NOREAD parameter was not specified), all the row's variables: keys, names, and extension; were populated to the variable pool. They are therefore immediately available to be loaded to the output table row.
In fact, because of this behavior: populating every variable regardless of type; it might be good practice to never read a table row without acquiring the list of extension variables.
You can get fancier than this, of course. For an idea of just how much fancier you can get, take a look at TBCOPY over on my REXX page. TBCOPY doesn't use TBQUERY. It relies on an external subroutine, TBLGEN, which itself relies on a special table in which is stored the DNA of all permanent tables, sort of a data dictionary for tables. TBLGEN is much more elaborate than is strictly necessary, because it is intended to do all of an installations TBCREATEs for permanent tables. TBCOPY is likewise much more elaborate than is strictly necessary because it is intended to do all TBCOPYs for permanent tables within an installation.