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

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;

Related

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 set ORA-00922: missing or invalid option on set serveroutput on;

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;
/

How to make to see the output result to execute PL/SQL from SQL Plus?

When i run PL/SQL statment from SQL Plus,I don't see my output result.However,it was successful completed but it don't show output result.
My code is here
DECLARE
message varchar2(20):= 'Hello World';
BEGIN
dbms_output.put_line(message);
END;
/
RESULT: PL/SQL is successfully completed.
It didn't show Hello World output.
You need use the command set serveroutput on to configure buffering for dbms_output like below. Check this Oracle Community Post
set serveroutput on size 15000;

How do I select a variables value in SQL Developer

Problem
I just want to see the value of a variable. I don't understand why this has to be so difficult.
My SQL Statement
--set serveroutput on format wrapped; Tried this too
SET SERVEROUTPUT ON;
--DBMS_OUTPUT.ENABLE(32000); Tried with, and without this
vend_num xx.VENDOR_CWT.VEND_NO%TYPE;
SELECT vend_no
INTO vend_num
FROM xx.VENDOR_NAME
WHERE VENDOR_NAME1 = 'xxxx';
dbms_output.put_line(vend_num);
The Error I'm Geting
Error starting at line 13 in command:
dbms_output.put_line(vend_num)
Error report:
Unknown Command
What I've Tried
I've tried the following answers:
Print text in Oracle SQL Developer SQL Worksheet window
Printing the value of a variable in SQL Developer
I've done what this answer suggested with the gui: https://stackoverflow.com/a/7889380/496680
I've tried exec dbms_output[...] as some posts have suggested.
Question
How do I just print the value of vend_num;
DBMS_Output is a PL/SQL package, so you'd call it from within PL/SQL code.
declare
end_num xx.VENDOR_CWT.VEND_NO%TYPE;
begin
SELECT vend_no
INTO vend_num
FROM xx.VENDOR_NAME
WHERE VENDOR_NAME1 = 'xxxx';
dbms_output.put_line(vend_num);
end;
/

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);

Resources