DB2 Command Line Processor Tips and Tricks by Paul C. Zikopoulos and Roman B. Melnyk If you are an experienced database administrator (DBA) you likely use a mix of graphical user interface (GUI) tools and the IBM® DB2® Universal Database™ (DB2 UDB) command line processor (DB2 CLP) to accomplish your day-to-day tasks. In some cases, the difference between using a GUI or the CLP is a cultural one: Windows®-based DBAs prefer to use a GUI interface (for example, the DB2 Control Center), whereas Linux and UNIX®based DBAs often choose the DB2 CLP. When business catch phrases like "time to value" or "flattened learning curves" find their way into every aspect of a vendor's marketing campaign, they always seem to revolve around the product's GUI. Sometimes lost, though, are the little things that could help increase the productivity and efficiency of DB2 CLP users. Since the release of DB2 UDB Version 8.1, there have been some ease-of-use enhancements to the DB2 CLP. The ability to customize the DB2 CLP (interactive mode) was added in FixPak 1. The DB2 CLP was further enhanced in Version 8.1.2 by adding plug-ins for your favorite text editors, as well as a command cache. This article will detail these new features. Customizing the DB2 CLP in Interactive Mode DB2 UDB Version 8.1 with FixPak 1 adds the ability to have customizable DB2 CLP interactive prompts. You can use this feature to reflect the context of the current instance attachment and database connection, and is a neat way for DBAs to quickly and easily identify characteristics of their current attachment. In the past, DBAs would have to repeatedly invoke CLP commands (such as GET INSTANCE) to recall this information. The DB2 CLP interactive prompt was hard coded and could not be changed, as shown below.
The new customization feature allows you to have the prompt reflect the current user, current instance attachment, and database connection, among others. The following example shows a customized interactive DB2 CLP prompt that identifies user PAULZ, who is attached to the DB2 instance and connected to the SAMPLE database.
To customize the command prompt, a new DB2 UDB registry variable called DB2_CLPPROMPT was introduced. The DB2_CLPPROMPT registry variable can be set to any text string that is 1 to 100 characters long. The customized string can contain optional tokens that will be replaced at run time. The
information in the prompt is always displayed in uppercase characters, regardless of how the options are configured, and if you change the settings, the new values will not take effect until you restart the CLP. You can set the DB2_CLPPROMPT registry variable in the DB2 UDB Profile Registry using the DB2SET command, as shown below:
The following table lists the tokens that you can set:
The DB2 CLP Command Cache In DB2 UDB Version 8.1.2, a DB2 CLP command cache was added to the interactive runtime environment. This neat feature gives you the ability not only to recall commands that you have entered in an interactive DB2 CLP session, but to run them as well. This feature is implemented through two new DB2 commands: HISTORY and RUNCMD. The HISTORY and RUNCMD Commands The HISTORY command lets you access the contents of a cache, which, by default, holds up to twenty 20 recently run commands in the same interactive DB2 CLP session. The commands are returned with corresponding sequence numbers that show the order in which the commands were run. The most recently run command is listed last. The size of the command history cache can be controlled using the DB2_CLPHISTSIZE DB2 registry variable. The range of permissible values is 1 to 500, inclusive. If you set this variable to a value that is outside of this range, the DB2 UDB CLP will use the default size of 20. The syntax of the HISTORY command is shown below:
For example, note the order in which the commands in the following session were entered:
In the command cache, they are assigned sequence numbers as follows: db2 db2 db2 db2
connect to sample [1] select * from staff where dept = 20 [2] connect reset [3] create database Melnyk [4]
You can verify this by entering the HISTORY command:
You can use the REVERSE keyword to reverse the order in which the commands are listed:
You can run any command in the command cache using the RUNCMD command. The syntax of this command is shown below:
The following example shows you how to run a command from the cache:
If the option is not specified, the default behavior is to run the most recent command. The command history does not persist from session to session; if you leave the prompt, the history is lost. If you start two separate interactive CLP sessions, each session's command history is stored in a separate cache. Interactive CLP Roundtrip Editing DB2 UDB Version 8.1.2 adds the capability of "roundtrip editing" to the DB2 CLP interactive mode. This integration allows you to run DB2 UDB commands, edit them (using the operating system's integrated command editor or an editor of your choice), and then run the edited commands. To use this feature, invoke the new DB2 interactive CLP command called EDIT. The syntax of this command is shown below:
The option is used to retrieve the sequence number of the command in the command cache. (See the previous section for more information.) If you do not specify this option, the last executed command is loaded into the editor. If you elect to save your changes when you close the editor, the updates will be reflected in the DB2 UDB interactive CLP prompt, and you can run the command immediately if you wish. If you typed multiple commands in the editor, only the first command is retrieved into the CLP interactive session.
The following four screens illustrate the process of running, editing, and rerunning an edited command using the local operating system's text editor:
There are several ways to specify which editor will be invoked when you use the EDIT command. Running this command launches the appropriate editor based on the following ordered criteria: 1. If the EDITOR <editor> option is specified in the EDIT command, the editor specified by <editor> will be used.
2. If the DB2_CLP_EDITOR registry variable is set, the editor specified by this variable will be used. 3. If the VISUAL environment variable is set, the editor specified by this variable will be used. 4. If the EDITOR environment variable is set, the editor specified by this variable will be used. 5. Otherwise, the default Notepad editor on Windows operating systems or the VI editor on UNIX-based systems will be used. Conclusion DB2 UDB Version 8.1 with FixPak 1 and DB2 UDB Version 8.1.2 introduced new usability features for the DB2 interactive CLP, including customizable DB2 CLP interactive prompts and a command cache that can be accessed or modified through the new DB2 CLP HISTORY command, the RUNCMD command or the EDIT command. About the Authors Paul C. Zikopoulos, BA, MBA, is with IBM Canada Ltd. Paul has written numerous magazine articles and books about DB2. Paul has co-authored the books: DB2 Version 8: The Official Guide, DB2: The Complete Reference, DB2 Fundamentals Certification for Dummies, DB2 for Dummies, and A DBA's Guide to Databases on Linux. Paul is a DB2 Certified Advanced Technical Expert (DRDA and Cluster/EEE) and a DB2 Certified Solutions Expert (Business Intelligence and Database Administration). You can reach him at: paulz_ibm at msn.com. Roman B. Melnyk, PhD, is with IBM Canada Ltd., specializing in database administration, DB2 utilities, and SQL. Roman has written numerous DB2 books, articles, and other related materials. Roman co-authored DB2 Version 8: The Official Guide, DB2: The Complete Reference, DB2 Fundamentals Certification for Dummies, and DB2 for Dummies. You can reach him at roman_b_melnyk at hotmail.com.