From: Tim X on
Jeremy <jeremy0505(a)gmail.com> writes:

> Hi, real simple question and one I suspect the answer to which is "no".
>
> Can you create sqlplus scripts with "conditions" such that if for
> example a SQL statement returns a particular value or error condition
> then path A or path B is followed?

It depends a little on exactly what you mean.

If you are asking if a single specific sql script can have conditionals
inside it that react based on the results from some SQL, either DDL or
DML, and as a consequence perform additional SQL /within the same
session/, the short answer is no.

Generally, thre are two possible solutions you can use, both have some
drawbacks.

1. You can wrap your calls to sqlplus inside a shell script, such as a
bash script. The script can then capture the results from your sql and
use OS level commands i.e. bash built-ins, awk, sed, etc to process that
information and possibly execute another sqlplus session and script
based on what it finds. For very simple SQL, you can use shell 'here
documents', which will at least mean all your code is in one file.
However, here docs can get difficult to manage/maintain once they reach
a certain level of complexity and it is difficult to get any code reuse.
Alternatively, the sql is put in its own files, which makes it a bit
easier to maintain, but now you have multiple file dependencies, which
can be a source of bugs or more complex maintenance. A way to simplify
things a bit is to actually put the SQL in PL/SQL procedures/functions
and then just have small bits of SQL in your shell script that is
essentially a begin/end block that calls the pl/sql

Some of the limitations of this approach are

1. It cannot be used to do things like commits and rollbacks of
the previous sql because all the SQL runs in separate sessions.

2. It can be a little tricky to manage sensitive data, such as
passwords using this approach.

3. The scripts can begin to get difficult to manage quite quickly.
once you pass a certain level of complexity.

These days, my preferred approach is to use perl and the perl Oracle DBD
driver for perls DBI infrastructure. Going this route, you get a lot
more flexibility and have it all backed up with a far more powerful
scripting language than just a relatively simple shell script. The main
advantage are that all your sql can be executed inside a single oracle
session, so you can manage sessions and do rollbacks/commits based on
the results from other SQL. You can also call pl/sql procedures,
retrieve ref cursors and process them etc and you have access to all the
CPAN modules for ding things like generating reports, parsing data such
as dates, perl regexp, etc

The main disadvantages to this approach are

1. The perl DBI connection specification can be a little sensitive
to Oracle site changes. There are a number of different ways to
specify the connect string. Some methods appear to be more sensitive
to site changes than others and it can depend on whether your using
a straight TNS Listner config or something like Oracle names etc.

2. The Perl that comes bundled with Oracle tends to be quite old
compared to the version that you would normally find on a current OS
distribution.

3. Perl's ability to support multiple styles/ways of ding the same
thing can lead to maintenance issues if you have a large team and
everyone has their own perl 'style'.

Many of these limitations can be avoided through the use of Perl
modules. For example, I setup a perl OracleDB connection module.
Scripts that need to coonnect to our databases use this module. The DBAs
control who can access this module through a combination of file system
controls and a perl script registry module. i.e. the perl script must
have the right access permissions to load the module AND the name of
that script must be in a registry file managed by the DBAs (the script
name is the full pathname). This allows developers to use the scripts
and even update/modify them, but they do not know the passwords and
cannot obtain the passwords unless they can access files managed by the
DBAs. It also means that if we upgrade perl, Oracle etc, only one module
may need to be updated and all the scripts will continue to work.

We don't tend to use the perl that comes with Oracle. Instead, we use
the version that comes with the OS and build the Oracle DBD driver
ourselves. This allows us to run the latest code, so if we run into a
perl bug, we can report it and hopefully get action. Using the older
Oracle bundled versions, you tend to just get the standard "upgrade to
the latest version" for any bugs reported.

We also encourage people to use PL/SQL procedures/functions to
modularise SQL rather than have SQL coded into the perl scripts. While
this isn't always practicle, it does mean for frequent/common SQL tasks,
we have one central version to maintain rather than multiple copies in
various scripts.


Reading between the lines of your question, I would suggest you really
want to go the Perl route rather than the shell script route.

HTH

Tim




--
tcross (at) rapttech dot com dot au
From: Noons on
Jeremy wrote,on my timestamp of 17/01/2010 7:24 AM:
> In article <hit6ej$phc$4(a)solani.org>, gogala.mladen(a)gmail.com says...>
>> On Sat, 16 Jan 2010 18:37:24 +0000, Jeremy wrote:
>>
>>
>>> Can you create sqlplus scripts with "conditions" such that if for
>>> example a SQL statement returns a particular value or error condition
>>> then path A or path B is followed?
>> Yes. It's called PL/SQL and is available as of the version 6.
>
> Is that intended to be serious answer?
>
>

I think so. Mladen is usually spot-on in his Oracle statements. And a few other
areas!

PL/SQL would be my first choice as well: checking the return conditions of
individual SQL and taking action accordingly is a lot easier with PL/SQL than
with ksh, perl or other non-db tools. Assuming of course you want to continue
to execute db commands.

This is not to say it can't be done!

Entirely up to you which way to go, probably depending on what you feel
comfortable with or what you really need to do, now or in future.
From: Noons on
Mladen Gogala wrote,on my timestamp of 17/01/2010 10:15 AM:

> Nial Litchfield or Nuno Suoto
> as judges in this matter.

Souto. Not Suoto. Unless of course you are from PCWorld or Computerworld...

;)

<g,d&r>
From: Jeremy on
In article <hithhv$d19$4(a)solani.org>, gogala.mladen(a)gmail.com says...>
> On Sat, 16 Jan 2010 20:24:36 +0000, Jeremy wrote:
>
> > In article <hit6ej$phc$4(a)solani.org>, gogala.mladen(a)gmail.com says...>
> >> On Sat, 16 Jan 2010 18:37:24 +0000, Jeremy wrote:
> >>
> >>
> >> > Can you create sqlplus scripts with "conditions" such that if for
> >> > example a SQL statement returns a particular value or error condition
> >> > then path A or path B is followed?
> >>
> >> Yes. It's called PL/SQL and is available as of the version 6.
> >
> > Is that intended to be serious answer?
>
> Yes. That is precisely what PL/SQL is intended for: you run SQL and
> follow some logic path depending on the outcome.

Perhaps I assumed I had conveyed more of my requirements to the post
than I did.

It's about conditional execution of .sql files - am looking at ways to
automate install/upgrades which cannot be done using PL/SQL.

--
jeremy
From: Mladen Gogala on
On Sun, 17 Jan 2010 19:00:23 +1100, Noons wrote:


> Souto. Not Suoto. Unless of course you are from PCWorld or
> Computerworld...

I humbly apologize.



--
http://mgogala.byethost5.com