Insert into table with xmltype column from a xml file - oracle

Following is my query to insert xml file
INSERT INTO sampletagtable VALUES ( 1 , XMLType(bfilename('xmldir3', 'book.xml') , nls_charset_id('AL32UTF8') ));
Before that I have created the xmldir3 by the following query,
CREATE OR REPLACE DIRECTORY xmldir3 AS '/opt/user/nishanth/xmldir';
Here /opt/user/nishanth is a directory in my linux os.
book.xml is inside that specified directory.
I am getting the following error,
SQL Error: ORA-22285: non-existent directory or file for FILEOPEN operation
ORA-06512: at "SYS.XMLTYPE", line 296
ORA-06512: at line 1
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.

You created the directory as xmldir3, which is an unquoted identifier so it'll be upper-case in the data dictionary. But then you refer to it in lower-case. You need to use:
bfilename('XMLDIR3', 'book.xml')
You can check the actual directory name by querying the all_directories view:
SQL> CREATE OR REPLACE DIRECTORY xmldir3 AS '/opt/user/nishanth/xmldir';
Directory created.
SQL> SELECT directory_name, directory_path FROM all_directories;
DIRECTORY_NAME DIRECTORY_PATH
------------------------------ ----------------------------------------
XMLDIR3 /opt/user/nishanth/xmldir
...

Related

Oracle DATAPUMP import failed

I am currently trying to import a database using DBMS_DATAPUMP in PL/SQL using the following script.
DECLARE
h1 NUMBER;
BEGIN
h1 := DBMS_DATAPUMP.OPEN('IMPORT', 'FULL', NULL, DBMS_SCHEDULER.generate_job_name, 'LATEST');
DBMS_DATAPUMP.ADD_FILE(handle => h1, filename => 'EXAMPLE6.DMP', directory => 'DUMP');
DBMS_DATAPUMP.START_JOB(h1);
dbms_datapump.detach(h1);
END;
/
Everytime I execute this code, I get the following error message.
ERROR in line 1:
ORA-39001: invalid argument value
ORA-06512: in "SYS.DBMS_SYS_ERROR", line 79
ORA-06512: in "SYS.DBMS_DATAPUMP", line 4929
ORA-06512: in "SYS.DBMS_DATAPUMP", line 5180
ORA-06512: in line 5
I already googled the error, but the answer mostly consisted of checking if the directory was already created and if the user had read and write access to the directory.
I also tried the impdp tool just as an experiment, to see if I could execute imports that way.
impdp pdb2 directory="DUMP" dumpfile="EXAMPLE6.DMP"
Based on the user I am executing impdp as, I get different error messages.
As a user with all privileges granted:
ORA-39001: invalid argument value
ORA-39000: bad dump file specification
ORA-39155: error expanding dump file name "C:\Users\...\EXAMPLE6.DMP"
ORA-48128: opening of a symbolic link is disallowed
As the sysdba user:
ORA-39002: invalid operation
ORA-39070: unable to open the log file
ORA-39087: directory name DUMP is invalid
As I already said, the directory does exist on my drive, I created the directory called DUMP in Oracle and granted read and write access to my user.
All help would be appreciated and I would be happy to clarify if I wrote something confusing!
Edit:
Output of select directory_name, directory_path from dba_directories;
DIRECTORY_NAME
--------------------------------------------------------------------------------
DIRECTORY_PATH
--------------------------------------------------------------------------------
DUMP
C:\Users\Nemanja\Desktop\oraclePLS
I forgot to mention, that the Oracle service has complete access to the specified Windows directory.

How to get all XML file names from a directory using EXTERNAL TABLE

I am trying to get all XML file names present in a directory in order to feed them to a procedure which pulls data out of those files. Could anyone help with how I can get the file name using the EXTERNAL TABLE.
I am having trouble with ACCESS PARAMETERS and LOCATION file. Don't know what exactly would go there.
Thanks
CREATE TABLE S7303786.XML_FILES
(
FILE_NAME VARCHAR2(255 CHAR)
)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_LOADER
DEFAULT DIRECTORY AUTOACCEPT_XMLDIR
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE
PREPROCESSOR AUTOACCEPT_XMLDIR: 'list_file.sh'
FIELDS TERMINATED BY WHITESPACE
)
LOCATION ('list_file.sh')
)
REJECT LIMIT UNLIMITED;
list_files.sh just contains the directory where the files are present.
sticky.txt has nothing in it
error I am getting are :
ORA-29913: error in executing ODCIEXTTABLEFETCH callout
ORA-29400: data cartridge error
KUP-04004: error while reading file /home/transfer/stu/nshstrans/sticky.txt
Error you got might have something to do with directory, Oracle object which points to physical directory on database server's disk. It is created by a privileged user - SYS, who then grants read and/or write privileges on it to users who will use it.
If you missed to do anything of above mentioned things, your external table won't work.
So:
SQL> show user
USER is "SYS"
SQL>
SQL> create directory mydir as 'c:\temp';
Directory created.
SQL> grant read, write on directory mydir to scott;
Grant succeeded.
SQL>
Connect to Scott and create external table:
SQL> connect scott/tiger
Connected.
SQL> create table extusers
2 (username varchar2(20),
3 country varchar2(20)
4 )
5 organization external
6 (type oracle_loader
7 default directory mydir --> this is directory I created
8 access parameters
9 (records delimited by newline
10 fields terminated by ';'
11 missing field values are null
12 (username char(20),
13 country char(20)
14 )
15 )
16 location ('mydata.txt') --> name of the file that contains data
17 ) -- located in c:\temp, which is MYDIR
18 reject limit unlimited -- directory
19 /
Table created.
SQL>
Contents of the sample text file:
SQL> $type c:\temp\mydata.txt
Littlefoot;Croatia
Michel;France
Maaher;Netherlands
SQL>
Finally, let's select from the external table:
SQL> select * from extusers;
USERNAME COUNTRY
-------------------- --------------------
Littlefoot Croatia
Michel France
Maaher Netherlands
SQL>
Works OK, doesn't it? Now, try to do what I did.
On a second reading,
it appears that you don't want to read file contents, but directory contents. If that's so - apparently, it is - then see whether this helps.
In order to make it work, privileged user has to grant additional privilege - EXECUTE - to the directory.
SQL> show user
USER is "SYS"
SQL> grant execute on directory mydir to scott;
Grant succeeded.
Next step is to create an operating system executable (on MS Windows I use, it is a .bat script; on Unix, that would be a .sh, I think) which will list the directory. Note the first line - I have to navigate to a directory which is source for Oracle directory object. If you don't do that, it won't work. The .bat file is simple:
SQL> $type c:\temp\directory_contents.bat
cd c:\temp
dir /b *.txt
SQL>
Create external table:
SQL> create table extdir
2 (line varchar2(50))
3 organization external
4 (type oracle_loader
5 default directory mydir
6 access parameters
7 (records delimited by newline
8 preprocessor mydir:'directory_contents.bat'
9 fields terminated by "|" ldrtrim
10 )
11 location ('directory_contents.bat')
12 )
13 reject limit unlimited
14 /
Table created.
SQL> connect scott/tiger
Connected.
Let's see what it returns:
SQL> select * From extdir;
LINE
-----------------------------------------------
c:\Temp>dir /b *.txt
a.txt
dept.txt
emp.txt
emps.txt
externalfile1.txt
lab18.txt
mydata.txt
p.txt
parfile_01.txt
sofile.txt
test.txt
test2.txt
15 rows selected.
SQL>
Well ... yes, those are my .txt files located in c:\temp directory.
As you use *nix, I think that problem you got is related to list_files.sh script. You didn't post its contents (which would probably help - not necessarily help me as I forgot almost everything I knew about *.nix), but - regarding Preprocessing External Tables (written by Michael McLaughlin), you might need to
prepend /usr/bin before the ls, find, and sed programs: /usr/bin/ls ...
See if it helps.

How to import dmp file to oracle DB via SqlDeveloper or CMD in windows?

I have an Oracle dump file that got exported from an unfamiliar database.
I need to import it to my Oracle DB with either SqlDeveloper or command line in windows.
When using Data Pump Import Wizard in SqlDeveloper I'm getting the below error:
ORA-00942: table or view does not exist
When using the CMD I'm getting the below error:
ORA-39002: invalid operation ORA-39070: Unable to open the log file. ORA-29283: invalid file operation ORA-06512: at "SYS.UTL_FILE", line 536 ORA-29283: invalid file operation
My command line:
impdp USER/password DUMPFILE=c:\folder_name\file_name.dmp TABLES=All LOG=dump_log.log
I tried different variations and each time the same error.
Thank you for your help.
The command is missing the directory or the files in the proper directory on the db server. The default is DATA_PUMP_DIR which can be found from the DB as follow.
SQL> SELECT directory_name, directory_path FROM dba_directories
2 WHERE directory_name='DATA_PUMP_DIR';
DIRECTORY_NAME DIRECTORY_PATH
_________________ _________________________________________________________________
DATA_PUMP_DIR /opt/oracle/admin/ORCL/dpdump/8967C87908440D12E053020011AC6F8A
To make a new directory:
CREATE DIRECTORY MY_DIR AS 'c:\folder_name\';
Then add the directory and remove the path from the file parameter.
impdp USER/password directory=MY_DIR DUMPFILE=file_name.dmp TABLES=All LOG=dump_log.log
ref:
IMPDP > https://docs.oracle.com/cd/E11882_01/server.112/e22490/dp_import.htm#SUTIL907
Create directory > https://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_5007.htm#SQLRF01207

FOPEN not accepting directory path

I'm having trouble with the line
F := UTL_FILE.FOPEN('my_directory', 'MyCSV.csv', 'R');
After previously running
CREATE DIRECTORY my_directory AS 'C:\Users\timfo\eclipse-workspace\CSVCreation';
which succeeds. The error I'm getting is
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at line 9
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.
but I've made sure that the user has permission with
grant create any directory to TimGraupner2;
CREATE DIRECTORY my_directory AS 'C:\Users\timfo\eclipse-workspace\CSVCreation';
grant read on directory my_directory to TimGraupner2;
and all those succeeded. I've tried entering the full directory path in the FOPEN line, and that didn't work either. What's going on?

plsql CSV file reading and write

OWNER SYS
DIRECTORY_NAME ME
DIRECTORY_PATH \\172.16.20.11\Mad\
begin
vSFile := utl_file.fopen('ME','20170405.csv','R');
IF utl_file.is_open(vSFile) THEN
LOOP
i am getting error :
ORA-29283: invalid file operation ORA-06512: at "SYS.UTL_FILE", line 536 ORA-29283: invalid file operation ORA-06512: at "MADHUR.MSP_UPD_DAILYSALESFRMSAP", line 28 ORA-06512: at line 1
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.
Your error tells you exactly what the problem is:
*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.
and what to do to fix it:
*Action: Verify file and directory access privileges on the file system,
and if reading, verify that the file exists.
So you specify:
DIRECTORY_PATH \\172.16.20.11\Mad\
are you able to actually access \\172.16.20.11\Mad\ with your oracle user?
if not, then you need to grant read, write on directory to user and also check OS permissions for your user to that path.
but also consider doing a network share to drive letter, instead of UNC path.
The reason for getting such issue is that you dont have read or write permission to the directory.
Run the below query to see if you have read and write priviledges:
SELECT *
FROM all_tab_privs
WHERE table_name = 'your_directory name';
If you find dont have any access then grant read and write privs.
SQL>CREATE OR REPLACE DIRECTORY dir1 as '/opt/oracle/';
SQL>GRANT READ,WRITE on dir1 to <Required user>; (if you want to give access to particular user)
OR
SQL>GRANT READ,WRITE on dir1 to PUBLIC; (if you want to give access to all users then give access to public)

Resources