tags:

views:

257

answers:

4

Hello.

I'm attempting to run the following command in Korn Shell (ksh):

set -A INDEXES `db2 "describe indexes for table ${TABSCHEMA}.${TABNAME} show detail" | awk '{print $1"."$2}'`

What I'm attempting to achieve is place a list of the indexes over a particular table into an array which I can later iterate through.

The problem is, when I run the above command the contents of the array starts with the error message of 'SQL1024N' (which is telling me that the database connection does not exist).

However, if I remove the 'awk' at the end of the statement as so:

set -A INDEXES `db2 "describe indexes for table ${TABSCHEMA}.${TABNAME} show detail"`

it works just fine (well, to the extent its returning data. Obviously without the awk I'm not capturing the correct data).

Does anyone know why the awk is having this affect?

I appreciate there is more than one way to get this data, but it baffles me as to why this is happening.

Thanks in advance.

Greg.

+1  A: 

I doubt it's awk per se. Maybe db2 is particular about stdout being connected to a tty or console? Or at least doesn't like when it's connected to a pipe.

PEZ
As a test, what happens when you run "db2 ... | awk ..." as a plain command on the command line? Also, get used to using $(...) instead of backticks; it is much easier to manage quoting. Since the backticks capture the output (via file or pipe) it is not clear that piping to awk matters.
Jonathan Leffler
Hi JonathanSorry for the delayed response. I've only just spotted this comment.When the "db2 | awk" command is run outside of the backticks it works as expected.
greggannicott
A: 

Hi,

This is an unusual one, and as Pez says it is probably a db2 quirk.

I have seen similar issues when, e.g. using time or timex prior to a db2 command string, where db2 does not have a defined database to connect to.

There is an environment variable DB2DBDFT which sets a default database for implicit connections. I am sorry to say that I'm not sure if this variable is available to non-DBA users (presuming you're an app developer). It is worth investigating if setting this variable via:

db2set DB2DBDFT=${your_db_name}

And retrying your query.

If you have multiple databases your environment could connect to you can unset the DB2DBDFT variable once you have completed your work.

Mark S
Thanks for your answers on this. I'll speak to our DBAs and mention what you've suggested. When I first showed this to our DBAs they just kind of shrugged their shoulders.With that in mind, thanks again for taking the time to answer.
greggannicott
A: 

Try the db2 command with the -x switch:

db2 **-x** "describe indexes for table ${TABSCHEMA}.${TABNAME} show detail"....

I've had instances where this has cured my inability to pipe output to awk.

lt4ryan
A fair point. Unfortunately I started out with the -x switch. For some reason I didn't include it in my example above. Thanks for trying though!
greggannicott
+1  A: 

In this case, when the DB2 CLP says that it's not connected to the database, it's because the shell has opened up a sub-process that requires its own dedicated db2bp backend process, which cannot access the connection opened by the original shell process. It's not that something is becoming disconnected, it's that a newly created shell process (and its accompanying db2bp process) are being created but aren't being told to connect to a database. One way to remedy this is to explicitly connect (or re-connect) to the database when you know you're in one of those situations.

set -A INDEXES `db2 connect to watevrDB >/dev/null;db2 -x describe indexes for table ${TABSCHEMA}.${TABNAME} show detail | awk {'print $1"."$2'}`

I realize that this question is more about scripting and awk with DB2 than about the system catalog, or else I would have recommended some straightforward catalog queries to produce the same result.

Fred Sobotka
Excellent! That's worked a treat thanks, Fred.
greggannicott
Just as an aside: my dirty work-around to this attempt (which in itself could well be classed as a dirty work-around) was to run db2look and using an awk to simulate a multi-line grep (something that can't be done in ksh using grep) I managed to get the list of indexes.
greggannicott
The db2look utility is a good way to generate full DDL for objects, but if you're just interested in what the indexes are about, the SYSCAT.INDEXES view has a lot of useful detail.
Fred Sobotka