Calendar of Posts

September 2010
M T W T F S S
« Aug   Jan »
 12345
6789101112
13141516171819
20212223242526
27282930  

Contributors

Paul Mayhew dbmsguy.com

Ivan Beg
A $25 loan can change a life forever

Inline database shell scripts using awk

One of the things I find very powerful in my day to day work as a DBA, is the use of inline shell/awk scripts.  I use this technique when I need to do the same thing to many objects.  I often use it when I want something quick and dirty or create a base script to work from.

An example might be when I want to do row counts on every table, or maybe set up a reorg on all the tables in a database.

Here is the command string you can use to do such a thing:

db2 connect to sample

db2 -x “select tabschema, tabname from syscat.tables where tabschema=’DB2INST1′ and type=’T'” | awk ‘BEGIN{print “db2 connect to sample”} {print “db2 -v \” select count(*) from “$1″.”$2″\”"}’ | ksh

The above inline script will select all the tables and return tablenames, then piping that into awk, we first print a connect statement and then print all the count statements.  All of this is then piped into a ksh.  The connect must be put there because a new instance of ksh is generated.

The base query can, of course, be customize to whatever suits your needs.

The pipe in to ksh is optional as you could just direct it to an output file and use it later and or make further modifications.

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>