pl /sql set ORA-00922: missing or invalid option on set serveroutput on; - oracle

set serveroutput on;
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN dbms_output.put_line(message);
END;
/

As others have indicated "set server output on" is a SQL*Plus command. If you need that functionality in plsql the you're looking for is DBMS_OUTPUT.ENABLE. Your above block becomes:
declare
message varchar2(20) := 'Hello World';
begin
dbms_output.enable;
dbms_output.put_line(message);
end ;

If use SQL*plus then this code works fine.
[oracle#krw-sql-ora12-01 ~]$ sqlplus scott/tiger
SQL*Plus: Release 11.2.0.3.0 Production on Fri Feb 22 08:07:25 2019
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> set serveroutput on;
SQL> DECLARE
2 message varchar2(20):= 'Hello, World!';
3 BEGIN dbms_output.put_line(message);
4 END;
5 /
Hello, World!
PL/SQL procedure successfully completed.
SQL>

It seems that you used SET SERVEROUTPUT ON within the PL/SQL procedure (or an anonymous block), e.g.
SQL> begin
2 set serveroutput on;
3 end;
4 /
set serveroutput on;
*
ERROR at line 2:
ORA-06550: line 2, column 7:
PL/SQL: ORA-00922: missing or invalid option
ORA-06550: line 2, column 3:
PL/SQL: SQL Statement ignored
SQL>
Perhaps you didn't post everything you really have; is that SET command part of a larger procedure? If so, move it out.

You mention that you are using toad. You have to use the lightning bolt instead of the green triangle. Hover your mouse over the two icons. You will see the green triangle says: Execute/compile statement at caret. You will see the lightning bolt says: Execute as script.
set serveroutput on;
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN dbms_output.put_line(message);
END;
/

Related

Oracle - How to use & without being asked about the value? [duplicate]

This question already has answers here:
How to declare variable and use it in the same Oracle SQL script?
(11 answers)
Closed 11 months ago.
Basically, I don't want to be asked about the value like this::
SQL> select &test from dual;
Enter value for test:
I want only declare the &test along the script, something like it:
&test varchar2(100):= 'some value'; --of course, this don't work.
Execute
SQL> set define off
before running your code.
SQL> select '&test' from dual;
'&TES
-----
&test
SQL>
If you want to "declare" it, then use var:
SQL> var test varchar2(200);
SQL> exec :test := 'some value';
PL/SQL procedure successfully completed.
SQL> print test
TEST
----------------------------------------------------------------------------------------------------
some value
SQL>
In dynamic SQL: I won't lock anyone, but - I'll change my password.
SQL> connect scott/tiger
Connected.
SQL> var test varchar2(200);
SQL> exec :test := 'lion';
PL/SQL procedure successfully completed.
SQL> print test
TEST
----------------------------------------------------------------------------------------------------
lion
SQL> begin
2 execute immediate 'alter user scott identified by ' || :test;
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> connect scott/tiger
ERROR:
ORA-01017: invalid username/password; logon denied
Warning: You are no longer connected to ORACLE.
SQL> connect scott/lion
Connected.
SQL>

How can I drop and re-create a pluggable database in Oracle DB 18c XE? [duplicate]

I'm trying to execute a script on SQL PLus, it's simple.
SET serveroutput ON;
DECLARE
mode NUMBER(1) := 1;
IF (mode = 1) THEN
prompt 'HERE'
END IF;
prompt 'fim'
I call the script from SQLPlus using sqlplus user/pw#db and #myscript.sql after a successful connection. But the output is strange for me:
Conectado a:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> #myscript.sql
9
10
11
And it continues to print this sequence indefinitely.
What am I doing wrong?
From your edited question... you have to terminate the PL/SQL block with a / on a new line to make it end and run, otherwise SQL*Plus will keep prompting for more lines of code (which is the numbers you're seeing). The documentation shows how to run PL/SQL blocks. And prompt is a SQL*Plus command so you can't use it inside a PL/SQL block. You also don't have your block syntax right:
SET serveroutput ON;
DECLARE
mode NUMBER(1) := 1;
BEGIN
IF mode = 1 THEN
DBMS_OUTPUT.PUT_LINE('HERE');
END IF;
END;
/
prompt fim
You cannot use sqlplus command in plsql.We can use dbms_output instead which will display the output in SQL prompt
SET serveroutput ON;
DECLARE
mode NUMBER(1) := 1;
BEGIN
IF (mode = 1) THEN
dbms_output.put_line('HERE');
END IF;
dbms_output.put_line('fim');
END;
/
Go to your oracle home Oracle\product\<version>\client_2\sqlplus\admin\glogin.sql and add the following lines to enable printing globally,
SET ECHO ON;
SET TERM ON;
WHENEVER SQLERROR EXIT FAILURE ROLLBACK;
SET DEFINE OFF;

How to rollback automatically if script having error in Oracle sql plus

I need to execute the multiple script having one master sql file. Whenever I used to execute the master calling script named as calling_test.sql if anything error comes need to be rollbacked.
sqlplus USERNAME/PWD#SIR_NAME;
##calling_test.sql
here is content of calling_test.sql script.
SET echo ON;
SET define ON;
SET scan ON;
define PATH =/krishna/test
define AB_SCHEMA=AIM
spool Test_incremental.log
SET define ON;
##&&PATH/AUG/2019-08-28/test1.sql
SET define ON;
##&&PATH/AUG/2019-08-29/test2.sql
SET define ON;
##&&PATH /AUG/2019-08-30/test3.sql
SET define ON;
The scrip should contain something like this:
whenever sqlerror exit rollback
Example:
SQL> create table test (col number);
Table created.
SQL>
SQL script (named p.sql)
whenever sqlerror exit rollback
insert into test values (100);
insert into test values ('A');
Calling it:
M:\>sqlplus scott/tiger#orcl #p.sql
SQL*Plus: Release 11.2.0.1.0 Production on ╚et Ruj 26 13:38:50 2019
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
1 row created.
insert into test values ('A')
*
ERROR at line 1:
ORA-01722: invalid number
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
M:\>
Result:
SQL> select * From test;
no rows selected
SQL>
You can rollback the actions from called scripts, but in addition to WHENEVER command you must also SET AUTOCOMMIT OFF. Normally when Sqlplus exits a script invoked in a separate file it commits. However the preceding overrides that action. See below: (save each script to the indicated file.)
---------------------------------------------------------------------------- -- script mst_0.sql
create table multi_script_test( id integer, description varchar2(50));
insert into multi_script_test values( 0, 'Initial before script.');
commit;
-- script mst_1.sql
insert into multi_script_test values( 1, 'Insert from script mst_1');
-- script mst_2.sql
insert into multi_script_test values ( 2, 'Insert from script mst_2');
-- script mst_3.sql
insert into multi_script_test values ( 3/0, 'oops');
-- script mst_4.sql
insert into multi_script_test values ( 4, 'Insert from script mst_4');
-- main script mst_main.sql
set echo on
set autocommit off
whenever sqlerror continue rollback
##c:/so/ora/mst_0.sql
##c:/so/ora/mst_1.sql
##c:/so/ora/mst_2.sql
-- following should display rows 0, 1, 2
select * from multi_script_test;
-- generate error and due to whenever directive 'rollback' discard rows 1,2
#c:/so/ora/mst_3.sql
-- continue script processing, also due to whenever directive 'contunue'
#c:/so/ora/mst_4.sql
commit;
----------------------------------------------------------------------------
sqlplus -- complete the signon
-- run main script
#mst_main
-- following show show display 0, 4
select * from multi_script_test;
exit

PL SQL output is not getting displayed

I have fairly simply code ..running in Oracle Virtualbox. However for some reason it is not displaying pl/sql output.
Here is code snippet
SQL> set serveroutput on
SQL> list
1 Create or Replace procedure mytz
2 IS
3 v_mytz TIMESTAMP WITH TIME ZONE DEFAULT '2013-05-05 12:00:00 AM';
4 BEGIN
5 DBMS_OUTPUT.PUT_LINE ('Default timestamp is ' );
6* end mytz ;
SQL> /
Procedure created.
SQL>
Is there anything I need to do special to see the output on SQL prompt ?
You have to actually run the procedure, not just create it, e.g.:
set serverputput on
exec mytz;
The set serveroutput SQL*Plus command has to be in the session the procedure is executed, not the one where it is created (if they are different).
You are not showing the value of your variable at the moment; maybe you wanted this?
dbms_output.put_line('Default timestamp is: ' || v_mytz);

How do I Suppress "PL/SQL procedure successfully completed" message in sqlplus?

Is there a way that you can have SERVEROUTPUT set to ON in sqlplus but somehow repress the message "PL/SQL procedure successfully completed" that is automatically generated upon completed execution of a plsql procedure?
Use the command:
SET FEEDBACK OFF
before running the procedure. And afterwards you can turn it back on again:
SET FEEDBACK ON
This has worked well for me in sqlplus, but I did just notice that "set feedback off" suppresses errors in Sql Developer (at least version 17.2.0.188). Just something to be aware of if you use Sql Developer:
create or replace procedure test_throw_an_error as buzz number; begin dbms_output.put_line('In test_throw_an_error. Now, to infinity!'); buzz:=1/0; end;
/
set serveroutput on
set feedback off
exec test_throw_an_error;
exec dbms_output.put_line('Done, with feedback off');
set feedback on
exec test_throw_an_error;
exec dbms_output.put_line('Done, with feedback on');
Result:
Procedure TEST_THROW_AN_ERROR compiled
In test_throw_an_error. Now, to infinity!
Done, with feedback off
In test_throw_an_error. Now, to infinity!
Error starting at line : 11 in command -
BEGIN test_throw_an_error; END;
Error report -
ORA-01476: divisor is equal to zero
ORA-06512: at "ECTRUNK.TEST_THROW_AN_ERROR", line 1
ORA-06512: at line 1
01476. 00000 - "divisor is equal to zero"
*Cause:
*Action:
Done, with feedback on
PL/SQL procedure successfully completed.

Resources