Re: if EXISTS in ksh script
Posted in 1998
Paul Watson at WF Software wrote:
> dboling@vnet.net (Don Boling) wrote:
>
> > I am trying to test if a table exists in a korn shell script
> > before I execute "create table (...) "
> I like
>
> EXIST=`dbaccess ${DBNAME} -<<EOF 2>/dev/null | awk '/ [0-9]/{print $1}'
> select count(*)
> from systables
> where tabname = "{$TABNAME}"> EOF
> `
> if [ ${EXIST} ]
> then
> else
> fi
It isn't really fair of me to pick on Paul's answer as at least two
other answers suffer from the same defects. The code given will not
work in a MODE ANSI database because the system catalogue tables
must be identified by owner and name. The solutions can also break
down in a MODE ANSI database if there is more than one table with
the same name but different owners.
I tend toward the view that the simplest way to find out if a table
exists is to try creating it -- the create will fail if the table
exists. Depending on what the requirements are, you could just drop
the table in case it exists, ignoring the error if it doesn't exist.
By default, DB-Access continues after an error (though I think that
may be configurable in the very latest versions).\\, so you might be
able to put the drop ahead of the create statement. If you don't want
to create the table again if it already exists, then simply try the
new create and see what happens.
--
Jonathan Leffler (jleffler@informix.com, jleffler@earthlink.net)
Guardian of DBD::Informix v0.60 -- see http://www.perl.com/CPAN
#include <disclaimer.h>