I have installed cygwin and i have Oracle 10 g now i want to connect database from shell.can anyone help me??
SQL> create or replace procedure get_area
2 (n_length in number,
3 n_width in number,
4 n_area out number)
5 as
6 begin
7 n_area := n_length*n_width;
8 end get_area;
9 /
This is my procedure i have created in oracle 10g.I want to call the get_area from shell
i am doing this to start that process
subho#subho-PC ~$ #!/bin/csh-f
you can just do it like this
sqlplus login/pass #get_area.sql
where get_area.sql contains your SQL code
the last detail is to put an exit; at the end of the script oftherwise you will stay on the SQL > prompt
Related
This question already has answers here:
Printing the value of a variable in SQL Developer
(9 answers)
Closed 10 months ago.
I have written a simple PL/SQL command of printing hello world to console but it prints nothing and still message is prompted that PL/SQL procedure successfully completed. I am not able to figure it out as to what to do in this case?
Code:
BEGIN
dbms_output.put_line ('Hello World..');
END;
OUTPUT:
You're missing the set serveroutput on.
This is what you have:
SQL> begin
2 dbms_output.put_line('Hello world');
3 end;
4 /
PL/SQL procedure successfully completed.
This is what you should have:
SQL> set serveroutput on --> this
SQL> begin
2 dbms_output.put_line('Hello world');
3 end;
4 /
Hello world --> here's the result
PL/SQL procedure successfully completed.
SQL>
I'm trying to read a file of type txt from c:\Dir and insert the content on the table Oracle Sql
set SERVEROUTPUT ON
CREATE OR REPLACE DIRECTORY MYDIR AS ' C:\dir';
DECLARE
vInHandle utl_file.file_type;
eNoFile exception;
PRAGMA exception_init(eNoFile, -29283);
BEGIN
BEGIN
vInHandle := utl_file.Fopen('MYDIR','attachment.txt','R');
dbms_output.put_line('The File exists');
EXCEPTION
WHEN eNoFile THEN
dbms_output.put_line('The File not exists');
END;
END fopen;
/
i have the file not exists but i have this file
I don't know whether space you have in front of the directory name in the first statement you posted makes difference (or is it just a typo), but - nonetheless, here's how it is usually done.
Create directory on hard disk:
C:\>mkdir c:\dir
Connect to the database as SYS (as it owns the database, as well as directories); create directory (Oracle object) and grant privileges to user which will use that directory:
C:\>sqlplus sys as sysdba
SQL*Plus: Release 11.2.0.2.0 Production on ╚et O×u 5 18:34:43 2020
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Enter password:
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> create or replace directory mydir as 'c:\dir';
Directory created.
SQL> grant read, write on directory mydir to scott;
Grant succeeded.
SQL>
You don't need this, as you already have the file; I'll create it by spooling table contents.
SQL> connect scott/tiger
Connected.
SQL> spool c:\dir\example.txt
SQL> select * From dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> spool off;
SQL> $dir c:\dir\*.txt
Volume in drive C is OSDisk
Volume Serial Number is 7635-F892
Directory of c:\dir
05.03.2020. 18:39 539 example.txt
1 File(s) 539 bytes
0 Dir(s) 290.598.363.136 bytes free
SQL>
Finally, reusing code you wrote:
SQL> set serveroutput on
SQL>
SQL> DECLARE
2 vInHandle utl_file.file_type;
3 eNoFile exception;
4 PRAGMA exception_init(eNoFile, -29283);
5 BEGIN
6 BEGIN
7 vInHandle := utl_file.Fopen('MYDIR','example.txt','R');
8 dbms_output.put_line('The File exists');
9 EXCEPTION
10 WHEN eNoFile THEN
11 dbms_output.put_line('The File not exists');
12 END;
13 END fopen;
14 /
The File exists
PL/SQL procedure successfully completed.
SQL>
Works properly (congratulations, you wrote code that actually works!).
So, what have you done wrong?
as I said, space in front of c:\dir: CREATE OR REPLACE DIRECTORY MYDIR AS ' C:\dir';
database isn't on your computer but on a separate database server
it means that you probably created directory, but it points to c:\dir directory on the database server, not your own PC!
As Boneist commented, it is possible to create a directory (Oracle object) on computer which is NOT a database server, but that's not something we usually do. If you opt to choose this option, you'll have to use UNC (Universal Naming Convention) while creating directory.
Another option you might want to consider is to use SQL Loader. It is an operating system utility, installed along with the database or (full, not instant) client software. Its advantage is that it runs on your local PC (i.e. you don't have to have access to the database server) and is extremely fast. You'd create a control file which tells Oracle how to load data stored in the source (.txt) file.
Another option, which - in the background - uses SQL Loader, is to use an external table. It is yet another Oracle object which points to the source (.txt) file and allows you to access it using a simple SQL SELECT statement. Possible drawback: it still requires access to the Oracle directory (just like your UTL_FILE option).
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
I apologize up-front for this super-lightweight question, but I'm missing something when starting to work with sqlcl as a potential replacement for sqlplus.
sqlcl is compelling, but I'm troubled in that I'm missing how to run anonymous-blocks interactively. The below example works fine when saved as Little-Anonymous-Block.sql and run in sqlcl via #Little-Anonymous-Block.sql, but the raw pl/sql fails with the the below PLS-00103.
Little-Anonymous-Block.sql:
BEGIN
DBMS_OUTPUT.PUT_LINE('This anonymous-block ran in sqlcl!');
END;
/
Running as a Script:
SQL> SET SERVEROUTPUT ON;
SQL> #Little-Anonymous-Block.sql;
This anonymous-block ran in sqlcl!
PL/SQL procedure successfully completed.
But running ad-hoc:
SQL> BEGIN
2 DBMS_OUTPUT.PUT_LINE('This anonymous-block ran in sqlcl!');
3 END;
4 /
gives:
Error starting at line : 1 in command -
BEGIN
DBMS_OUTPUT.PUT_LINE('This anonymous-block ran in sqlcl!');
END;/
PLS-00103: Encountered the symbol "/" The symbol "/" was ignored.
sqlcl appears to be conjoining the "/" with the block-terminating END;
The same command works fine in sqlplus.
Can you tell me, how do I interactively run anonymous blocks in sqlcl? I've got the early-adopter release from 20160513. java 8.0_77. Apologies for this question if its in the sqlcl manual, I didn't find much to go by on the oracle sqlcl-page.
I've found that you can run an anonymous block with exec but that has its limitations (e.g. all code on one line).
As far as I can tell what you've found is a bug. A workaround would be to end your block with a . then execute the buffer with a / as shown below:
This was indeed a bug. It should be fixed in the latest release on OTN
BARRY#orcl☘ >BEGIN
2 DBMS_OUTPUT.PUT_LINE('This anonymous-block ran in sqlcl!');
3 END;
4 /
PL/SQL procedure successfully completed.
BARRY#orcl☘ >l
1 BEGIN
2 DBMS_OUTPUT.PUT_LINE('This anonymous-block ran in sqlcl!');
3* END;
BARRY#orcl☘ >
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);