If I execute the following scripts on one and another CLI tool, UTL_FILE would trigger a writing error ? What would you do when you can't find ways to trigger exception errors in unit test?
script1.sql
DECLARE
OUT_HANDLE UTL_FILE.FILE_TYPE;
BEGIN
OUT_HANDLE := UTL_FILE.FOPEN('DIRECTORY','LOGFILE.TXT','R');
DBMS_SESSION.SLEEP(30);
UTL_FILE.FCLOSE(OUT_HANDLE);
END;
script2.sql
DECLARE
OUT_HANDLE UTL_FILE.FILE_TYPE;
BEGIN
OUT_HANDLE := UTL_FILE.FOPEN('DIRECTORY','LOGFILE.TXT','A');
UTL_FILE.PUT_LINE('want to trigger a writing error.');
UTL_FILE.FCLOSE(OUT_HANDLE);
END;
Related
I wish to redeploy some packages.
Here the simplified code:
vCode CLOB;
SELECT DBMS_METADATA.GET_DDL('PACKAGE_BODY', 'MY_PACKAGE', USER) INTO vCode FROM DUAL t;
EXECUTE IMMEDIATE CONVERT(vCode, 'WE8MSWIN1252', 'US8PC437');
Would it be possible to check if the code would be compiled without errors?
And if so, run the EXECUTE IMMEDIATE command?
Many thanks!
When I try to call dbms_metadata.get_ddl('TABLE', 'MYTABLE', 'MYSCHEMA') either in the pl/sql block or in the package procedure it works fine.
When I try to call dbms_metadata.get_ddl('TABLE', 'MYTABLE') (without schema explicitely provided) either in the pl/sql block or in the package procedure it works fine also.
When I try to call dbms_metadata.get_ddl('DIRECTORY', 'MYDIR') (without schema explicitely provided) in the pl/sql block it works fine also.
But,
When I try to call dbms_metadata.get_ddl('DIRECTORY', 'MYDIR', 'MYSCHEMA') either in the pl/sql block or in the package procedure it raises the error:
ORA-31600: invalid input value MYSCHEMA for parameter SCHEMA in function GET_DDL
When I try to call dbms_metadata.get_ddl('DIRECTORY', 'MYDIR') (without schema explicitely provided) in the package procedure it raises the error:
ORA-31603: object "MYDIR" of type DIRECTORY not found in schema "MYSCHEMA"
What is the problem?
EXECUTE_CATALOG_ROLE=true
SELECT_CATALOG_ROLE=true
'CREATE ANY DIRECTORY'=true
PL/SQL Release 12.2.0.1.0 - Production
You need to add the AUTHID CURRENT_USER clause (Docs)
create or replace procedure dir_ddl (dir_name in varchar2) AUTHID CURRENT_USER is
x clob;
begin
SELECT DBMS_METADATA.get_ddl ('DIRECTORY', dir_name) into x from dual;
dbms_output.put_line(x);
end dir_ddl;
/
set serveroutput on
exec dir_ddl('PLSHPROF_DIR')
And my output is...
Procedure DIR_DDL compiled
CREATE OR REPLACE DIRECTORY "PLSHPROF_DIR" AS '/home/oracle/profiler'
PL/SQL procedure successfully completed.
If I remove the AUTHID clause, I see the same error as you report.
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 have following privileges CREATE ANY DIRECTORY, read, write on directory DOCS to user_name
I am using oracle 10g on Windows.
I have first created the directory as
CREATE DIRECTORY DOCS AS 'C:\Documents and Settings\Owner\Desktop\file';
Directory created successfully.
Now when I tried to execute following code
DECLARE
l_file UTL_FILE.file_type;
l_location VARCHAR2(100) := 'DOCS';
l_filename VARCHAR2(100) := 'test.pdf';
l_text VARCHAR2(32767);
BEGIN
-- Open file.
l_file := UTL_FILE.fopen(l_location, l_filename, 'r', 32767);
-- Read and output first line.
UTL_FILE.get_line(l_file, l_text, 32767);
dbms_output.put_line('First Line: |' || l_text || '|');
-- Close the file.
UTL_FILE.fclose(l_file);
END;
I get these errors
ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 475
ORA-29283: invalid file operation
ORA-06512: at line 8
i don't have access to oracle on windows, but something like this in you init.ora file
UTL_FILE_DIR=c:\DOCS
you will then need to restart oracle
you should also be able to do the following (but i've not tested it)
alter system set utl_file_dir=c:\DOCS scope=both
What could cause the following behaviour:
Database 11gR2
declare
l_amt number := dbms_lob.lobmaxsize;
l_dst_loc clob;
l_dst_offset number := 1;
l_lang_ctx number := dbms_lob.default_lang_ctx;
l_src_loc bfile;
l_src_offset number := 1;
l_warning number;
begin
l_src_loc := bfilename('ODS_SERVER_DIRECTORY', '_CIVKD_ASU.CSV');
dbms_lob.createtemporary(l_dst_loc, true);
dbms_lob.fileopen(l_src_loc, dbms_lob.file_readonly);
dbms_lob.loadclobfromfile(l_dst_loc
,l_src_loc
,l_amt
,l_dst_offset
,l_src_offset
,dbms_lob.default_csid
,l_lang_ctx
,l_warning);
commit;
dbms_lob.fileclose(l_src_loc);
dbms_output.put_line(substr(l_dst_loc, 1, 200));
end;
/
ORA-22288: file or LOB operation FILEOPEN failed
.
ORA-06512: in "SYS.DBMS_LOB", line 805
ORA-06512: in line 31
22288. 00000 - "file or LOB operation %s failed\n%s"
*Cause: The operation attempted on the file or LOB failed.
*Action: See the next error message in the error stack for more detailed
information. Also, verify that the file or LOB exists and that
the necessary privileges are set for the specified operation. If
the error still persists, report the error to the DBA.
However opening and reading the exact same file succeeds when using utl_file.
declare
l_file utl_file.file_type;
l_regel varchar2(4000);
begin
l_file := utl_file.fopen('ODS_SERVER_DIRECTORY', '_CIVKD_ASU.CSV', 'R');
-- Haal de volgende regel op
utl_file.get_line(l_file, l_regel);
dbms_output.put_line(l_regel);
utl_file.fclose_all;
end;
So it seems the file is available and accessable by the database.
It's the first time we run into this particular error and it's one of the first 11gR2 instances so maybe there is something 11g specific we don't know about?
=== Update 8-6-2012 ===
Some progress made. It turns out the directory object points to a shared drive. It's a windows server and Oracle runs as Local System. I always thought that it was impossible to read anything from a shared drive in this situation. Apparently in some situations you can using utl_file but not usign dbms_lob.
Could you confirm that ODS_SERVER_DIRECTORY is an actual DIRECTORY
SELECT
*
FROM
dba_objects
WHERE
object_type = 'DIRECTORY'
AND object_name = 'ODS_SERVER_DIRECTORY'
Possibly you have it set in the UTL_FILE_DIR parameter of init.ora (should not be done anymore..)
But its one possibility as to why utl_file would see the directory and dbms_lob would not.