This works partially, the code returned should be ORA-942 but it returns 174 because in UNIX the exit status is from 0 - 255, anyone know a way to overcome this :-?
#!/bin/bash
sqlplus user/passwd@move << EOF
select * from kk;
variable temp number
whenever sqlerror exit sql.sqlcode;
begin
:temp := 25; -- this is really a function call, not 25
if ( :temp <> 0 )
then
raise_application_error( (-20000-224) - :temp, 'test' );
end if;
end;
/
EOF
echo $?
i havent tested this as i am not very good at shell scripts
the crux of the logic is raise_application_error( (-20000-224) - :temp, 'test' );
We can raise errors in a given range but the probelm lies with unix shell as the shell will only keep an unsigned byte in the status return value (values 0..255). It takes our exit file and just looks at that last byte. By using -20000-224 and subtracting from the your return code -- we end up exiting with the value of your return code
Ah, yes. I didn't fully read the question. I usually only care if the return code is 0 or not.
I don't think there is any way you will be able to return a status > 255. You can return the exit status in an environment variable and exit with a non-zero value. When you exit with the non-zero value you can then check the special environment variable.
That was a typing mistake from me i am extremely sorry.here is teh right solution .
#!/bin/bash
sqlplus -s user/passwd@move << EOF |grep '^ORA'
select * from kk;
set echo on
variable temp number
whenever sqlerror exit sql.sqlcode;
begin
:temp := 25; -- this is really a function call, not 25
if ( :temp <> 0 )
then
raise_application_error( (-20000-224) - :temp, 'test' );
end if;
end;
/
EOF
set x=$?
echo $?
now save this file as test.sh
and now do
sh test.sh
output would be
ORA-00942: table or view does not exist
ERROR at line 1:
ORA-20279: test
ORA-06512: at line 5
Bookmarks