PL SQL output is not getting displayed - oracle

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

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>

Extracting output from PLSQL procedure to local drive of my laptop

I have a database connection server "server_dev" in sqldeveloper .
Now i want to create a procedure whose output can be directly saved in a csv file for data comparison later in the local drive of my laptop.
So i tried using UTL_FILE oracle package but when i ran the procedure the UTL_FILE was trying to write in the file of the server "server_dev" whereas i dont have any access to that server hence that command isnt working.
for example: the code is:-
CREATE OR REPLACE PROCEDURE export_to_csv_test
IS
v_file UTL_FILE.file_type;
v_string VARCHAR2 (4000);
CURSOR c_contexts
IS
SELECT workspace_id,context_id from contexts where rownum<5;
BEGIN
v_file :=
UTL_FILE.fopen ('Z:\My_Project_knowledge\CSVDIR', 'empdata.csv','w',1000);
FOR cur IN c_contexts
`enter code here`LOOP
v_string :=
cur.workspace_id
|| ','
|| cur.context_id;
UTL_FILE.put_line (v_file, v_string);
END LOOP;
UTL_FILE.fclose (v_file);
END;
for calling it :-
BEGIN
export_to_csv_test;
END;
Error report:
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at "RAY_DEV07_OWNER.EXPORT_TO_CSV_TEST", line 20
ORA-06512: at line 3
29280. 00000 - "invalid directory path"
*Cause: A corresponding directory object does not exist.
*Action: Correct the directory object parameter, or create a corresponding
directory object with the CREATE DIRECTORY command.
So,I analysed it and found that my SQL developer is connected to a server to my local machin and since its my office laptop I cant alter it.
Can i have any other way in which I can save the output of my stored procedure to my local drive in a text or Csv file?
To write a file to your local machine you may use dbms_output; for example in SQLPlus:
SQL> set feedback off
SQL> set echo off
SQL> set serveroutput on
SQL> spool d:\spool.txt
SQL> begin
2 for i in (select level from dual connect by level <= 5) loop
3 dbms_output.put_line('Level ' || i.level);
4 end loop;
5 end;
6 /
WIll produce the file d:\spool.txt:
Level 1
Level 2
Level 3
Level 4
Level 5
If you can select directly from a table or table function, then SQL*Plus 12.2's new SET MARKUP CSV option will be useful. Instead of paginating the query output it will produce CSV. The full syntax is
SET MARKUP CSV {ON|OFF} [DELIMI[TER] character] [QUOTE {ON|OFF}]
Output generation will faster if you turn on this mode with the sqlplus -m option.
It's also useful for querying JSON types. See https://blogs.oracle.com/opal/entry/fast_generation_of_csv_and

sqlplus - turn off output (interactive)

In Oracle SQL*Plus, how can the output (to the terminal) be turned off?
This is for interactive mode, not running a script.
Example:
SELECT 1 from dual;
-- don't want to see the resulting row(s) !
var c refcursor
exec some_procedure(:c);
print
-- don't want to see the resulting row(s) variable C !
Primarily looking for solution on Windows, Oracle DB version 11.2.
set autotrace traceonly The query still runs. The results are sent to the client, but are not displayed. The plan and statistics are displayed however. The statistics requires access to certain v$ views. To run without the statistics, set autotrace traceonly explain.
You can't, really; the options that might help (like set termout off, which you're presumably already familiar with) only apply when running a script.
I'm not sure it really makes sense for the print command, but if you really want to run a query without seeing the output could just put it in a PL/SQL block:
SQL> set feedback off
SQL> set timing on
2 begin
3 for r in (select 1 from dual) loop
4 null;
5 end loop;
6 end;
7 /
Elapsed: 00:00:00.00
SQL>
The null; is needed because you have to do something inside the loop. The compiler doesn't optimize it out - it does still execute the query and retrieve the results, at least as far as I can tell; a slightly more useful query like select table_name from all_tables takes non-zero time - 1.45 seconds on my system.
You could loop over a ref cursor result too I suppose, if you wanted to time the equivalent of the print fetches:
SQL> var c refcursor
2 begin
3 open :c for select table_name from all_tables;
4 end;
5 /
Elapsed: 00:00:00.17
SQL> declare
2 t all_tables.table_name%type;
3 begin
4 loop
5 fetch :c into t;
6 exit when :c%notfound;
7 end loop;
8 close :c;
9 end;
10 /
Elapsed: 00:00:00.10
I went with a script like this (in a file that I execute via #):
var c refcursor
set timi on
exec proc1(:c);
set feedback off
set timi off
exec DBMS_OUTPUT.PUT_LINE(' before PRINT ' || systimestamp);
set termout off
print
set termout on
exec DBMS_OUTPUT.PUT_LINE(' after PRINT ' || systimestamp);
set feedback on
set timi on
rollback;
exit
Set termout off also suppresses the timing output, so I added the PUT_LINEs (could have used a select systimestamp from dual too).
I guess there is no short and elegant solution.

Execute copy from command from plsq

How to execute copy from command inside a plsql block?
E.g. I have copy from test/test#test insert emp using select * from emp;
How can I call this in a plsql block? I have tried with
execute immediate 'copy from test/test#test insert emp using select * from emp';
However when I execute my script which has plsql block gives me
ORA-00900: invalid SQL statement
How can I resolve this issue
COPY is a SQL*Plus command. So it only works in the SQL*Plus client. Find out more.
EXECUTE IMMEDIATE is a PL/SQL command to run dynamic calls, and it only recognises SQL and PL/SQL.
"I am executing sqlscript from sqlplus"
Yes, but you are calling COPY in an anonymous block, so that's with a PL/SQL scope; which means PL/SQL and SQL only.
The way to do this is with a shell script. These are operating system dependent, but something like this would work on a Linux environment.
#!/bin/bash
echo Please enter local Username:
read USERNAME
echo "Please enter local Password:"
read -s PASS
SID=${ORACLE_SID}
if [ "${ORACLE_SID}" != 'TEST' ]
then
sqlplus -s -l $USERNAME/$PASS#$SID << EOF
copy from test/test#test insert emp using select * from emp
exit
EOF
else
echo "Can't copy from TEST to TEST"
fi
Obviously this is just a wild guess at what your program actually does, but I hope you can understand the principle.
In a plsql code if we directly use the command as follows shall serve the similar output
begin
insert into emp1 select * from emp;
end;
emp1 is target table
emp is source table
There are similar ask where one wants to create blank structure or structure with data for backup kindly of activity.Refer link https://oracle-concepts-learning.blogspot.com/2019/09/copy-table-structure-or-data.html
1) Creating blank structure from existing table
--Execute on sql prompt
begin
execute immediate 'create table emp1 as select * from emp where 1=2';
end;
--Execute on sql prompt
select count(1) from emp1;
2) Creating structure from existing table with data
--Execute on sql prompt
begin
execute immediate 'create table emp1 as select * from emp';
end;
--Execute on sql prompt
select count(1) from emp1;

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

Resources