4.2. The Command Line Tools
All DB2 operations are invoked by DB2 commands or SQL statements. For example, to back up a database, you use the BACKUP DATABASE command. To create a table, you use the CREATE TABLE SQL statement. All of these commands and SQL statements can be entered using the command line tools.The command line tools consist of the Command Line Processor (CLP), the Command Window (Windows platform only), and the Command Editor. Since they are command driven, you must have some knowledge of DB2 commands and SQL statements to use them.DB2 commands to refer to both types of commands: DB2 system commands and DB2 CLP commands. When a section is only applicable to a given type of command, it will be explicitly indicated. Refer to section 2.1, SQL Statements and DB2 Commands, for an explanation about the differences between these two types of commands.
4.2.1. The Command Line Processor and the Command Window
The DB2 CLP and the DB2 Command Window are text-based tools used to interact with the DB2 engine. Figure 4.2 shows the relationship between the CLP and the Command Window.
Figure 4.2. The Command Line Processor versus the Command Window
Figure 4.1). Alternatively, to invoke the Command Window from an MS-DOS window, issue the command db2cmd . This command spawns another window which displays DB2 CLP in the title bar. Note that the Command Window looks like any MS-DOS window except for this title bar.From a MS-DOS window, you can perform operating system commands and DB2 system commands but not DB2 CLP commands or SQL statements. However, you can perform all of these from a Command Window.For example, as shown in Figure 4.2, from the MS-DOS window you can execute:
• Operating system commands: | dir |
• DB2 system commands: | db2start |
• DB2 CLP command: | db2 list applications |
• SQL statements: | db2 SELECT * FROM department |
as illustrated in Figure 4.3. The figure also shows how the same statement works from the Command Window after it is invoked with the db2cmd command.
DB21061E Command line environment not initialized
Figure 4.3. Invoking the Command Window from a MS-DOS command prompt
[View full size image]

We recommend that you learn how to use the Command Line Processor, as it is the common tool available with all DB2 versions and clients.
4.2.1.1 Methods to Work with the CLP
There are three ways to issue a DB2 command or SQL statement with the CLP: interactive mode, non-interactive mode, and non-interactive mode using a file as input. These methods are discussed in the following sections.
Method 1: Interactive Mode
You start the CLP in interactive mode by clicking on Start > Programs > IBM DB2 > Command Line Tools > Command Line Processor (see Figure 4.1). Alternatively, from the Command Window or Linux/UNIX shell, you start the CLP in interactive mode by entering db2 and pressing Enter as shown in Figure 4.4.
Figure 4.4. Figure 4.4 The Command Line Processor in interactive mode
Chapter 5 for information about DB2 registry variables).
r 3
e 5
Figure 4.5. Examples of CLP commands in interactive mode
[View full size image]

Method 2: Non-interactive Mode
To work with the CLP in non-interactive mode is equivalent to working with the DB2 Command Window (on Windows) or the Linux/UNIX shell. If you start the CLP in interactive mode, entering the quit command takes you to the CLP in non-interactive mode. In this mode you need to prefix the CLP command or SQL statement with the db2 executable. For example:
Using this method you can execute operating system commands in addition to DB2 commands and SQL statements from the same window or session.NOTEReferences to the Command Window and the CLP on Windows platforms are sometimes used interchangeably in DB2 books to indicate the use of a command line interface as opposed to a GUI tool.Many DB2 users prefer to work in this environment because they can use some shortcut key strokes, such as pressing the up arrow key to repeat the last commands on Windows, or to take advantage of operating system mechanisms like piping the output of the CLP to the more command on Linux and UNIX to display the output in portions.Every time you issue the db2 executable, a "CLP session" is created where a front-end process is invoked. This takes the rest of the statement as input and then closes the process. For example, when you issue:
db2 connect to sample
db2 list applications all
db2 select * from employee
db2 invokes a CLP front-end process that takes list db directory as input. Once the CLP digests this command, it implicitly issues the quit command to end the CLP front-end process. The front-end and back-end processes are discussed in more detail later in this chapter.Figure 4.6 shows the CLP in non-interactive mode.
db2 list db directory
Figure 4.6. The Command Line Processor in non-interactive mode
[View full size image]

you may receive the following error message, since the asterisk (*) is a wildcard character:
db2 select * from employee
Use double quotes to avoid parsing errors:
SQL0104N An unexpected token "*" was found following "select "
A more deceiving example occurs when you use the greater than (> ) character. For example, the statement:
db2 "select * from employee"
will be first parsed by the operating system, which will interpret > 10000 as the redirection of the output to the file 10000 . After executing the above statement, your current directory will have a new file with the name 10000 containing a DB2 syntax error message because only select lastname from employee where salary was passed to DB2. Again, to resolve this problem, make sure to enclose the statement in double quotes:
db2 select lastname from employee where salary > 10000
db2 "select lastname from employee where salary > 10000"
Method 3: Non-interactive Mode Using a File as Input
The CLP can use a file containing one or more CLP commands or SQL statements and process them one after the other. This is ideal to develop DB2 database scripts. For example, Figure 4.7 shows the contents of the file myInput.txt , which we will use as input to the CLP.
Figure 4.7. Input file to be used by the CLP

Figure 4.8. Invoking the CLP in non-interactive mode using a file as input
[View full size image]

If you prefix each of the CLP commands with db2 (the CLP executable) in a file and remove the terminator characters, you are effectively converting this file into an operating system script rather than a DB2 script. Depending on the operating system, you may have to make additional modifications. For example, on Windows, you need to use rem for comments. You may also need to change the file name so that the .bat extension is used. Figure 4.9 shows this for the file myOS_Input.bat .
DB21007E End of file reached while reading the command.
Figure 4.9. Invoking DB2 CLP commands and SQL statements in a Windows script file

Figure 4.10 shows the same script for a Linux or UNIX platform.
chmod +x myOS_Input.txt
Figure 4.10. Invoking DB2 CLP commands and SQL statements in a Linux/UNIX script file
[View full size image]

DB2 scripts do not accept parameters, but operating system scripts do. In other words, if you need to invoke your scripts with parameters, you need to use operating system scripts.
4.2.1.2 CLP Command Options
The CLP is just another program designed to interact with DB2. Like many other programs, the CLP has been designed to accept several parameter options. The CLP command list command options displays the available CLP command option parameters (see Figure 4.11).
Figure 4.11. CLP command options
[View full size image]

After you execute this command, a COMMIT statement is automatically issued because autocommit is enabled. (As you can see in Figure 4.11, the Auto-Commit option was already on by default, so including -c in the above example is not necessary.)To disable autocommit, invoke the CLP as follows:
db2 -c insert into employee (firstnme) values ('Raul')
Note that specifying a command option in the db2 command applies only to that session of the CLP. Issuing the db2 command without an option will use the default command option values, or the ones contained in the DB2OPTIONS registry variable, which we discuss later in this section.You can also change a command option when working with the CLP in interactive mode using the following command:
db2 +c insert into employee (firstnme) values ('Raul')
Figure 4.12 shows an example where the v option (verbose) is used. This option causes the command or statement to be repeated or echoed when executed as discussed earlier. In Figure 4.12, note that the SELECT * FROM department statement is echoed.
update command options using option value option value ...
Figure 4.12. The CLP in interactive mode
[View full size image]

the DB2OPTIONS registry variable is set so that any command executed will be echoed (-v option), and the output will be spooled in the file myfile.log (-z myfile.log option). The changes take effect immediately for the current session and any other new CLP sessions that you start.To reset the values to the default, issue this command:
db2set db2options="-v -z myfile.log"
DB2 registry variables are explained in detail in Chapter 5, Understanding the DB2 Environment, DB2 Instances, and Databases.
db2set db2options=
4.2.1.3 Obtaining Help Information from the CLP
One of the most useful CLP commands is the help command represented by a question mark (? ). This command provides help on SQL error codes (SQLCODE), DB2 messages, and CLP command syntax. For example:
In addition, using the help command by itself displays the entire list of CLP commands, as shown in Figure 4.13.
db2 ? SQL0104N
db2 ? DB21004E
db2 ? list applications
Figure 4.13. Output of the command db2 ?
[View full size image]

Figure 4.14. The CLP help (?) command
[View full size image]

4.2.1.4 Line Continuation
There are two ways to use line continuation from the CLP: with the backslash character and with the delimiter terminator character.
Method 1: Using the Backslash (\) Character
You can use the backslash (\ ) character in either interactive or non-interactive mode. Figure 4.15 shows an example using interactive mode first, followed by a non-interactive mode example.
Figure 4.15. Line continuation in the CLP using the backslash continuation character
[View full size image]

db2 (cont.) =>
Method 2: Using a Delimiter Terminator Character with the CLP in Interactive Mode
Using this method, the CLP is invoked in interactive mode using the terminator delimiter option. For example:
After entering this command and pressing Enter, the CLP is invoked in interactive mode. You can wrap commands onto multiple lines until you type the terminator character, which is the exclamation mark (! ) in the example shown in Figure 4.16.
db2 -td!
Figure 4.16. Line continuation in the CLP using a delimiter termination character in interactive mode
[View full size image]

After you copy and paste the statement into the CLP, enter the terminator character and press Enter to execute it.
select * from staff
where name = 'Edwards'
and job = 'Sales'
4.2.1.5 The CLP Front-end and Back-end Processes
The CLP has both front-end and a back-end processes. The front-end allows you to perform actions without connecting to a database. For example, issuing the command:
does not require a connection to a database. Depending on the operation, the instance need not be started either.The back-end process is needed when you perform actions against a database. The back-end process is created when you connect to the database in a CLP session and can be identified by the application name db2bp . Figure 4.17 shows the output of the list applications command, which shows this process (or thread in Windows) indicating a connection to the sample database.
db2 list db directory
Figure 4.17. The CLP back-end process
[View full size image]

![]() | 4.2.2. The Command Editor |
The Command Editor is the graphical user interface (GUI) version of the Command Line Processor. The Command Editor offers several other functions in addition to those provided by the CLP and the Command Window.
- The ability to execute multiple DB2 commands and SQL statements interactively and simultaneously. With the CLP, only one command at a time can be executed interactively. If you want to execute multiple commands, you have to save them in a text file and execute the file with the -f option as explained in section 4.2.1.1, Methods to Work with the CLP.
- The ability to save all the commands you typed in the Command Editor window to a file or as a task to be executed from the Task center.
- The ability to display a Visual Explain output of the access path chosen by the DB2 optimizer for an SQL statement. The Visual Explain tool is discussed in the next section.
- The ability to display results in well-formatted tables.
section 4.4, General Administration Tools.) Alternatively, the command db2ce starts it from a command line prompt.Figure 4.18 shows the start screen of the Command Editor. The Target field is empty until you click on the Add button, which displays the Specify Target window. Select a Target type from the pull-down menu, and the database you want to work with in the Available targets pull-down menu.
Figure 4.18. The Command Editor lets you choose the database you want to work with
[View full size image]

Figure 4.19. The Commands view in the Command Editor
Access Plan button

If you chose to execute the command, the results are nicely displayed in a table in the Query Results view, as shown in Figure 4.20.
Figure 4.20. The Query Results view in the Command Editor
Row button respectively. All of these changes are not permanent unless you click on the Commit button. If you want to cancel your changes, click on the Roll Back button.If you chose either of the other two options that produce the access plan, a tool called Visual Explain is invoked. The next section explains this tool in more detail.
4.2.2.1 Visual Explain
access plan, and the particular component of DB2 that makes this decision is called the DB2 optimizer , which can be considered the "brain" of DB2. To analyze the access plan, you can use text-based tools like the db2exfmt and the db2expln command line tools; however, it is often useful to display this information in a graph. Use Visual Explain to graphically display an access plan for a query.You can invoke Visual Explain from the Command Editor. Simply enter an SQL statement in the Command Editor's Command Input window, and then press either the Execute and Access Plan button or the Access Plan button. Chapter 16, Database Performance Considerations, for more details.