DB2 Samples

19 December 2008


There have always been some samples in DB2 for some things like user exits and some code stubs, but as of DB2 v9.5 we are really getting serious about it.  There are now many samples for application development and administration.  The samples are not yet all in one place, but I am writing this article to summarize what is available and where to find them.  Even if you are running an older version of DB2, many of these samples will be useful to you because they exploit features of DB2 that have been there for a long time. 


Where are these samples?






Information Center

By far the most complete set of samples is in the DB2 v9.5 Information Center site.  These samples are great for both the administrator and the application developer.  For the DBA, there samples for lots things that you might want to see done such as


There are tons of these types of samples in the information center.  Most of them are in the “C” language category of the page listed above and some are on the CLP section.  These samples are operating system specific.  For the application developer these scripts are also useful.  For the language that interests you like C, C#, Java, PHP, Perl, C++, Perl, Several Visual Basic.Net languages, and others you can get lots of samples for writing code.  Helpful libraries are also available.  There are also specific examples of writhing SQL Stored Procedures.  Examples include:

So anytime that you are doing something new, I recommend looking for a sample to save time.  By the way, the Information Center also has XML code samples.



Developerworks Exchange

This is the up and coming place for samples.  It is envisioned as a community page where anyone can add scripts and rate scripts that are already there.  So if you have a script that you have developed that you think others would find useful, please post it here.  This will not get that much better if you wait for only IBMers to add stuff.  Currently there are several scripts for using our new pureXML feature, and a set of administration scripts.  These scripts mostly demonstrate how to use some of our newer features such as:



DB2 Install Sample Directory

There are many samples that get installed when you install DB2 v9 on your server or desktop.  Most, if not all of the scripts contained in the Information Center samples should be in the sample directory of your DB2 install.  I believe that there are additional samples as well.  The DBA should look at the directory sample/admin_scripts for several administration scripts including

Truncate table

Event Monitor


Roll-in data for partitioned table

Roll-out data for partitioned table

Automatic Maintenance functions (v9.5)

Many more….


The sample directory also has samples of triggers and stored procedures as well as many of the samples noted above for writing DB2 calls in the various programming languages.  Again you can find the sample directory under the DB2 software install directory or in the instance home directory as follows:



Sample Database

Do you want to try out some of the samples, but don’t have a database where you can try them?  DB2 comes with a script to create a small test database with data.  The Information Center page with instructions for creating the sample database and descriptions of the tables can be seen by clicking the heading of this paragraph.



Finally, if I can’t find a script for what I want to do in any of these places, I often find that someone has posted one on the web somewhere and I can find it with Google.  I highly recommend this route as well.


HOME | Search