The introduction of the tool CLPPlus in DB2 9.7 for Linux, UNIX, and Windows brought a number of advantages over previous DB2 tools:
- It has many more reporting features than the old DB2 command line processor.
- The capabilities can easily be utilized in scripts for regular database reporting or maintenance.
- Being a remote client tool, it can connect to remote databases without requiring you to catalog the remote databases in the local machine.
- Its server independence allows you to connect to different IBM data servers from within a single session.
- It has a number of SQL compatibility features to DB2 that are helpful when it comes to migrating from other relational databases. These features enable you to migrate SQL scripts from other database server products to DB2, either as is, or with minimal modifications. This greatly reduces the migration effort involved in modifying the scripts from different database vendors.
In the following sections, learn about the major report formatting options supported by CLPPlus, with examples to help you get started.
CLPPlus is available in all DB2 9.7 installations and later. It is installed automatically by default along with other server and client components. It is also part of the DB2 Data Server Driver package, which can be downloaded at no charge and installed independently (see Resources).
Once you have CLPPlus installed, you can start CLPPlus in any of the following thee ways:
- In Windows, click on Start > Run
to bring up a command window. Enter
clpplus, and click on OK.
Figure 1. Starting from the Windows Run command prompt
- From an operating system command window, enter
clpplus, and press Enter. This applies both to Windows and to AIX and Linux command shells.
Figure 2. Starting from a command prompt
- Go to Start > All Programs >
IBM DB2, and then select Command Line
Tools > Command Line Processor Plus.
Figure 3. Initiating from the GUI interface
Get started with report options
CLPPlus provides several commands that you can use to specify the report format for an executed query. These commands can be broadly categorized into three categories:
- Formatting column values using the
COLUMNcommand - Using client-side aggregation functions and corresponding formats for
specific column values using
BREAKandCOMPUTEcommands - Generating dynamic headers and footers for the generated reports
In the following sections, examine each of these categories.
CLPPlus provides several options to format columns. These are useful in
formatting reports containing multiple columns of different types and
widths. The COLUMN command allows formatting of
column values as well as modifying column attributes such as header text,
width, and so on. Listing 1 provides an example of
COLUMN command syntax:
Listing 1.
COLUMN command
COL[UMN] [column_name-1 [CLE[AR] |
FOR[MAT] format_spec |
HEA[DING] heading_text |
JUSTIFY [ LEFT | RIGHT | CENTER ] |
WRAPPED | TRUNCATED |
FOLD_AFTER |
FOLD_BEFORE |
NEWLINE |
LIKE <column_name-2> |
NULL <user-text> |
PRINT|NOPRINT |
NEW_VALUE variable-name |
OLD_VALUE variable-name |
ALIAS alias-name |
ON |
OFF...]]
|
In Listing 1, column_name-1 specifies
the name of a column in the result set of an executed query. The
COLUMN command is used to specify the format
specification for the column column_name-1.
The various options of the command are as follows:
CLEAR- Clear the previously defined specifications for this column.FORMAT- Indicate format specifications for numeric and alphanumeric data values.HEADING- Provide column heading text.JUSTIFY- Left, right, or center justify the column values.WRAPPED- When the column value exceeds the column width, wrap the value to the next row.TRUNCATED- When the column value exceeds the column width, truncate the exceeding characters.NULL- When null values are encountered, print user-provided text.PRINT | NOPRINT- Specify whether the column in the result set is to be printed or not.ON | OFF- Turn column specifications for a specific column on or off.LIKE- Copy the format specifications for column-name-1 from column-name-2.FOLD_AFTER,FOLD_BEFORE- When a record exceeds the number of characters available for a row, this option allows you to decide where to fold the record—after or before the current column.NEWLINE- Same asFOLD_AFTERoption.ALIAS- Provides an alias name whose column format specifications will be used for column-name-1.
The following examples demonstrate how you would use these options. Note: These examples assume the availability of the SAMPLE database that comes standard with a DB2 server installation.
Formatting according to column types
If you want the values in the report to be formatted according to column
types, then the column option FORMAT can be
defined on the columns to specify the report format for the column values.
The example in Listing 2 shows how you would use
the FORMAT options for two different column
types. Column SALARY is formatted with decimal formatting "99999.99." The
values not adhering to this format will be displayed using the pound
character (#). The column LASTNAME is formatted with "A6," which restricts
the column values to a maximum of six characters.
Listing 2. Formatting column types
SQL>column salary format 99999.99
SQL>column lastname format A6
SQL>firstnme, lastname, salary from employee ;
FIRSTNME LASTNA SALARY
------------ ------ -----------
CHRISTINE HAAS ########
MICHAEL THOMPS 94250.00
ON
SALLY KWAN 98250.00
JOHN GEYER 80175.00
IRVING STERN 72250.00
EVA PULASK 96170.00
I
EILEEN HENDER 89750.00
SON
THEODORE SPENSE 86150.00
R
VINCENZO LUCCHE 66500.00
SSI
SEAN O'CONN 49250.00
ELL
|
This example shows how you would restrict column values to a particular
width. The column options WRAPPED and
TRUNCATED give you the option to wrap or
truncate values if they exceed the specified length. By default, columns
are set to WRAPPED.
In Listing 3, FORMAT A8
sets FIRSTNME and
LASTNAME to have a maximum width of eight
characters. As illustrated in Listing 3, the
additional characters can either be wrapped or truncated. The dots (...)
indicate that part of the name has been truncated.
Listing 3. Formatting column widths
SQL>column firstnme format A8 wrapped SQL>column lastname format A8 truncated SQL>select firstnme, lastname from emp; FIRSTNME LASTNAME -------- -------- CHRISTIN HAAS E MICHAEL THOMPSON SALLY KWAN JOHN GEYER IRVING STERN EVA PULASKI EILEEN HENDE... THEODORE SPENSER VINCENZO LUCCH... SEAN O'CON... DELORES QUINTANA HEATHER NICHOLLS BRUCE ADAMSON ELIZABET PIANKA H MASATOSH YOSHI... I MARILYN SCOUTTEN |
Next, let's look at how you can justify your columns using CLPPlus. The example in Listing 4 assumes that you want all alphanumeric values to be left-justified or centered, and all numeric values to be right-justified.
Listing 4. Justifying columns
SQL>column firstnme justify left SQL>column midinit justify center SQL>column salary justify right SQL>select firstnme, midinit, salary from employee; FIRSTNME MIDINIT SALARY ------------ ------- ----------- CHRISTINE I 152750.00 MICHAEL L 94250.00 SALLY A 98250.00 JOHN B 80175.00 IRVING F 72250.00 EVA D 96170.00 EILEEN W 89750.00 THEODORE Q 86150.00 VINCENZO G 66500.00 SEAN 49250.00 DELORES M 73800.00 HEATHER A 68420.00 BRUCE 55280.00 ELIZABETH R 62250.00 MASATOSHI J 44680.00 MARILYN S 51340.00 JAMES H 50450.00 DAVID 57740.00 WILLIAM T 68270.00 JENNIFER K 49840.00 JAMES J 42180.00 SALVATORE M 48760.00 |
The two important commands used for the report generation are
BREAK and COMPUTE.
These commands are used together during report generation to format the
report based on column values. Using BREAK and
COMPUTE allows reports to be broken down into
multiple sets of related records, thus making a report more readable and
sorted.
The BREAK command is always used in tandem with
the COMPUTE command. Each time a row is
processed for printing, the BREAK command
compares the value of the column to the previous value. When the column
value changes, row processing breaks and performs the actions specified in
the COMPUTE command, and then the specified
SKIP action in the
BREAK command is performed.
Here is the syntax for BREAK:
BREAK ON <column-name> [ SKIP < PAGE | n > ] |
where n is an integer.
COMPUTE is always used with the
BREAK command. When a break point is reached
for a specific column (column-2), COMPUTE for
that column gets invoked. The specified aggregate function is performed on
all the values of column-1 since the last break point for column-2.
Here is the syntax:
COMPUTE <function LABEL text OF column-1>+ ON column-2 |
where function is: [ SUM | MAX | MIN | AVG | COUNT | NUM | STD | VAR ].
If a report needs to be broken into separate sets of records based on a
particular column value, then you can use the
BREAK command. In the example in Listing 5, the break is defined on the column
EDLEVEL. So each time the EDLEVEL column value changes, the result table
is broken with a break separator and skipped line.
Listing 5. Breaking on column values
SQL>break on edlevel skip 2
SQL>select edlevel, firstnme, lastname, salary, bonus from employee order by edlevel;
EDLEVEL FIRSTNME LASTNAME SALARY BONUS
------- ------------ --------------- ----------- -----------
12 JOHN PARKER 35340.00 300.00
12 MAUDE SETRIGHT 35900.00 300.00
12 MICHELLE SPRINGER 35900.00 300.00
*******
14 THEODORE SPENSER 86150.00 500.00
14 SEAN O'CONNELL 49250.00 600.00
14 JAMES JEFFERSON 42180.00 400.00
14 PHILIP SMITH 37750.00 400.00
14 WING LEE 45370.00 500.00
14 GREG ORLANDO 39250.00 600.00
14 HELENA WONG 35370.00 500.00
*******
15 DANIEL SMITH 49180.00 400.00
15 MARIA PEREZ 37380.00 500.00
*******
16 JOHN GEYER 80175.00 800.00
EDLEVEL FIRSTNME LASTNAME SALARY BONUS
------- ------------ --------------- ----------- -----------
16 IRVING STERN 72250.00 500.00
16 EVA PULASKI 96170.00 700.00
16 EILEEN HENDERSON 89750.00 600.00
16 DELORES QUINTANA 73800.00 500.00
16 BRUCE ADAMSON 55280.00 500.00
16 MASATOSHI YOSHIMURA 44680.00 500.00
16 JAMES WALKER 50450.00 400.00
16 DAVID BROWN 57740.00 600.00
16 SYBIL JOHNSON 47250.00 300.00
16 RAMLAL MEHTA 39950.00 400.00
16 JASON GOUNOT 43840.00 500.00
16 KIYOSHI YAMAMOTO 64680.00 500.00
16 ROY ALONZO 31840.00 500.00
*******
|
If your report needs to be broken into a set of records based on a
particular column value, and then you need to perform some computation on
those records, you can use BREAK and
COMPUTE together, as shown in Listing 6.
In Listing 6, BREAK is
defined on column EDLEVEL. COMPUTE has been
defined on column EDLEVEL, which will perform a compute operation on the
column SALARY. So each time the EDLEVEL column value changes, the report
breaks and the specified compute operation MAX
is performed on the column SALARY. The number of lines skipped after each
breakpoint can be modified using the SKIP
option in the BREAK command.
Listing 6. Performing a computation on a break
SQL> BREAK ON EDLEVEL SKIP 2
SQL> COMPUTE MAX LABEL MAX_SAL of SALARY on EDLEVEL
SQL> SELECT EDLEVEL, EMPNO, FIRSTNME, SALARY FROM EMPLOYEE ORDER BY EDLEVEL;
EDLEVEL EMPNO FIRSTNME SALARY
------- ------ ------------ -----------
12 000290 JOHN 35340.00
12 000310 MAUDE 35900.00
12 200310 MICHELLE 35900.00
******* -----------
MAX_SAL 35900.00
14 000100 THEODORE 86150.00
14 000120 SEAN 49250.00
14 000230 JAMES 42180.00
14 200120 GREG 39250.00
14 200330 HELENA 35370.00
******* -----------
MAX_SAL 86150.00
...
...
|
In the example in Listing 7, multiple computes have
been performed for each break point on column SALARY. This example shows
how comma-separated multiple computes can be defined in a single
COMPUTE command. MAX
and AVG operations are performed on BONUS and
SALARY columns respectively for each break point on EDLEVEL.
Listing 7. Multiple computes
SQL> BREAK on EDLEVEL skip 1
SQL> COMPUTE MAX label MAX_BON of BONUS, AVG label AVG_SAL of SALARY
on EDLEVEL
SQL> SELECT EDLEVEL, FIRSTNME, BONUS, SALARY FROM EMPLOYEE ORDER
BY EDLEVEL;
EDLEVEL FIRSTNME BONUS SALARY
------- ------------ ----------- -----------
12 JOHN 300.00 35340.00
12 MAUDE 300.00 35900.00
12 MICHELLE 300.00 35900.00
******* -----------
MAX_BON 300.00
******* -----------
AVG_SAL 35713.33
14 THEODORE 500.00 86150.00
14 SEAN 600.00 49250.00
14 JAMES 400.00 42180.00
14 PHILIP 400.00 37750.00
14 WING 500.00 45370.00
14 GREG 600.00 39250.00
14 HELENA 500.00 35370.00
******* -----------
MAX_BON 600.00
******* -----------
AVG_SAL 47902.85
...
...
|
There may be scenarios where you would like to do some computations on sets
of records broken based on particular column values, but would not like to
print that column in the report. In this case, you can use the
NOPRINT option. Use the column
PRINT and NOPRINT
options to enable and disable printing a particular column in the report.
In Listing 8, the MAX
operation is performed on column SALARY for each break point on column
EDLEVEL. But since NOPRINT is enabled for
column EDLEVEL, this column is omitted from the report. As shown, not
printing column EDLEVEL in the report doesn't affect the compute and break
operations defined on it.
Listing 8. Omitting values in your report
SQL>break on EDLEVEL skip 1
SQL>column edlevel noprint
SQL>COMPUTE MAX LABEL MAX_SAL of SALARY on EDLEVEL
SQL>SELECT EDLEVEL, EMPNO, FIRSTNME, SALARY FROM EMPLOYEE ORDER BY EDLEVEL;
EMPNO FIRSTNME SALARY
------ ------------ -----------
000290 JOHN 35340.00
000310 MAUDE 35900.00
200310 MICHELLE 35900.00
-----------
MAX_SAL
35900.00
000100 THEODORE 86150.00
000120 SEAN 49250.00
000230 JAMES 42180.00
000300 PHILIP 37750.00
000330 WING 45370.00
200120 GREG 39250.00
200330 HELENA 35370.00
-----------
MAX_SAL
86150.00
...
...
|
CLPPlus supports titles, a useful feature for improving your report's appearance. Using titles along with page numbers and column options makes reporting much more attractive and informative. Multi-line titles, defined using a comma-separated title list, are also supported. You have the option to align the titles to the left, center, or right of the page, and you can use the skip option to leave blank lines after each title.
The following list details the types of titles supported in CLPPlus:
TTITLE: Page top title; displayed at the top of each page in a report.BTITLE: Page bottom title. LikeTTITLE, this is displayed at the bottom of each page in a report.REPHEADER: Report header; displayed once only at the beginning of the report.REPFOOTER: Report footer; displayed once only at the end of the report.
The example in Listing 9 shows the usage of
TTITLE and BTITLE in
a report. Line size is set to value 70 in order to align the title with
the table display. Here, top title is right-justified and bottom title is
center-justified.
Listing 9. Formatting column widths
SQL> ttitle right 'Employee Data'
SQL> btitle center 'IBM Corporation'
SQL> select empno, firstnme, lastname, edlevel, salary, bonus from emp ;
SQL> set linesize 70
Employee Data
EMPNO FIRSTNME LASTNAME EDLEVEL SALARY BONUS
------ ------------ --------------- ------- ----------- -----------
000010 CHRISTINE HAAS 18 152750.00 1000.00
000020 MICHAEL THOMPSON 18 94250.00 800.00
000030 SALLY KWAN 20 98250.00 800.00
000050 JOHN GEYER 16 80175.00 800.00
000060 IRVING STERN 16 72250.00 500.00
000070 EVA PULASKI 16 96170.00 700.00
000090 EILEEN HENDERSON 16 89750.00 600.00
000100 THEODORE SPENSER 14 86150.00 500.00
000110 VINCENZO LUCCHESSI 19 66500.00 900.00
000120 SEAN O'CONNELL 14 49250.00 600.00
000130 DELORES QUINTANA 16 73800.00 500.00
IBM Corporation
Employee Data
EMPNO FIRSTNME LASTNAME EDLEVEL SALARY BONUS
------ ------------ --------------- ------- ----------- -----------
000210 WILLIAM JONES 17 68270.00 400.00
000220 JENNIFER LUTZ 18 49840.00 600.00
000230 JAMES JEFFERSON 14 42180.00 400.00
000240 SALVATORE MARINO 17 48760.00 600.00
000250 DANIEL SMITH 15 49180.00 400.00
000260 SYBIL JOHNSON 16 47250.00 300.00
000270 MARIA PEREZ 15 37380.00 500.00
000280 ETHEL SCHNEIDER 17 36250.00 500.00
000290 JOHN PARKER 12 35340.00 300.00
000300 PHILIP SMITH 14 37750.00 400.00
000310 MAUDE SETRIGHT 12 35900.00 300.00
IBM Corporation
|
In the example below, TTITLE is defined with the
PGNO option, which causes the page number to be
displayed at the top of each page in the report.
Listing 10. Page numbers
SQL> TTITLE right 'PAGE NO: ' PGNO
SQL> select empno, firstnme, salary, edlevel from employee order by edlevel ;
PAGE NO: 1
EMPNO FIRSTNME SALARY EDLEVEL
------ ------------ ----------- -------
000290 JOHN 35340.00 12
000310 MAUDE 35900.00 12
200310 MICHELLE 35900.00 12
000100 THEODORE 86150.00 14
000120 SEAN 49250.00 14
000230 JAMES 42180.00 14
000300 PHILIP 37750.00 14
000330 WING 45370.00 14
200120 GREG 39250.00 14
200330 HELENA 35370.00 14
000250 DANIEL 49180.00 15
000270 MARIA 37380.00 15
000050 JOHN 80175.00 16
000060 IRVING 72250.00 16
000070 EVA 96170.00 16
PAGE NO: 2
EMPNO FIRSTNME SALARY EDLEVEL
------ ------------ ----------- -------
000090 EILEEN 89750.00 16
000130 DELORES 73800.00 16
000150 BRUCE 55280.00 16
000170 MASATOSHI 44680.00 16
000190 JAMES 50450.00 16
|
In this example, multi-line TTITLE
(comma-separated) is used in combination with
PGNO and column
NEW_VALUE variable.
NEW_VALUE variable, which can be defined on any
column, holds the new value for that column at any given point. Similarly
OLD_VALUE holds the last value for a column.
New and old value variables can be used with
TTITLE and BTITLE
respectively.
As shown in Listing 11,
TTITLE is displayed in two lines. The first
line displays the current page number, and the second line displays the
new value for EDLEVEL column. Similarly, BTITLE
displays old value for EDLEVEL column.
Listing 11. Variables in titles
SQL> break on edlevel skip 1
SQL> column edlevel new_value new_edlevel_value old_value old_edlevel_value
SQL> TTITLE RIGHT 'PAGE NO: ' PGNO skip , center 'Education Level : ' new_edlevel_value
SQL> BTITLE CENTER 'Education Level : ' old_edlevel_value
SQL> select edlevel, empno, firstnme, lastname, salary from employee order by edlevel;
PAGE NO: 1
Education Level : 12
EDLEVEL EMPNO FIRSTNME LASTNAME SALARY
------- ------ ------------ --------------- -----------
12 000290 JOHN PARKER 35340.00
12 000310 MAUDE SETRIGHT 35900.00
12 200310 MICHELLE SPRINGER 35900.00
*******
14 000100 THEODORE SPENSER 86150.00
14 000120 SEAN O'CONNELL 49250.00
14 000230 JAMES JEFFERSON 42180.00
14 000300 PHILIP SMITH 37750.00
14 000330 WING LEE 45370.00
14 200120 GREG ORLANDO 39250.00
14 200330 HELENA WONG 35370.00
*******
15 000250 DANIEL SMITH 49180.00
15 000270 MARIA PEREZ 37380.00
*******
Education Level : 15
PAGE NO: 2
Education Level : 16
EDLEVEL EMPNO FIRSTNME LASTNAME SALARY
------- ------ ------------ --------------- -----------
16 000050 JOHN GEYER 80175.00
16 000060 IRVING STERN 72250.00
16 000070 EVA PULASKI 96170.00
16 000090 EILEEN HENDERSON 89750.00
16 000130 DELORES QUINTANA 73800.00
16 000150 BRUCE ADAMSON 55280.00
16 000170 MASATOSHI YOSHIMURA 44680.00
16 000190 JAMES WALKER 50450.00
16 000200 DAVID BROWN 57740.00
16 000260 SYBIL JOHNSON 47250.00
16 000320 RAMLAL MEHTA 39950.00
16 000340 JASON GOUNOT 43840.00
16 200170 KIYOSHI YAMAMOTO 64680.00
16 200340 ROY ALONZO 31840.00
*******
17 000160 ELIZABETH PIANKA 62250.00
Education Level : 17
PAGE NO: 3
Education Level : 17
EDLEVEL EMPNO FIRSTNME LASTNAME SALARY
------- ------ ------------ --------------- -----------
17 000180 MARILYN SCOUTTEN 51340.00
17 000210 WILLIAM JONES 68270.00
17 000240 SALVATORE MARINO 48760.00
17 000280 ETHEL SCHNEIDER 36250.00
17 200240 ROBERT MONTEVERDE 37760.00
17 200280 EILEEN SCHWARTZ 46250.00
*******
18 000010 CHRISTINE HAAS 152750.00
18 000020 MICHAEL THOMPSON 94250.00
18 000140 HEATHER NICHOLLS 68420.00
18 000220 JENNIFER LUTZ 49840.00
|
Listing 12 shows how to use report headers and
report footers in a report. Unlike TTITLE and
BTITLE, these are displayed only at the
beginning and end of the report irrespective of the number of pages in the
report.
Listing 12. Using headers and footers
SQL> REPHEADER CENTER 'EMPLOYEE INFORMATION REPORT'
SQL> REPFOOTER CENTER 'END OF EMP INFORMATION REPORT'
SQL> select empno, firstnme, lastname, salary, bonus, edlevel from employee where edlevel
between 16 and 19;
EMPLOYEE INFORMATION REPORT
EMPNO FIRSTNME LASTNAME SALARY BONUS EDLEVEL
------ ------------ --------------- ----------- ----------- -------
000010 CHRISTINE HAAS 152750.00 1000.00 18
000020 MICHAEL THOMPSON 94250.00 800.00 18
000140 HEATHER NICHOLLS 68420.00 600.00 18
000160 ELIZABETH PIANKA 62250.00 400.00 17
000180 MARILYN SCOUTTEN 51340.00 500.00 17
000210 WILLIAM JONES 68270.00 400.00 17
000220 JENNIFER LUTZ 49840.00 600.00 18
000240 SALVATORE MARINO 48760.00 600.00 17
000280 ETHEL SCHNEIDER 36250.00 500.00 17
200010 DIAN HEMMINGER 46500.00 1000.00 18
200140 KIM NATZ 68420.00 600.00 18
200220 REBA JOHN 69840.00 600.00 18
200240 ROBERT MONTEVERDE 37760.00 600.00 17
200280 EILEEN SCHWARTZ 46250.00 500.00 17
14 rows were retrieved.
END OF EMP INFORMATION REPORT
|
After reading this article and following the examples presented, you should be able to start generating reports using the command line tool CLPPlus. This article has covered some of the important report formatting options, which should give you a clear and better understanding of report generation facilities in CLPPlus. There are several other options in CLPPlus that can be used to enhance the reports by formatting them and improving their readability. The server independence aspect of CLPPlus makes it consistent and hence minimizes tool learning effort for users across IBM data servers.
Learn
- DB2 for Linux, UNIX, and Windows page on developerWorks: Get the
resources you need to advance your DB2 skills.
- developerWorks
Information Management zone: Learn more about Information
Management. Find technical documentation, how-to articles, education,
downloads, product information, and more.
- Stay current with developerWorks technical events and webcasts.
Get products and technologies
-
DB2 Data Server Driver: Download the DB2 Data Server Driver
package to get CLPPlus.
-
DB2 for Linux, UNIX, and Windows: Download a free trial version
of DB2 9.7 for Linux, UNIX, and Windows.
- DB2 Express-C 9.7: Download DB2 Express-C 9.7.2, a fully licensed
product available at no-charge for the community.
- Build your next
development project with IBM trial
software, available for download directly from
developerWorks.
Discuss
- Participate in the discussion forum.
- The DB2 My developerWorks group is the place to interact and
collaborate with other DB2 users (all versions and all platforms).
- Participate in developerWorks
blogs and get involved in the My developerWorks
community; with your personal profile and custom home page, you
can tailor developerWorks to your interests and interact with other
developerWorks users.

Mangesh is a staff software engineer working with IBM India for past three and a half years. Since then he has been associated with Information Management tools development in India Software Lab. He has around five years of total experience in software product development. Currently he is working with the Common Application Development team, where he is developing CLPPlus reporting, scripting, and SQL compatibility features. He has a B.E. in computer Science from VTU Karnataka (India). In his free time, he loves to read books and watch movies. He is also an avid sports fan.




