Error Handling

We are using a korn shell script and just calling the sql scripts in the following manner

i\ /tmp/anything1.sql
i\ /tmp/anything2.sql

I want to have some error handling that writes to a log file and then moves to execute the next file. Can anyone assist on how to handle?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What sql flavor?

Oracle -> Use "spool" + "whenever sql error" commands
earngreenAuthor Commented:
Mmmm, don't know posgres. What is the "i\" part? an executable?

To check for return codes, do this:
exec >/tmp/anything.log
i\ /tmp/anything1.sql  >>/tmp/anything.log
if [ $? -ne 0 ]
  echo "!Error..."
# . . .  Etc . . .

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

DavidSenior Oracle Database AdministratorCommented:
k, if an error is thrown, you want it shown.  Do you need to abort subsequent steps, or allow everything to continue?  Also, can the two sql scripts shown be started in parallel?
earngreenAuthor Commented:
yes, I want to show the error orbjust append to a log file. Some can be run on parrallel and some cannot.
DavidSenior Oracle Database AdministratorCommented:
Mike's first answer works within the SQL script -- you can spool all output to a local file.  The "whenever" syntax will dictate whether the script stops or continues, and comes in both SQL and OS flavors.

I'd perhaps start with enabling a detailed trace, then to scan the alert log for errors thrown.

You could rewrite the /i lines to redirect your SYSOUT and SYSERR away from the display and into separate files -- but that doesn't do anything about the Oracle errors, just the OS ones.
psql -f FILENAME 1>&2 2>/var/log/psql_error_log

the error handling will contain file names and line numbers if there are multiple queries in each file.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.