Using Substitution Variables | KEVIN RESCHENBERG 08-02-2004 |
Last week we discussed SQR substitution
variables—what they are and how they're defined, with a few simple examples. Today I'll show
you other ways in which these can be used.
PeopleSoft delivers a file called "setenv.sqc". This is copied into a program using the
#INCLUDE directive, generally at the top of the program. (It is not normally
important where you code #INCLUDE for an SQC. However, in this case, we must
place it at the beginning of the program. Recall that substitution variables must be defined
physically before use in the program. The logic flow of the program is irrelevant.
The setenv.sqc file contains numerous substitution variable definitions and must
be placed in the SQR file before any of these are used. Therefore, it is generally placed
at the top of the program.)
Setenv.sqc also #INCLUDE s other SQCs. Together, they "set the environment" by
defining a number of substitution variables that are used by other delivered SQCs. We can use
them in our own programs as well.
Setenv.sqc (actually, one of its included SQCs) defines the operating system. Last week I
used the substitution variable {FILEPREFIX} in an example. This is defined in setenv.sqc:
#ifdef NT
#define FILEPREFIX C:\TEMP\
#end-if
#ifdef UNIX
#define FILEPREFIX /usr/tmp/
#endif
...and similarly for MVS and OS400. We can then use FILEPREFIX to construct a file name that
is appropriate whether the SQR is running on an NT server or workstation (under SQRW, for example)
or running on a Unix server:
open '{FILEPREFIX}output.txt' as 1 for-writing record=1000:vary ...
When the program runs onder Unix, the file name is "/usr/tmp/output.txt". Under NT, it's
"C:\TEMP\output.txt".
Here's another example from setenv.sqc:
#ifndef PTDateDelim
#define PTDateDelim /
#end-if
#define PTRPTDATE 'MM{PTDateDelim}DD{PTDateDelim}{PTRPTYear}' !MM/DD/YYYY
{PTRPTYear} has been previously defined as YYYY .
The purpose of the last line, as shown in the comment, is to define a substitution variable
containing the characters MM/DD/YYYY for use
in date routines. But it also includes some built-in flexibility. The first line,
#ifndef PTDateDelim , says that if the
substitution variable PTDateDelim has not been defined yet ("if not defined"), then
it will be defined as a forward slash (/). If we want to override this for one SQR, we could
define that variable in our own program as "-" and then PTRPTDATE will become MM-DD-YYYY:
#define PTDateDelim -
#include 'setenv.sqc'
Another predefined variable corresponds to the database. One of the following will be defined:
{ORACLE}
{MICROSOFT}
{SYBASE}
{INFORMIX}
{DB2ALL}
You can then enclose blocks of code in #ifdef ORACLE ,
for example. Setenv.sqc also defines a number of "date wrappers" and SQL keywords for each
database. It goes to great lengths to provide for platform-independent code. (I have rarely seen
custom code using these features, however. I guess that most of us hope the installation will
never switch to a different database!)
One other very common use of substitution variables is to ensure that an SQC is included
in your program only once. Since the #INCLUDE can
be nested, it's possible to have the same code included many times—but if it contains a
procedure, for example, a syntax error would occur. We can prevent this easily:
! This is in MYSQC.SQC
#ifndef MYSQC_INCLUDED
#define MYSQC_INCLUDED
... (code) ...
#end-if
All of the code in this SQC is wrapped within the #IFNDEF / #END-IF
block. This ensures that the variable MYSQC_INCLUDED will be defined only once. Although the file will be
physically included each time, its code will be exposed to SQR only once and hidden each time after that.
|