tags:

views:

123

answers:

4

Hi ,

I am very new to oracle.I need to create a file with the system timestamp from oracle. Please let me know how do i do that.

More over I need to write any exceptions or errors thrown by my pl/sql code to a file and exit after an error. How do i do this?

Thanks, Priya.R

+1  A: 

This is how you can get a dynamic filename in SQL Plus

SET TERMOUT OFF

DEFINE dynamic_filename = idle
COLUMN which_dynamic NEW_VALUE dynamic_filename

SELECT   'prefix_'
       ||TO_CHAR( SYSDATE, 'YYYYMMDD' )
       ||'_'
       ||TO_CHAR( SYSDATE, 'HH24MISS' )
       ||'.log'   which_dynamic
  FROM dual;

SET TERMOUT ON

SPOOL &dynamic_filename

SELECT * FROM dual;

SPOOL OFF

The file gets created in the default directory for SQL Plus (on windows this is the "Start In:" property of the shortcut)

To place the output in a known directory amend the SPOOL command to something like...

SPOOL c:\output_dir\&dynamic_filename

To get an SQL Plus script to exit after an error then include this command...

WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK
Paul James
OP did state that this was to be done from PL/SQL. This is a SQL*Plus-centric solution that won't work in PL/SQL.
DCookie
To DCookie - the OP didn't state the file was to be created from PL/SQL, just "from Oracle". They then stated they wanted to "write any exceptions or errors thrown by my pl/sql code to a file and exit", from this I assumed that the PL/SQL was being run by SQL*Plus so therefore constructed my answer appropriately.
Paul James
@Paul, point taken - the question is not perfectly clear. I would argue, however, that "from Oracle" is not the same thing as "from SQL*Plus" (and SQL*Plus was never mentioned), although some fail to see the distinction, as perhaps is the case with the OP.
DCookie
@DCookie - agreed, so we're both right. Upvoted accordingly.
Paul James
A: 

Thanks for your reply. It would be helpful if you could explain me in some detailed way.

Priya
What don't you understand?What level of detail are you looking for?
Paul James
Thanks Paul. I am not able to understand where this file gets created and when I execute this query, I get the following result,1 rows selectedDUMMY ----- X 1 rows selectedI am not sure whether it is creating anyting with the time...:(
Priya
Updated answer with some further clarification.
Paul James
Also, you should have posted this "answer" as a comment under @Paul's answer. Your two "answers" clutter up the entire discussion making it harder to get to the real answers.
DCookie
A: 

I am not able to see the file that gets created also. Please help in finding the file and also to create the file under specified directory (OS directory and the oracle has the directpry defined to it).

Thanks in advance.

Priya
You shouldn't post an "answer" to your question that is really clarification of your original question - rather you should edit your original question.
DCookie
+2  A: 

It seems you want to look into the UTL_FILE Oracle supplied package, as you're trying to use PL/SQL to create the file.

You can generate the file this way:

(You'll need to create an Oracle DIRECTORY first, pointing to the OS location of the file:

CREATE OR REPLACE DIRECTORY DIR AS 'your OS directory';

Note that the name 'DIR' is used in the sample code that follows. You will also require the CREATE DIRECTORY privilege, and then grant read and write permissions on the directory to the user who will use it:

GRANT READ,WRITE ON DIR TO user1;

)

DECLARE
  v_logfile VARCHAR2(100);
  v_FH      UTL_FILE.FILE_TYPE;
BEGIN
  v_logfile := TO_CHAR(SYSDATE,'YYYYMMDD HH24MISS')||'_process.log';
  v_FH      := UTL_FILE.FOPEN(DIR, v_logfile, 'w');
  UTL_FILE.PUTLINE(v_FH, 'Some text on a new line');
  UTL_FILE.FCLOSE(v_FH);
END;
DCookie