Search code examples
oracle-databasekshdbms-output

How to get the dbms_output.put_line within the store procedure result by unix korn shell


Please refer to my sample, In oracle DB,these is below SP

CREATE OR REPLACE PROCEDURE SP_TEST_PUTLINE AS 
BEGIN
DBMS_OUTPUT.ENABLE;
  dbms_output.put_line('Hello world!');
END SP_TEST_PUTLINE;

I use the unix shell to call SP to get the dbms_output.put_line('Hello world!') from the SP, how should I do. I use sqlplus command to logon to DB ,seems can't get the result I want.

   output=$(IFS='';echo connect ${DBUSER}/${DBPASS}@${ORACLE_SID} execute SP_TEST_PUTLINE|sqlplus -s /nolog )

Can anyone help me? Thanks in advance...

------------------------------------split line 20180705 -------------------------------------

Thanks for the help of Alex and Kaushik Nayak,very helpful.

below are some findings for Kaushik Nayak,Please refer.

Hi Kaushik,It works, But at first, it was failed with below error

unknown command beginning "-e connect..." - rest of line ignored. SP2-0734: 

So I change echo -e option to echo,then it works. So here are the questions 1)why I use the echo without -e option can get below result,Is it cause by IFS?

echo "abc\n def \nghi" 
abc
 def 
ghi

2) according to Alex's comments, whese two options need to write them to two lines? but you did not use \n between them

 set serveroutput on
set feedback off

and when I add \n between them as below .it encouterred error as below, but the 'hellow world!' has output,

SP2-0734: unknown command beginning "feedback o..." - rest of line ignored. Hello world! PL/SQL procedure successfully completed. 

here is question, why did you not use \n between set serveroutput on and set feedback off, did this DB command (set feedback off) has run successfully?

Look forward to your reply. thanks in advance!


Solution

  • The command you are using will fail to even connect because you need a line break between the connect and the execute.

    But you also need to set serveroutput on, and you probably want to set feedback off too (and maybe other options.

    I'd use a heredoc to make it easier to read and maintain:

    output=$(
    sqlplus -s /nolog <<!EOF
    connect ${DBUSER}/${DBPASS}@${ORACLE_SID}
    set serveroutput on
    set feedback off
    execute SP_TEST_PUTLINE
    !EOF
    )
    
    # then do whatever you want with the output
    echo ${output}
    

    You might also want to do some error checking...