PLSQL UTL FILE IS NOT GETTING CREATED ANY Solution - sql

Hi guys IP am trying to use the utl_file package in PLSQL...
I have granted the user to create directory privilege and read and write privilege from sysdba...when I create a directory it says "directory created", but is physical harddrive local location no directory is created.
When I write a code to give output in file it gives some errors.
Code:
SQL> create or replace directory PLSQL as 'D:\PLSQL';
Directory created.
SQL>declare
2 fp utl_file.file_type;
3 begin
4 fp := utl_file.fopen('D:\PLSQL', 'Hello.txt','w');
5 utl_file.putf(fp,'HI THIS IS WRITTEN IN SQLPLUS');
6 utl_file.fclose(fp);
7 end;
8 /
declare
*
ERROR at line 1:
ORA-29280: invalid directory object
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at line 4
if any solution help...

Two objections so far:
when you create a directory (an Oracle object), nothing is "automatically" created in filesystem. That directory (D:\PLSQL) has to exist, you have to create it manually (using e.g. Windows Explorer or Command Prompt or ...).
Note that directory usually resides on a database server. If D:\PLSQL is located on your own PC (while the database is elsewhere), it won't work. If you can't access the database server (because of lack of privileges), you'll have to talk to DBA to do it for you. Though, saying that you connected as sysdba, I presume that it shouldn't be a problem for you.
when referencing the directory in PL/SQL code, you have to use its name (PLSQL), not its path (D:\PLSQL):
fp := utl_file.fopen('PLSQL', 'Hello.txt','w');
I tested your code, works just fine (file is created in my directory).

1st create a logical directory using below syntax:
create directory logfile as 'D:\PLSQL';
and then refer the same in your code. Also please be sure you have read, write grant on this new logical directory.
GRANT READ,WRITE ON DIRECTORY logfile TO user_Name;
declare
2 fp utl_file.file_type;
3 begin
4 fp := utl_file.fopen(logfile , 'Hello.txt','w');
5 utl_file.putf(fp,'HI THIS IS WRITTEN IN SQLPLUS');
6 utl_file.fclose(fp);
7 end;
8 /

Related

ORA-22288: passing xml file to SQL Developper

Do you have an idea how to solve this error? The file exists on my computer, but something is going wrong. I have created a XML file, later I have created a schema for it (.xsd) and now I want to connect the schema file with my oracle data base and after this function I would create a table for this schema.
I have also tried to place file in oracle directory, but it doesn't help.
BEGIN
DBMS_XMLSCHEMA.registerSchema(
SCHEMAURL => 'http://xmlns.oracle.com/xdb/documentation/peldesana.xsd',
SCHEMADOC => bfilename('MAPE','peldesana.xsd'),
LOCAL => TRUE,
GENTYPES => FALSE,
GENTABLES => FALSE,
CSID => nls_charset_id('AL32UTF8'));
END;
Error:
ORA-22285: non-existent directory or file for FILEOPEN operation
ORA-06512: at "SYS.DBMS_LOB", line 822
ORA-06512: at "XDB.DBMS_XMLSCHEMA", line 131
ORA-06512: at line 2
22285. 00000 - "non-existent directory or file for %s operation"
*Cause: Attempted to access a directory that does not exist, or attempted to access a file in a directory that does not exist.
*Action: Ensure that a system object corresponding to the specified directory exists in the database dictionary, or make sure the name is correct.
Creating a directory isn't enough.
A directory, object which points to a file system directory (folder), is created by SYS.
SQL> connect sys as sysdba
Enter password:
Connected.
SQL> create or replace directory xml_dir as 'c:\temp';
Directory created.
Then you have to grant privileges on that directory to user which will be using it. If you'll only read data from it, grant read; if you'll also create files there, grant write as well:
SQL> grant read, write on directory xml_dir to scott;
Grant succeeded.
SQL>
Now, user scott will be able to access directory and files within.
In the end I have generated.xsd file once more (using the same .xml file). And then I added one more line (OPTIONS => 2), everything works now.
BEGIN
DBMS_XMLSCHEMA.registerSchema(
SCHEMAURL => 'http://xmlns.oracle.com/xdb/documentation/peldesana2.xsd',
SCHEMADOC => bfilename('INPUT_DIR','peldesana2.xsd'),
LOCAL => TRUE,
GENTYPES => FALSE,
GENTABLES => FALSE,
CSID => nls_charset_id('AL32UTF8'),
OPTIONS => 2);
END;

How to write a txt file on desktop using SQL script in Toad

I'm trying to write a simple txt file using SQL in Toad. Below is the SQL
create or replace directory dir_temp as 'C:\Users\name\Desktop\';
declare
f utl_file.file_type;
begin
f := utl_file.fopen('DIR_TEMP', 'something.txt', 'w');
utl_file.fclose(f);
end;
/
This results into error:
ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 536
ORA-29283: invalid file operation
ORA-06512: at line 4
I've verified the grants for Oracle user on directory. It has all permissions.
Also, I'm able to create spool file in the same directory.
Directory should be created by SYS, and has to be located on the database server (OK, it can be elsewhere, but it usually isn't). If database isn't installed on your own computer but on some database server, then directory object points to a directory on the server, not your own PC.
If you do it properly, it works. I use 11gXE, installed on my laptop. Here's how:
Connected as SYS, I'm creating a directory and granting privileges to user (SCOTT) who will use it.
SQL> show user
USER is "SYS"
SQL> create or replace directory dir_temp as 'c:\users\littlefoot\desktop';
Directory created.
SQL> grant read, write on directory dir_temp to scott;
Grant succeeded.
Connect as Scott; reuse code you wrote:
SQL> connect scott/tiger
Connected.
SQL> declare
2 f utl_file.file_type;
3 begin
4 f := utl_file.fopen('DIR_TEMP', 'something.txt', 'w');
5 utl_file.fclose(f);
6 end;
7 /
PL/SQL procedure successfully completed.
Looks OK. Check whether the file was created:
SQL> $dir c:\users\littlefoot\desktop\*.txt
Volume in drive C is OSDisk
Volume Serial Number is 7635-F892
Directory of c:\users\littlefoot\desktop
05.03.2020. 19:25 0 something.txt
1 File(s) 0 bytes
0 Dir(s) 290.609.057.792 bytes free
SQL>
Right; the file is here (empty, though, but - it exists).
So, if you do it right, it works.

Trying to import Datapump dump into remote Oracle Server via SQL

I am trying to import a datapump dump file into a remote server via SQLPlus.
So, local machine -> remote oracle Server
Dump file is on local machine on a SMB share.
I have written a little script for that operation, however it fails with errors.
The Datapump Directory exists (SMB share on local machine, DIRECTORY is pointing to said share)
My script:
DECLARE
ind NUMBER; -- Loop index
h1 NUMBER; -- Data Pump job handle
percent_done NUMBER; -- Percentage of job complete
logfileName VARCHAR2(30);-- Name of the Logfile
job_state VARCHAR2(30); -- To keep track of job state
le ku$_LogEntry; -- For WIP and error messages
js ku$_JobStatus; -- The job status from get_status
jd ku$_JobDesc; -- The job description from get_status
sts ku$_Status; -- The status object returned by get_status
BEGIN
h1 := DBMS_DATAPUMP.OPEN('IMPORT','FULL',NULL,'IMPORT_XYDESCRIPTOR');
DBMS_DATAPUMP.ADD_FILE(h1,'dump.dmp','DPIMP_REMOTE',NULL,DBMS_DATAPUMP.KU$_FILE_TYPE_DUMP_FILE,1);
DBMS_DATAPUMP.SET_PARAMETER(h1,'TABLE_EXISTS_ACTION','APPEND');
DBMS_DATAPUMP.START_JOB(h1);
[... more stuff...]
However I get the following error, when I execute that script as SYSDBA:
DECLARE
*
ERROR at line 1:
ORA-39001: invalid argument value
ORA-06512: at "SYS.DBMS_SYS_ERROR", line 79
ORA-06512: at "SYS.DBMS_DATAPUMP", line 4056
ORA-06512: at "SYS.DBMS_DATAPUMP", line 4307
ORA-06512: at line 15
line 15 is this line:
DBMS_DATAPUMP.ADD_FILE(h1,'dump.dmp','DPIMP_REMOTE',NULL,DBMS_DATAPUMP.KU$_FILE_TYPE_DUMP_FILE,1);
I don't really get why I get an invalid argument value error, the command is exactly as the Oracle wiki dictates.
Can someone help me on what I am doing wrong?
Maybe the thing I try isn't even possible?

reading and writing text files using UTL_FILE package remotely

I 'am trying to read and write files using utl_file package remotely but the oracle server cant read and write on other pc ,so i make a shared path on '\\adel-pc\test', and i want to read and write on that path but i get this error when reading :
SQL Error [29283] [99999]: ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 536
ORA-29283: invalid file operation
ORA-06512: at line 8
java.sql.SQLException: ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 536
ORA-29283: invalid file operation
ORA-06512: at line 8
and this error when writing :
SQL Error [29283] [99999]: ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 536
ORA-29283: invalid file operation
ORA-06512: at line 7
java.sql.SQLException: ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 536
ORA-29283: invalid file operation
ORA-06512: at line 7
and this is my code :
CREATE OR REPLACE DIRECTORY utl_test AS '\\10.10.60.11\test';
-- reading the file
DECLARE
p_dir varchar2(2000):='UTL_TEST';
p_file_name varchar2(2000):='IFD18021801IFEEDTL'; --file name
l_file UTL_FILE.file_type;
l_text varchar2(32767);
begin
l_file := UTL_FILE.fopen(p_dir,p_file_name,'r');
LOOP
UTL_FILE.get_line(l_file,l_text);
DBMS_OUTPUT.put_line(l_text);
END LOOP;
UTL_FILE.fclose(l_file);
EXCEPTION
WHEN UTL_FILE.invalid_operation THEN dbms_output.PUT_LINE('cannot open file invalid name');
WHEN UTL_FILE.read_error THEN dbms_output.PUT_LINE('cannot be read');
WHEN no_data_found THEN dbms_output.PUT_LINE('end of file');
UTL_FILE.fclose(l_file);
END;
--writing the file
declare
l_file UTL_FILE.file_type;
l_location varchar2(100) := 'UTL_TEST'; -- capital latter
l_filename varchar2(100) := 'am';
begin
l_file := UTL_FILE.fopen(l_location,l_filename,'w');
FOR i IN (SELECT * FROM hr.EMPLOYEES)
LOOP
UTL_FILE.PUT_LINE(l_file,i.EMPLOYEE_ID||' '||i.FIRST_NAME);
END LOOP;
UTL_FILE.fclose(l_file);
l_file := UTL_FILE.fopen(l_location,l_filename,'A');
UTL_FILE.PUT_LINE(l_file,'Additonal lines');
UTL_FILE.fclose(l_file);
END;
so my questions are:
could I write and read on the shared path or it cant be done using utl_file ?
why I am getting this error , knowing that I gave read & write permissions to that path.
I'd suggest you not to do it that way.
First of all, you're creating that procedure in SYS schema, which is - generally speaking - a bad idea. Leave SYS (and SYSTEM) alone; create your own user, grant it required privileges (CREATE SESSION, CREATE PROCEDURE and any other you might require), connect to it and then write code you want.
Why are you creating directories dynamically? That is a one-time job and should be done once, connected as SYS. Although you might have created those directories (it is a good idea to first DBMS_OUTPUT.PUT_LINE command you're about to execute, make sure it is correctly written and then actually run it), you did not grant READ nor WRITE privileges on them to anyone, so - nobody can use them but SYS and - as I've said above - don't use SYS for that.
Note that I'm not talking about operating system privileges, but Oracle ones, e.g.
grant read, write on directory some_dir to scott;
Finally, as of creating a directory on a computer that is not the database server: you should use UNC (Universal Naming Convention) and create a directory literally as \\adel-pc\test; it won't work if you map that directory on the server and use its drive letter.
As of the error itself, huh ... you posted several hundreds lines of code, it is little bit too much to debug just by looking at it, and we miss other objects you use. No idea, sorry.
finally the problem is solved ,the problem was in windows system privileges , I had to give full control permissions to the files ,so yes i can write and read on the shared path using utl_file .

Oracle DB create directory

I am trying to create directory is SQL Developer and create a simple text file there using this code :
CREATE DIRECTORY ABC AS '/abc';
DECLARE
fileHandler UTL_FILE.FILE_TYPE;
BEGIN
fileHandler := UTL_FILE.FOPEN('ABC', 'test_file.txt', 'W');
UTL_FILE.PUTF(fileHandler, 'Writing to a file\n');
UTL_FILE.FCLOSE(fileHandler);
END;
But it ends up with that Error
29283. 00000 - "invalid file operation"
*Cause: An attempt was made to read from a file or directory that does
not exist, or file or directory access was denied by the
operating system.
*Action: Verify file and directory access privileges on the file system,
and if reading, verify that the file exists.
In SQL developer the directory variable is created and visible
So, my question is Does that piece of code suppose to create directory by itself or do i have to create it manually? (I don't have access to the servers file system)
It looks like GRANTS are missing.
To create a directory:
CREATE OR REPLACE DIRECTORY alias AS 'pathname';
Where:
alias is the name of the directory alias.
pathname is the physical directory path.
To Grant:
GRANT permission ON DIRECTORY alias TO {user | role | PUBLIC};
Where:
permission is one of the following:
READ for read-only access
WRITE for write-only access
ALL for read and write access
alias is the name of the directory alias.
user is a database user name.
Edit:
Directory Check:
SQL> SELECT DIRECTORY_NAME , DIRECTORY_PATH FROM DBA_DIRECTORIES WHERE DIRECTORY_NAME = 'BDUMP';
DIRECTORY_NAME DIRECTORY_PATH
------------------------------ ---------------
BDUMP /home/fil_test/
Change directory permission. By default it has only Read and execute permission for others.
terminal$ chmod 777 fil_test
Block:
DECLARE
fHandle UTL_FILE.FILE_TYPE;
BEGIN
fHandle := UTL_FILE.FOPEN ('BDUMP', 'test_file', 'w');
UTL_FILE.PUT (fHandle, 'This is the first line');
UTL_FILE.PUT (fHandle, 'This is the second line');
UTL_FILE.PUT_LINE (fHandle, 'This is the third line');
UTL_FILE.FCLOSE (fHandle);
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE (
'Exception: SQLCODE=' || SQLCODE || ' SQLERRM=' || SQLERRM);
RAISE;
END;
/
Execution:
SQL> #tt.sql
PL/SQL procedure successfully completed.
And i See the file created:
terminal$ ls -lrt test_file*
-rw-r----- 1 oracle dba 68 Oct 24 14:49 test_file