Thursday, October 2, 2008

Create physical files on the fly:

CL programmers sometimes need to create physical files (PFs) on the fly. To create a PF that has an external definition, you have to use either DDS or DDL, and for CL programmers, that means having a separate source member that contains the file definition. Or does it have to mean that?
This article demonstrates how you can take advantage of QShell from a CL program to create a PF in which you can embed the source code, including the field definitions, inside the code of the CL program itself.
The trick to producing a PF on the fly from CL is QShell's db2 utility. This utility runs an SQL statement that's passed as a parameter. Because QShell commands can be run from CL's STRQSH command, embedding an SQL statement in a CL program is relatively easy. For example:
PGM
STRQSH CMD('db2 "create table SOMELIB.SOMEFILE ( +
field1 decimal(5,0), +
field2 char(30), +
field3 date +
)"')
ENDPGM

1 comment:

luis said...

Running the STRQSH command opens a terminal session which, if run interactively, forces you to press ENTER at the end of the command.

Instead, you could use:
QSH CMD('db2 "create table @pruebas.SOMEFIL2 ( field1 decimal(5,0), field2 char(30), field3 date )">/dev/null')

Which redirects the output to NULL