0

I am trying to call pl/sql block in unix script using sqlplus. I just tried to print a statement but nothing is getting printed and I am not getting any error as well.

Result=`sqlplus -s $TgtUsrID/$TgtPswd@$TgtServer <<eof
whenever sqlerror exit sql.sqlcode;
SET SERVEROUTPUT ON;
BEGIN
   DBMS_OUTPUT.PUT_LINE('Hello World!');
END;
eof
`
current_time=`date`
echo " Script execution   finished at $current_time"

1 Answer 1

2

Just need to prepend the variable(Result) with the dollar operator after the last EOF along with an echo command such as

Result=`sqlplus -S /nolog << EOF
 conn $TgtUsrID/$TgtPswd@$TgtServer
 whenever sqlerror exit sql.sqlcode
 set feedback off

 SET SERVEROUTPUT ON;
 BEGIN
   DBMS_OUTPUT.PUT_LINE('Hello World!');
 END;
 /
EOF`

echo $Result    
echo Script execution finished at $(date)

would yield such a result

Hello World!
Script execution finished at Tue Feb 14 00:15:51 +03 2021

where quotes for the description after the last echo is redundant, and prefer using connection with /nolog as being securer, since the password of the schema would be unveiled whenever ps -ef command issued in the current case.

Sign up to request clarification or add additional context in comments.

6 Comments

Why is the timestamp over 2 week ago?
I see. You have one of them old-fashioned slow virtual machines..... ;-)
just as an aside about coding efficiency ... instead of two lines "current_time=date" and "echo Script execution finished at $current_time", you could / should just simply "echo Script execution finished at $(date)"
@EdStevens: Forget coding efficiency. For me it is all about code verbosity and wondering if current_time was going to be used again somewhere.
@wallyk - well, by 'code efficiency' I really meant in writing the code, not in the execution of it. Certainly if I need to reference the same value multiple times I'd put it in its own variable. But in this case, where it is displaying the current time of a given activity it's pointless to capture the current time in a variable when there is already a built-in variable. Note that "echo date " is the same as "echo $(date) ".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.