Cant print result in CMD with PL/SQL - sql

I am new to PL/SQL, I have ran a simple command of Hello world in my command prompt.
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
But The hello world did not get displayed in CMD although I am getting the message PL/SQL procedure successfully completed message.
Please help out!

SET SERVEROUTPUT ON
will help you to print messages from buffer to your CMD...
so try,
SET SERVEROUTPUT ON
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/

use the below sqlplus command to display the output of dbms_output.put_line
set serveroutput on
before running the anonymous block

We can write pl/SQL in workspace and execute easy.
Example hello world
Set serveroutput on
Declare
message varchar 2(20):='Hello world';
Begin
dbms_output.put_line(message);
End;
/
Result will give
Hello world
Pl/SQL procedure successfully completed .

Related

How to convert a series of SQL execute calls into one single PLSQL code?

I have a large number of SQL execute statements like so :
exec s535.HLVS_Verify.Check_Object('S735', 'RTDPDAH$', 'PACKAGE BODY');
exec s535.HLVS_Verify.Check_Object('S735', 'RTDPDAH$JS$PKG_DATA_ACQ_HIST', 'PACKAGE');
And I am trying to convert this into PLSQL :
How to structure this ?
Could I just put this all in the BEGIN section of a standard PLSQL block ?
thanks
I guess you have exception logging in each of the objects being called.It is as simple as below,
If you intent to run from SQL prompt,
SET SERVEROUTPUT ON
BEGIN
s535.HLVS_Verify.Check_Object('S735', 'RTDPDAH$', 'PACKAGE BODY');
s535.HLVS_Verify.Check_Object('S735', 'RTDPDAH$JS$PKG_DATA_ACQ_HIST', 'PACKAGE');
END;
PS: In case you are using any tool that doesn't support SQL*Plus command just remove SET SERVEROUTPUT ON from the above.
If you would like to have a stored object like a stored procedure,
CREATE OR REPLACE PROCEDURE MASTER_PROCEDURE
AS
BEGIN
s535.HLVS_Verify.Check_Object('S735', 'RTDPDAH$', 'PACKAGE BODY');
s535.HLVS_Verify.Check_Object('S735', 'RTDPDAH$JS$PKG_DATA_ACQ_HIST', 'PACKAGE');
END;

sqlplus variable value is not accepting in EOF block in shell

I am unable to give input to the variable "y" written in below code. Can any one help us Please.
Code :
#!/bin/bash
${ORACLE_HOME}/bin/sqlplus -s abcd/passabcd <<EOF
declare
res varchar2(9) := '&y';
begin
insert into abc values(10);
if res in ('commit;')
then
EXECUTE IMMEDIATE 'commit;';
elsif res in ('rollback;')
then
EXECUTE IMMEDIATE 'rollback;';
else
DBMS_OUTPUT.PUT_LINE('Enter Correct Input');
end if;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error msg ' || substr(sqlerrm,1,200));
end;
/
EOF
if i run this, below is the screen. automatically i am getting sp2-0546 error without entering any input.
[oracle#ssmz861 ~]$ ./mm.sh
Enter value for y:SP2-0546: User requested Interrupt or EOF detected.
[oracle#ssmz861 ~]$
here is the scenario: I have a pl/sql code i need to give to sqlplus to execute in following manner like <EOF .....EOF. in that one i have to see the output of one query and decide whether need to commit or rollback by giving input.
thanks in advance
Siva

Print DBMS_OUTPUT.PUT_LINE in a file with a bash script

I have a pl/SQL query that has a variable .
I want DBMS_OUTPUT.PUT_LINE to a file used on the machine (linux)
Does someone know how to do this?
You need to put some pieces together. Here is my inspiration: DBMS_OUTPUT.PUT_LINE not printing
a) put your plsql in an sql script, e.g. d.sql - the key here is set serveroutput:
set serveroutput on size 30000;
begin
DBMS_OUTPUT.PUT_LINE('my line');
end;
/
exit
b) Then write another script - ksh this time - containing:
sqlplus / #d.sql > output.txt
If you want to restrain what displays from sqlplus, then read appropriate documentation about set statement

How to capture the result of stored procedure through shell script?

I'm trying to execute stored procedure through shell script and try to get return from stored procedure but I didn't get any thing from the stored procedure on other hand same thing I do with sqlplus prompt and I'm able to get the result
sqlplus -silent xxx#xxx <<EOF
set serveroutput on
declare
DE_REC_COUNT number(10);
begin
DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);
end;
EOF
Through sqlplus prompt
SQL> set serveroutput on
declare
DE_REC_COUNT number;
begin
DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);
end;
0
PL/SQL procedure successfully completed.
The version of the anonymous block in the shell script will not be executed as shown, because you don't have a slash after the block to run it. If you run that you get no output at all. If you change it to have a slash:
sqlplus -silent xxx#xxx <<EOF
set serveroutput on
declare
DE_REC_COUNT number(10);
begin
DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);
end;
/
EOF
then you'll see:
0
PL/SQL procedure successfully completed.
You've shown the interactive version in SQL*Plus without the slash too, but you must have had that to see the output you showed.
If you want the zero - which seems to be coming from a dbms_output call in your procedure, rather than directly from your anonymous block - n a shell variable you can refer to later, you can assign the output of the heredoc to a variable:
MY_VAR=`sqlplus -silent xxx#xxx <<EOF
set serveroutput on
set feedback off
declare
DE_REC_COUNT number(10);
begin
DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);
end;
/
EOF`
printf "Got back MY_VAR as %s\n" ${MY_VAR}
Note that I've added set feedback off so you don't see the PL/SQL procedure successfully completed line. Now when you run that you'll see:
Got back MY_VAR as 0
and you can do whatever you need to with ${MY_VAR}. It depends what you mean by 'capture' though.
Here's an example of how it can be done by surrounding the code with the evaluation operators (` back quotes):
#!/bin/sh
results=`sqlplus -s xxx#xxx <<EOF
set serveroutput on feedback off
declare
DE_REC_COUNT number(10);
begin
DE_DUP_PROC ('T_MCL_30404_20150317_020','MCL','30404','FT',DE_REC_COUNT);
end;
/
EOF`
echo $results
Lets say for instance I have a below procedure which calculates the sum of two numbers and prints the total using dbms_output.put_line() method
CREATE OR REPLACE
PROCEDURE SUM
(
a IN NUMBER,
b IN NUMBER)
AS
total NUMBER;
BEGIN
total:= a + b;
dbms_output.put_line(total);
END;
Then in order to call this in our shell script we need to call this procedure in an anonymous block and store in a variable, later using echo we can execute the command.
plsqlcode=`sqlplus -silent hr/sandeep#orcl <<EOF
set serveroutput on
begin
sum(10,20);
end;
/
EOF`
echo $plsqlcode
Ideally this is not a recommended approach you should keep your shell and pl/sql code in separate files.

Oracle how to print some message?

As we know in MSSQL we can write below line to print some message
print 'Some Message';
How we can do same in Oracle as print not working with Oracle?
Use
DBMS_OUTPUT.put_line('Some Message');
To expand on #mhasan's answer and #AlexPoole's comment: assuming that you're executing your script using SQL*Plus you'll need to add the beginning of your script before the first DECLARE or BEGIN:
SET SERVEROUTPUT ON SIZE 1000000
SET LINESIZE 255
If you're using a tool other than SQL*Plus to run your script there will be different ways to view output written to DBMS_OUTPUT. For example, in PL/SQL Developer a "Test" window has a "DBMS Output" tab where text written to DBMS_OUTPUT can be viewed after the test script terminates.
Share and enjoy.