You are on page 1of 23

®

IBM Software Group

Lesson 3: Tools & Scripting


Part 2 of 2
(DB2 on Campus Lecture Series)

Raul F. Chong
IBM Toronto Laboratory

© 2005 IBM Corporation


IBM Software Group | DB2 Information Management Software

Agenda

ƒ Introduction
ƒ Control Center
ƒ Command Editor
ƒ Command Window / CLP
ƒ Task Center
ƒ Journal
ƒ Health Center
ƒ Scripting

2
IBM Software Group | DB2 Information Management Software

Command Window
Only on Windows. On Linux use the Linux shell
Can be started with “db2cmd”

3
IBM Software Group | DB2 Information Management Software

Command Line Processor (CLP)


Available on all DB2 servers and clients
The prompt starts with “db2 =>”

4
IBM Software Group | DB2 Information Management Software

Agenda

ƒ Introduction
ƒ Control Center
ƒ Command Editor
ƒ Command Window / CLP
ƒ Task Center
ƒ Journal
ƒ Health Center
ƒ Scripting

5
IBM Software Group | DB2 Information Management Software

Task Center

6
IBM Software Group | DB2 Information Management Software

Task Center – No Tools Catalog Warning Dialog


ƒ Task Center requires that a tools catalog be created
 This catalog stores all the meta data and information about scheduled tasks
ƒ If you did not create the tools catalog when the database was created, an error
message dialog will be displayed like the one below when Task Center is launched
 In order to schedule tasks, you must first create the tools catalog
(you can do this when you schedule the first task)

7
IBM Software Group | DB2 Information Management Software

Scheduling With Task Center

ƒ Any type of script can be scheduled using Task Center (whether or not
it was created through a DB2 GUI tool)
ƒ The advantage of creating tasks through a DB2 GUI tool is that they
can be edited again later using the original GUI tool used to create
them
ƒ Tasks are run at their scheduled time from the system where you
created the tools catalog, BUT run on the system where the database
exists
 No dependencies on the client where the task was created
ƒ View previous executions of your tasks in the Journal

8
IBM Software Group | DB2 Information Management Software

Agenda

ƒ Introduction
ƒ Control Center
ƒ Command Editor
ƒ Command Window / CLP
ƒ Task Center
ƒ Journal
ƒ Health Center
ƒ Scripting

9
IBM Software Group | DB2 Information Management Software

Journal

ƒ A DBA’s activity journal in online form

ƒ You can view:


Task History: All attempted scheduled tasks and their success
status
Database History: A record of database activities performed
(backup, restore, REORGs, etc.)
Message: History of messages returned by DB2 tools. This is useful
if you want to recall and compare old error messages, or if you
close a dialog box too quickly or by accident
Notification Log: contains system-level message. Critical errors are
recorded here

10
IBM Software Group | DB2 Information Management Software

Journal

Select what events


to view

Select database
and system

View more details


by double-clicking
on an entry

11
IBM Software Group | DB2 Information Management Software

Agenda

ƒ Introduction
ƒ Control Center
ƒ Command Editor
ƒ Command Window / CLP
ƒ Task Center
ƒ Journal
ƒ Health Center
ƒ Scripting

12
IBM Software Group | DB2 Information Management Software

Health Center

Customize health
indicator settings

Select what types


of alerts to use

Choose screen
refresh time

Double click any


alerts that appear
for more details

13
IBM Software Group | DB2 Information Management Software

Configuring Health Alert Notification

Health Center > Health Center menu > Configure > Alert Notification

14
IBM Software Group | DB2 Information Management Software

Agenda

ƒ Introduction
ƒ Control Center
ƒ Command Editor
ƒ Command Window / CLP
ƒ Task Center
ƒ Journal
ƒ Health Center
ƒ Scripting

15
IBM Software Group | DB2 Information Management Software

Scripting
ƒ There are two general forms of scripting:
SQL scripts
Operating system (shell) scripts
ƒ SQL Scripts
Simple to understand
Platform independent
Script parameters not supported
ƒ Operating system scripts
Greater flexibility
Additional logic possibilities
Supports parameters/variables
Platform-dependent
16
IBM Software Group | DB2 Information Management Software

A Basic SQL Script

ƒ Suppose the following commands are saved in a file called


script1.db2

CONNECT TO EXPRESS;

CREATE TABLE user1.mytable


(col1 INTEGER NOT NULL,
col2 VARCHAR(40),
col3 DECIMAL(9,2));
SELECT * FROM user1.mytable FETCH FIRST 10 ROWS ONLY;
COMMIT;

17
IBM Software Group | DB2 Information Management Software

Executing SQL Scripts


ƒ An SQL script can be executed from Command Editor or the operating
system command line
ƒ To run the previous script from the command line (DB2 Command
Window), you would use the following command:
db2 –t –v –f script1.db2 –z script1.log

-t indicates statements use the default statement termination character


(semicolon)
-v indicates verbose mode; causes db2 to output the command being
executed
-f indicates the following filename contains the SQL statements
-z indicates the following message filename should be used for
appending screen output for later analysis (optional, but
recommended)
Note: It is a good idea to delete these message files before the
execution of DB2 scripts so that output from a previous script execution
is not mixed with output from the current script execution

18
IBM Software Group | DB2 Information Management Software

When a different statement termination character is


needed
ƒ A script containing SQL PL code needs to use a different statement termination
character other than the default (semicolon)
 This is due to the fact that semicolons are used within SQL PL object definitions to
terminate procedural statements
ƒ Delimit the end of each SQL PL application object with a different statement
termination character
 e.g.
CREATE FUNCTION f1()
….
END!
CREATE FUNCTION f1()

END!
ƒ When executing the script, inform DB2 that a different statement termination
character is being used:
db2 –td! –v –f functions.db2 –z functions.log

19
IBM Software Group | DB2 Information Management Software

A Simple Operating System (Shell) Script


ƒ Suppose the following statements are saved in a file called
create_database.bat

set DBPATH=c:
set DBNAME=PRODEXPR
set MEMORY=25
db2 CREATE DATABASE %DBNAME% ON %DBPATH% AUTOCONFIGURE USING
MEM_PERCENT %MEMORY% APPLY DB AND DBM
db2 CONNECT TO %DBNAME% USER %1 USING %2
del schema.log triggers.log app_objects.log
db2 set schema user1
db2 –t –v –f schema.db2 –z schema.log
db2 –td@ -v –f triggers.db2 –z triggers.log
db2 –td@ -v –f functions.db2 –z functions.log

ƒ To execute this script from the command line, you would issue
the following command:
create_database.bat db2admin ibmdb2

20
IBM Software Group | DB2 Information Management Software

Technical Articles About DB2 Scripting

ƒ An Introduction to DB2 UDB Scripting on Windows


http://www.ibm.com/developerworks/db2/library/techarticle/0307fierros/03
07fierros.html
ƒ More scripting with DB2 UDB for Windows
http://www.ibm.com/developerworks/db2/library/techarticle/dm-
0406fierros/index.html
ƒ DB2 UDB Scripting with Windows Scripting Host
http://www.ibm.com/developerworks/db2/library/techarticle/dm-
0311fierros/index.html
ƒ Advanced SQL PL Scripting
http://www.ibm.com/developerworks/db2/library/techarticle/0203yip/0203y
ip.html
ƒ Yet Another Article on Advanced Scripting
http://www.ibm.com/developerworks/db2/library/techarticle/0211yip/0211yip4.html

21
IBM Software Group | DB2 Information Management Software

QuickLab #5 – Creating An Installation Script

ƒ In this QuickLab, you will create a simple installation script for the
EXPRESS database and its objects
ƒ Refer to the QuickLab #5 instructions

22
IBM Software Group | DB2 Information Management Software

What’s Next?

Lesson 1 Installation & the DB2 editions


Lesson 2 The DB2 Environment
Lesson 3 Tools & scripting COMPLETED!
Lesson 4 The DB2 architecture
Lesson 5 Client connectivity
Lesson 6 Working with data objects

Lesson 7 Data movement


Lesson 8 Security
Lesson 9 Backup & recovery
Lesson 10 Maintenance

23

You might also like