Computing.Net > Forums > Unix > SQL in shell script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

SQL in shell script

Reply to Message Icon

Name: Nitin
Date: September 10, 2003 at 12:54:59 Pacific
OS: Solaris
CPU/Ram: SPARC
Comment:

In shell script I am inserting the record in to the table.After excuation the script output is commit complete which is fine but I need my echo should be display like "Account created"

#!/bin/ksh
.
.
.
.
.
.

inst dev000
sqlplus -s username/password EOF

Insert into table (column1,column2,column3) values ($gblid,$sysid,$crdate);

commit;

exit
EOF

=============================
Script Output #1:

commit complete.
Instead of above line o/p should be "Account created".
How I can do this?
============================

============================
Script Output#2:
ORA-00001: unique constraint violated
If output is like this then o/p should be "Account can't created"
How I can do this?
============================

How I can trap oracle error?



Sponsored Link
Ads by Google

Response Number 1
Name: zeroguy
Date: September 10, 2003 at 14:27:01 Pacific
Reply:

Do you know which command specifically creates these error messages? And do you know what the return codes are for success/error?

If so, then just execute the command and redirect the output to /dev/null . Check the return code, and if it was successfull, echo "Account Created" and if it wasn't echo "Account Can't be Created" or something like that. Here's a little example code:

mycommand -flag argument &> /dev/null
if [ $? -e 0 ] ; then
     echo "Account created"
     exit 0
fi
echo "Account can't be created"
exit 1

I think that's how it goes


0

Response Number 2
Name: James Boothe
Date: September 10, 2003 at 15:36:17 Pacific
Reply:

Rather than send output to /dev/null, I would suggest directing it to a disk file. If you get failure, you would want to see the exact reason for it.

To get sqlplus to exit with a failure status code, the first line of your script should be:

whenever sqlerror exit failure

That will allow you to test success or failure of sqlplus per zeroguy.


0

Response Number 3
Name: Susan
Date: September 10, 2003 at 21:09:41 Pacific
Reply:

This code would do .

tmpfile=`mktemp`
touch $tmpfile
chmod 777 $tmpfile

sqlplus -s username/password /dev/null 2>&1
SET SERVEROUTPUT OFF
SET FEEDBACK OFF
declare
fl utl_file.file_type ;
.
.
begin
fl := pkg_util.fopen ('$tmpfile', 'w') ;
Insert into table (column1,column2,column3) values ($gblid,$sysid,$crdate);
commit;
if sql%rowcount > 0 then
utl_file.put_line (fl, 'Account Created');
else
utl_file.put_line (fl, 'Error Creating A/C')
utl_file.fclose (fl) ;
END ;
/
exit
!
. ${tmpfile}
rm -f ${tmpfile}



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: SQL in shell script

SQL in shell script www.computing.net/answers/unix/sql-in-shell-script/5703.html

Unix shell script runing SQL files www.computing.net/answers/unix/unix-shell-script-runing-sql-files/5286.html

loops in shell scripts!!! www.computing.net/answers/unix/loops-in-shell-scripts/5140.html