16.10.2015 Views

Getting Started with DB2 Express-C

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

100 <strong>Getting</strong> <strong>Started</strong> <strong>with</strong> <strong>DB2</strong> <strong>Express</strong>-C<br />

parameters are not supported. For example, the commands shown in Listing 5.1 below are<br />

saved in a file called script1.db2.<br />

CONNECT TO EXPRESS;<br />

CREATE TABLE user1.mytable<br />

( col1 INTEGER NOT NULL,<br />

col2 VARCHAR(40),<br />

col3 DECIMAL(9,2));<br />

SELECT * FROM user1.mytable FETCH FIRST 10 ROWS ONLY;<br />

COMMIT;<br />

Listing 5.1 - A sample SQL Script stored in file script1.db2<br />

In the above script, all the statements are SQL statements and each statement is<br />

separated by a statement delimiter, in this case a semi-colon. The file name does not need<br />

to use the extension “db2”. Any extension could be used.<br />

5.9.1.1 Executing SQL Scripts<br />

An SQL script can be executed from either the Command Editor or the <strong>DB2</strong> Command<br />

Window on Windows, or through a Linux shell. To run the script in Listing 5.1 from the <strong>DB2</strong><br />

Command Window or Linux shell, you can use the following command:<br />

db2 -t -v -f script1.db2 -z script1.log<br />

or the equivalent one:<br />

db2 –tvf script1.db2 –z script1.log<br />

In this command:<br />

-t indicates that statements use the default statement termination character (a semicolon)<br />

-v indicates verbose mode; causing db2 to echo the command being executed<br />

-f indicates that the filename specified after this flag is the script file.<br />

-z indicates that the filename specified after this flag is used for appending screen<br />

output for later analysis (this is optional, but recommended)<br />

When the -t flag is used and no line delimiter is specified, the semi-colon is assumed to be<br />

the delimiter of the statements. There may be situations where another delimiter is<br />

required. For example, a script containing SQL PL code needs to use a different statement<br />

termination character other than the default (semicolon), because semicolons are used<br />

<strong>with</strong>in SQL PL object definitions to terminate procedural statements.<br />

For example, in the script file functions.db2 shown in Listing 5.2 below, you see it<br />

contains the statement to create a function, and a semi-colon is needed at the end of the<br />

SELECT statement because it is part of the syntax required <strong>with</strong>in the function. For the<br />

CREATE FUNCTION statement terminator we are using an exclamation mark (!). If we had

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!