<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Oracle Tips and Techniques &#187; Oracle Built-ins</title>
	<atom:link href="http://www.oraclecity.com/category/oracle-built-ins/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.oraclecity.com</link>
	<description>Oracle 11g, Oracle 10g, PL/SQL, Oracle Enterprise Manager 11g Grid Control, Oracle Performance Tuning, Microsoft SQL Server and T-SQL</description>
	<lastBuildDate>Thu, 12 Jan 2012 11:36:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Oracle DBMS_UTILITY package- How to use &#8211; learn with example</title>
		<link>http://www.oraclecity.com/oracle-built-ins/oracle-dbms_utility-package-how-to-use-learn-with-example/</link>
		<comments>http://www.oraclecity.com/oracle-built-ins/oracle-dbms_utility-package-how-to-use-learn-with-example/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 23:53:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle Built-ins]]></category>
		<category><![CDATA[dbms_utility]]></category>
		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://www.oraclecity.com/?p=311</guid>
		<description><![CDATA[Oracle dbms_utility is a powerful built-in package that can be used to perform several tasks from pl/sql programs. Learn here with loads of example.]]></description>
			<content:encoded><![CDATA[<p>Oracle DBMS_UTILITY is a Built-In (Oracle supplied) Oracle package that provides a number of utility routine. The package helps to perform a number of taks which are very helpfull if you are required to do DBA related work. To be able to execute this package you will require execute grants from yuor DBA. If this is your own database and if your schema user does not have privilege to execute this package then you should login as sys user and grant execute privilege to this package to user application user schema.</p>
<p>You can run the following command syntax to give the grant to user SCOTT.</p>
<p><strong>GRANT EXECUTE ON SYS.DBMS_UTILITY TO SCOTT;</strong></p>
<p><strong><br />
</strong></p>
<p>Caution: Some of the commands that you can run are very long running processes &#8211; so take appropriate action if required i.e. inform other users or DBA if you want to recompile whole of your schema or analize the schema or database.</p>
<p>See the example how this package can be used to perform the following task:</p>
<ul>
<li><strong><a href="http://www.oraclecity.com/how-to-use-oracle-dbms_utility_exec_ddl_statement">exec_ddl_statement &#8211; To execute DDL statement</a></strong><strong><br />
</strong></li>
</ul>
<ul>
<li><strong><a href="http://www.oraclecity.com/how-to-compile-oracle-schema-objects-using-dbms-utility">compile_schema &#8211; To compile objects in a schema</a></strong><strong><br />
</strong></li>
</ul>
<ul>
<li><strong><a href="http://www.oraclecity.com/how-to-use-dbms_utility-analyze_schema-to-analyze-schema">analyze_schema &#8211; To analyze objects in a schema</a></strong><strong><br />
</strong></li>
</ul>
<ul>
<li><strong><a href="http://www.oraclecity.com/how-to-use-dbms-utility-analyze-database">analyze_database &#8211; To analyze objects in a database</a></strong><strong><br />
</strong></li>
</ul>
<ul>
<li><strong><a href="http://www.oraclecity.com/how-to-use-database-server-mode-using-dbms_utility-package">is_parallel_server &#8211; To find out if database server is running in parallel server mode</a></strong><strong><br />
</strong></li>
</ul>
<ul>
<li><strong><a href="http://www.oraclecity.com/how-to-get-current-time-dbms-utility-package"> get_time &#8211; To get current time in 100th&#8217;s of a second</a></strong><strong><br />
</strong></li>
</ul>
<ul>
<li><strong><a href="http://www.oraclecity.com/get-parameter-value-and-type-using-oracle-dbms-utility">get_parameter_value &#8211; to get a given init.ora parameter value and type</a></strong><strong><br />
</strong></li>
</ul>
<ul>
<li><strong><a href="http://www.oraclecity.com/how-to-get-operating-system-version-using-oracle-dbms_utility-package">port_string &#8211; To get operating system version and port of the operatign system</a></strong><strong><br />
</strong></li>
</ul>
<ul>
<li><strong><a href="http://www.oraclecity.com/how-to-get-database-version-info-using-oracle-dbms-utility"> db_version &#8211; To get database version and compatibility information</a></strong><strong><br />
</strong></li>
</ul>
<ul>
<li><strong><a href="http://www.oraclecity.com/how-to-use-dbms_utility-package-to-analyze-oracle-partitions"> analyze_part_object &#8211; To analyze partitions in Table/Index</a></strong><strong><br />
</strong></li>
</ul>
<ul>
<li><strong><a href="http://www.oraclecity.com/how-to-find-oracle-database-server-cluster-mode-using-dbms-utility-package">is_cluster_database &#8211; To find out if the database is in CLUSTER mode</a></strong><strong><br />
</strong></li>
</ul>
<ul>
<li><strong><a href="http://www.oraclecity.com/how-to-find-oracle-rac-instance-number-using-dbms-utility-package"> current_instance &#8211; To find out the instance number &#8211; in RAC Only</a></strong><strong><br />
</strong></li>
</ul>
<ul>
<li><strong><a href="http://www.oraclecity.com/how-to-get-cpu-time-using-oracle-dbms-utility-package">get_cpu_time &#8211; To get current CPU time in 100th&#8217;s of a second</a></strong><strong><br />
</strong></li>
</ul>
<ul>
<li><strong><a href="http://www.oraclecity.com/oracle-object-dependency-using-dbms-utility"> get_dependency &#8211; To get DEPENDENCY information on an object </a></strong></li>
</ul>
<p><span style="font-family: Courier; font-size: x-small;">. </span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.oraclecity.com/oracle-built-ins/oracle-dbms_utility-package-how-to-use-learn-with-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DBMS_LOCK &#8211; Sleep functionality (Procedure) in Oracle</title>
		<link>http://www.oraclecity.com/oracle-built-ins/dbms_lock-sleep-functionality-procedure-in-oracle/</link>
		<comments>http://www.oraclecity.com/oracle-built-ins/dbms_lock-sleep-functionality-procedure-in-oracle/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 11:55:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle Built-ins]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[pl/sql]]></category>

		<guid isPermaLink="false">http://www.oraclecity.com/?p=97</guid>
		<description><![CDATA[Learn about Oracle DBMS_LOCK package: DBMS_LOCK is an Oracle supplied in-built package which provides interface to Oracle's Lock management services. Using this package you can lock an object in specific mode, give a name to the lock and release it when lock is no more required. 
However the subject of this posting is not locks but Seep procedure in the DBMS_LOCK package.]]></description>
			<content:encoded><![CDATA[<p>HTML clipboardDBMS_LOCK is an Oracle supplied in-built package which provides interface to Oracle&#8217;s Lock  management services. Using this package you can lock an object in specific mode, give a  name to the lock and release it when lock is no more required.</p>
<p>However the subject of this posting is not locks but Seep procedure in the DBMS_LOCK  package.</p>
<p>In most of the programming languages you will find some form of technique where you can  wait/sleep for some specified amount of time. This SLEEP/WAIT functionality may be  required for different number of reason. For example, suppose you want to insert a record  into a table every 5 seconds. In Microsoft SQL Server you will just use  &#8220;waitfor&#8221; command which is a part of the T-SQL programming language. But how  will you do that in Oracle. Here is how:.</p>
<p>Example: This example will insert a record in table t_sleep_test. Then wait for 5  seconds. Then insert another record.</p>
<p>1. Create table t_sleep_test</p>
<p><span id="more-97"></span></p>
<p>create table t_sleep_test(<br />
run_cnt number,<br />
date_time date);</p>
<p>2. Create a stored procedure which will add the record every 5 seconds.</p>
<p>create or replace procedure p_sleep_test is<br />
l_cnt number;<br />
begin<br />
l_cnt := 0;<br />
execute immediate &#8216;truncate table t_sleep_test&#8217;;<br />
for l_cnt in 1..10 loop<br />
insert into t_sleep_test values (l_cnt, sysdate);commit;<br />
dbms_lock.sleep(5);<br />
end loop;<br />
end;<br />
/</p>
<p>3. Now run the stored procedure p_sleep_test from SQL*Plus by</p>
<p>SQL&gt;exec p_sleep_test;</p>
<p>4. From another session to your oracle database run a query to see that 1 record is  inserted every 5 seconds. In the program above it will look for 10 times</p>
<p>SQL&gt;select run_cnt, to_char(date_time,&#8217;dd-mon-yyyy hh24:mi:ss&#8217;) from    t_sleep_test;</p>
<p>So the above process shows how you can put delays/sleep in your program. I have put  execute immediate to get a fresh set of data every time it runs to make testing more  enjoyable <img src='http://www.oraclecity.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>.</p>
<p><big><span style="text-decoration: underline;"><strong>Read solutions using other Oracle Built-ins in this BLOG:</strong></span></big></p>
<p><big><span style="text-decoration: underline;"><strong> </strong></span></big></p>
<h4><a title="Permanent Link a DBMS_SCHEDULER: Learn with Example" rel="bookmark" href="../dbms_scheduler-learn-with-example/">DBMS_SCHEDULER: Learn with  Example</a></h4>
<h4><a title="Permanent Link a UTL_FILE – Writing to an O/S File" rel="bookmark" href="../utl_file-writing-to-an-os-file/">UTL_FILE – Writing to an  O/S File</a></h4>
<h4><a title="Permanent Link a UTL_FILE – Reading an O/S file" rel="bookmark" href="../using-oracle-built-ins/">UTL_FILE – Reading an O/S file</a></h4>
]]></content:encoded>
			<wfw:commentRss>http://www.oraclecity.com/oracle-built-ins/dbms_lock-sleep-functionality-procedure-in-oracle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DBMS_SCHEDULER: Learn with Example</title>
		<link>http://www.oraclecity.com/oracle-built-ins/dbms_scheduler-learn-with-example/</link>
		<comments>http://www.oraclecity.com/oracle-built-ins/dbms_scheduler-learn-with-example/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 13:52:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle Built-ins]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[Oracle Advanved Tech]]></category>

		<guid isPermaLink="false">http://www.oraclecity.com/?p=69</guid>
		<description><![CDATA[A simple but very useful guide on Oracle DBMS_SCHEDULER package. The examples will show you how to use the package to schedule a job and how to maintain the scheduled jobs in Oracle databases.
Released with Oracle 10g this package replaces DBMS_JOB which was avalable with previous version of Oracle databases. DBMS_JOB still runs in Oracle 10g and even in 11g but that is used only for backward compatibility and should not be used in developing new solutions. ]]></description>
			<content:encoded><![CDATA[<p>This is very useful Oracle supplied package which is used to run scheduled job in Oracle databases. Released with Oracle 10g this package replaces DBMS_JOB which was available with previous version of Oracle databases. DBMS_JOB still runs in Oracle 10g and even in 11g but that is used only for backward compatibility and should not be used in developing new solutions. Where possible it is advisable to convert legacy jobs which uses DBMS_JOB to use more powerful and robust DBMS_SCHEDULER.</p>
<p>Like DBMS_JOB package, the DBMS_SCHEDULER is a collection of functions and procedures intended to make job scheduling in Oracle more robust and easy to use.</p>
<p><span style="font-size: x-small; color: #000000;">PRIVILEGES:</span><br />
===========================</p>
<p>To create and runs job in Oracle database you will need CREATE JOB role. To perform administrative tasks you will need SCHEDULER_ADMIN role.</p>
<p>Depending on the requirement and your installation environment you may like to grant the following privileges:</p>
<p>CREATE ANY JOBS<br />
CREATE EXTERNAL JOBS</p>
<p>CREATING A SCHEDULED JOB:<br />
===========================</p>
<p>The package is supplied to help perform some database tasks such as running a stored procedure or package at some given time and at some given interval.</p>
<p>And example is here below:</p>
<p>dbms_scheduler.create_job( job_name=&gt;&#8217;Myschema.Daily_Emp_Report&#8217;,<br />
job_type=&gt;&#8217;STORED_PROCEDURE&#8217;,<br />
job_action=&gt;&#8217;Myschema.p_daily_emp_teport&#8217;,<br />
number_of_arguments=&gt;0,<br />
start_date=&gt;TRUNC(SYSDATE),         repeat_interval=&gt;&#8217;FREQ=WEEKLY;BYDAY=SUN;BYHOUR=07;BYMINUTE=00;  BYSECOND=00&#8242;,<br />
end_date=&gt;NULL,<br />
job_class=&gt; &#8216;DEFAULT_JOB_CLASS&#8217;,<br />
enabled=&gt;TRUE,<br />
auto_drop=&gt;FALSE,<br />
comments=&gt;NULL);</p>
<p>In this example I am creating a scheduled Oracle job named Myschema.Daily_Emp_Report. When run, this job will execute a STORED PROCEDURE called p_daily_emp_teport. The stored procedure p_daily_emp_teport resides in a schema called Myschema. The job will run on every week Sunday at 07:00.</p>
<p><span id="more-69"></span></p>
<p>Here it should be noted that the stored procedure to be run can be in any schema as long as the owner of the scheduler job has execute privilege to run the stored procedure.</p>
<p>The most interesting part in the above command is the parameter called repeat_interval. This will dictate when and how often this job is needed to run. In the above example of it is very clear we want to run the job only once a week which is on Sunday at 7 AM.</p>
<p>Some example parameter repeat_interval:</p>
<p>1. repeat_interval=&gt;&#8217;FREQ=daily;byhour=20;byminute=0;bysecond=0&#8242;.<br />
The job will runs daily from Monday to Sunday at 20:00 hours.</p>
<p>2. repeat_interval=&gt;&#8217;FREQ=daily;byhour=20;byminute=30;bysecond=0&#8242;.<br />
The job will runs daily from Monday to Sunday at 20:30 hours.</p>
<p>3. repeat_interval=&gt;&#8217;FREQ=monthly;bymonthday=10,20; byminute=0;bysecond=0;&#8217;<br />
The job will on 10th and 20th day of each month.</p>
<p>4. repeat_interval=&gt;&#8217;FREQ=yearly;bymonth=JAN,APR,SEP,DEC;BYMONTHDAY=21;<br />
byhour=20;byminute=30;bysecond=0&#8242; The job will run on 21st of months<br />
JAN, APR, SEP and DEC at 20:30 hours.</p>
<p>UPDATING A SCHEDULED JOB<br />
===========================</p>
<p>Sometime you may find yourself in a situation where you would like to change a parameter value of the scheduled job after you have actually created it. The easiest way to do that might be just drop the job and re-create it with the correct parameter values. But I would say that will be a drastic action to take and in some situation (when it is in production environment) it just might not be the appropriate action and your manager might be really feel uneasy in approving such steps, quite rightly.</p>
<p>That&#8217;s why Oracle has provided ways to change a parameter value without actualling dropping anything. An example is given below. Here I am changing my job Myschema.Daily_Emp_Report to run a new stored procedure called Myschema.p_monthly_emp_teport instead of running Myschema.p_daily_emp_teport:</p>
<p>DBMS_SCHEDULER.SET_ATTRIBUTE (<br />
name=&gt;&#8217;Myschema.Daily_Emp_Report&#8217;,<br />
attribute=&gt;&#8217;job_action&#8217;,<br />
value=&gt;&#8217;Myschema.p_monthly_emp_teport&#8217;);</p>
<p>STOPPING A SCHEDULED JOB<br />
===========================</p>
<p>If you need just to stop a scheduler job you can do so my executing the package</p>
<p>DBMS_SCHEDULER.STOP_JOB (<br />
job_name IN VARCHAR2<br />
force IN BOOLEAN DEFAULT FALSE<br />
commit_semantics IN VARCHAR2 DEFAULT &#8216;STOP_ON_FIRST_ERROR&#8217;);</p>
<p>where<br />
job_name -&gt; is the name of your job to be stopped<br />
force -&gt; is either TRUE (terminate immediately) or FALSE (stops the job gracefully)<br />
commit_semantics -&gt; accepted value STOP_ON_FIRST_ERROR or ABSORB_ERRORS</p>
<p>Example: DBMS_SCHEDULER.STOP_JOB(job_name=&gt;&#8217;Myschema.Daily_Emp_Report&#8217;,<br />
force=&gt;&#8217;TRUE&#8217;,  commit_semantics=&gt;&#8217;ABSORB_ERRORS&#8217;):</p>
<p>The preceding command will stop the job Myschema.Daily_Emp_Report immediately and will try to absorbs error and commit all stop operations that were successful.</p>
<p>DROPPING A SCHEDULER JOB<br />
===============================</p>
<p>The command to drop a scheduler job is</p>
<p>exec dbms_scheduler.drop_job(&#8216;myjobname&#8217;);</p>
<p>USEFUL DATA DICTIONARY OBJECTS<br />
========================================</p>
<p>DBA_SCHEDULER_PROGRAMS<br />
DBA_SCHEDULER_JOBS<br />
DBA_SCHEDULER_JOB_ROLES<br />
DBA_SCHEDULER_JOB_CLASSES<br />
DBA_SCHEDULER_WINDOWS<br />
DBA_SCHEDULER_PROGRAM_ARGS<br />
DBA_SCHEDULER_JOB_ARGS<br />
DBA_SCHEDULER_JOB_RUN_DETAILS<br />
DBA_SCHEDULER_JOB_LOG<br />
DBA_SCHEDULER_WINDOW_LOG<br />
DBA_SCHEDULER_WINDOW_DETAILS<br />
DBA_SCHEDULER_WINDOW_GROUPS<br />
DBA_SCHEDULER_WINGROUP_MEMBERS<br />
DBA_SCHEDULER_WINGROUP_MEMBERS<br />
DBA_SCHEDULER_SCHEDULES<br />
DBA_SCHEDULER_RUNNING_JOBS<br />
DBA_SCHEDULER_REMOTE_DATABASES<br />
DBA_SCHEDULER_REMOTE_JOBSTATE<br />
DBA_SCHEDULER_GLOBAL_ATTRIBUTE<br />
DBA_SCHEDULER_CHAINS<br />
DBA_SCHEDULER_CHAIN_RULES<br />
DBA_SCHEDULER_CHAIN_STEPS<br />
DBA_SCHEDULER_RUNNING_CHAINS<br />
DBA_SCHEDULER_CREDENTIALS</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oraclecity.com/oracle-built-ins/dbms_scheduler-learn-with-example/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>UTL_FILE &#8211; Writing to an Operating System File</title>
		<link>http://www.oraclecity.com/oracle-built-ins/oracle-utl-file-writing-to-an-os-file/</link>
		<comments>http://www.oraclecity.com/oracle-built-ins/oracle-utl-file-writing-to-an-os-file/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 14:37:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle Built-ins]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[utl_file]]></category>

		<guid isPermaLink="false">http://www.oraclecity.com/?p=56</guid>
		<description><![CDATA[Example of using UTL_FILE to write data to an operating system file. This package is supplied by Oracle and is used to read an operating system file or write to an operating system file. ]]></description>
			<content:encoded><![CDATA[<p><strong>UTL_FILE<br />
</strong>========<br />
This package is supplied by Oracle and is used to read an operating system file or write to<br />
an operating system file.</p>
<p>So if you are thinking of reading and processing data fro man operating system file or writing<br />
to a file to generate a report or something like that then you will definitely need to use this<br />
handy Oracle supplied package.</p>
<p><strong>ACCESSING OPERATING SYSTEM FILE STRUCTURE<br />
</strong>=========================================</p>
<p>To access an operating system directory structure for read/write the directory need to be defined first.<br />
In earlier versions of Oracle (i.e. Oracle 8.0.4) the init.ora parameter UTL_FILE_DIR is used define<br />
the directories to be used in UTL_FILE. While using that parameter it is necessary to re-start Oracle<br />
database so that your Oracle instance understands this parameter.<br />
The parameter is defined as:<br />
UTL_FILE_DIR=&#8217;/home/oracle/dave/&#8217; &#8212; for single directory<br />
or<br />
UTL_FILE_DIR=&#8217;/home/oracle/dave/&#8217;, &#8216;/home/oracle/tom/&#8217;, &#8216;/home/oracle/jim/&#8217; &#8211;for multiple directories</p>
<p><span id="more-56"></span></p>
<p>Or a command line facility is also available such as</p>
<p>alter system set utl_file_dir=&#8217;/home/oracle/dave/&#8217; scope=spfile</p>
<p>However later versions of Oracle introduced CREATE DIRECTORY command where you can simply create a<br />
directory and use it in UTL_FILE. For example to create such a directory run command which will look<br />
like:</p>
<p>CREATE OR REPLACE DIRECTORY DIR_MYTEMP AS &#8216;/home/oracle/Dave/&#8217;;</p>
<p>Note: To be able to create a directory you will need CREATE ANY DIRECTORY privilege.</p>
<p>Then you can use the DIR_MYTEMP directory in your code with UTL_FILE:</p>
<p><strong>WRITE TO A FILE USING UTL_FILE<br />
</strong>==============================</p>
<p>Here is an example of how to write to an operating system file<br />
using a stored procedure written in PL/SQL:<br />
create or replace procedure p_write<br />
is</p>
<p>cursor c_cur1 is<br />
select param_name, param_value<br />
from t_parameters;</p>
<p>l_file utl_file.file_type;<br />
l_dir varchar2(50) := &#8216;DIR_MYTEMP&#8217;;<br />
l_name varchar2(20) := &#8216;mydatafile.txt&#8217;;</p>
<p>begin</p>
<p>l_file := utl_file.fopen( l_dir, l_name, &#8216;W&#8217;);</p>
<p>for cur_rec in c_cur1 loop<br />
utl_file.put_line(l_file, cur_rec.param_name||&#8217;,'||cur_rec.param_value);<br />
end loop;</p>
<p>utl_file.fclose(l_file);</p>
<p>exception<br />
WHEN UTL_FILE.INVALID_PATH THEN<br />
DBMS_OUTPUT.PUT_LINE (&#8216;invalid_path&#8217;); RAISE;</p>
<p>WHEN UTL_FILE.INVALID_MODE THEN<br />
DBMS_OUTPUT.PUT_LINE (&#8216;invalid_mode&#8217;); RAISE;</p>
<p>WHEN UTL_FILE.INVALID_FILEHANDLE THEN<br />
DBMS_OUTPUT.PUT_LINE (&#8216;invalid_filehandle&#8217;); RAISE;</p>
<p>WHEN UTL_FILE.INVALID_OPERATION THEN<br />
DBMS_OUTPUT.PUT_LINE (&#8216;invalid_operation&#8217;); RAISE;</p>
<p>WHEN UTL_FILE.READ_ERROR THEN<br />
DBMS_OUTPUT.PUT_LINE (&#8216;read_error&#8217;); RAISE;</p>
<p>WHEN UTL_FILE.WRITE_ERROR THEN<br />
DBMS_OUTPUT.PUT_LINE (&#8216;write_error&#8217;); RAISE;</p>
<p>WHEN UTL_FILE.INTERNAL_ERROR THEN<br />
DBMS_OUTPUT.PUT_LINE (&#8216;internal_error&#8217;); RAISE;</p>
<p>end p_write;<br />
/</p>
<p>Executing the procedure will create a file named mydatafile.txt in<br />
directory /home/oracle/Dave/</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oraclecity.com/oracle-built-ins/oracle-utl-file-writing-to-an-os-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UTL_FILE &#8211; How to read data from a file</title>
		<link>http://www.oraclecity.com/oracle-built-ins/utl-file-how-to-read-data-from-a-file/</link>
		<comments>http://www.oraclecity.com/oracle-built-ins/utl-file-how-to-read-data-from-a-file/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 14:05:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle Built-ins]]></category>
		<category><![CDATA[pl/sql]]></category>
		<category><![CDATA[utl_file]]></category>

		<guid isPermaLink="false">http://www.oraclecity.com/?p=29</guid>
		<description><![CDATA[Easy to understand example of reading operating system file using UTL_FILE package of Oracle. ]]></description>
			<content:encoded><![CDATA[<p>UTL_FILE<br />
========</p>
<p>This package is supplied by Oracle and is used to read an operating system file or write to<br />
an operating system file.</p>
<p>So if you are thinking of reading and processing data fro man operating system file or writing<br />
to a file to generate a report or something like that then you will definitely need to use this<br />
handy Oracle supplied package.</p>
<p>ACCESSING OPERATING SYSTEM FILE STRUCTURE<br />
=========================================</p>
<p>To access an operating system directory structure for read/write the directory need to be defined first.<br />
In earlier versions of Oracle (i.e. Oracle 8.0.4) the init.ora parameter UTL_FILE_DIR is used define<br />
the directories to be used in UTL_FILE. While using that parameter it is necessary to re-start Oracle<br />
database so that your Oracle instance understands this parameter.</p>
<p>The parameter is defined as:</p>
<p>UTL_FILE_DIR=&#8217;/home/oracle/dave/&#8217;  &#8212; for single directory<br />
or<br />
UTL_FILE_DIR=&#8217;/home/oracle/dave/&#8217;, &#8216;/home/oracle/tom/&#8217;, &#8216;/home/oracle/jim/&#8217; &#8211;for multiple directories</p>
<p>Or a command line facility is also available such as</p>
<p>alter system set utl_file_dir=&#8217;/home/oracle/dave/&#8217; scope=spfile<br />
However later versions of Oracle introduced CREATE DIRECTORY command where you can simply create a<br />
directory and use it in UTL_FILE. For example to create such a directory run command which will look<br />
like:</p>
<p>CREATE OR REPLACE DIRECTORY DIR_MYTEMP AS &#8216;/home/oracle/Dave/&#8217;;</p>
<p>Note: To be able to create a directory you will need CREATE ANY DIRECTORY privilege.</p>
<p>Then you can use the DIR_MYTEMP directory in your code with UTL_FILE:</p>
<p><span id="more-29"></span></p>
<p>READING FROM AN OPERATING SYSTEM FILE USING UTL_FILE<br />
====================================================<br />
Points to note for the example:</p>
<p>1. DIR_MYTEMP is the directory where my files is located.<br />
2. mydatafile.txt is the filename in directory DIR_MYTEMP.<br />
The contect of the file is:</p>
<p>Param1,100,Cat1<br />
Parameter1,100,Cat2<br />
Paramw1,100,Cat3<br />
Parameeeeeee1,100,Cat4<br />
Paramee1,100,Cat5<br />
Param1,100,Cat6<br />
Here is an example of how to read from an operating system file<br />
using a stored procedure written in PL/SQL:<br />
create or replace procedure p_read<br />
is<br />
input_file      utl_file.file_type;<br />
input_buffer    varchar2(1000);<br />
l_dir           varchar2(50) := &#8216;DIR_MYTEMP&#8217;;<br />
l_name          varchar2(20) := &#8216;mydatafile.txt&#8217;;<br />
l_param_name    varchar2(50);<br />
l_param_val     varchar2(10);<br />
l_param_cat     varchar2(10);</p>
<p>begin<br />
begin<br />
input_file := utl_file.fopen( l_dir, l_name, &#8216;R&#8217;);</p>
<p>EXCEPTION WHEN OTHERS THEN<br />
dbms_output.put_line(&#8216;Error in opening FILE&#8217;);<br />
END;<br />
BEGIN<br />
LOOP<br />
utl_file.get_line (input_file, input_buffer);<br />
IF input_buffer IS NULL THEN<br />
EXIT;<br />
END IF;</p>
<p>l_param_name := substr(input_buffer,1, instr(input_buffer,&#8217;,',1, 1)-1);<br />
l_param_val  := substr(input_buffer,instr(input_buffer,&#8217;,',1, 1)+1,<br />
instr(input_buffer,&#8217;,',1, 2)-instr(input_buffer,&#8217;,',1, 1)-1);</p>
<p>l_param_cat  := substr(input_buffer,instr(input_buffer,&#8217;,',1, 2 )+1);</p>
<p>dbms_output.put_line(l_param_name||&#8217;-'||l_param_val||&#8217;-'||l_param_cat);</p>
<p>END LOOP;<br />
EXCEPTION<br />
WHEN NO_DATA_FOUND THEN<br />
dbms_output.put_line(&#8216;END OF FILE&#8217;);<br />
END;</p>
<p>utl_file.fclose(input_file);</p>
<p>exception<br />
WHEN UTL_FILE.INVALID_PATH THEN<br />
DBMS_OUTPUT.PUT_LINE (&#8216;invalid_path&#8217;); RAISE;</p>
<p>WHEN UTL_FILE.INVALID_MODE THEN<br />
DBMS_OUTPUT.PUT_LINE (&#8216;invalid_mode&#8217;); RAISE;</p>
<p>WHEN UTL_FILE.INVALID_FILEHANDLE THEN<br />
DBMS_OUTPUT.PUT_LINE (&#8216;invalid_filehandle&#8217;); RAISE;</p>
<p>WHEN UTL_FILE.INVALID_OPERATION THEN<br />
DBMS_OUTPUT.PUT_LINE (&#8216;invalid_operation&#8217;); RAISE;</p>
<p>WHEN UTL_FILE.READ_ERROR THEN<br />
DBMS_OUTPUT.PUT_LINE (&#8216;read_error&#8217;); RAISE;</p>
<p>WHEN UTL_FILE.WRITE_ERROR THEN<br />
DBMS_OUTPUT.PUT_LINE (&#8216;write_error&#8217;); RAISE;</p>
<p>WHEN UTL_FILE.INTERNAL_ERROR THEN<br />
DBMS_OUTPUT.PUT_LINE (&#8216;internal_error&#8217;); RAISE;</p>
<p>end p_read;<br />
/<br />
Executing the stored procedure will give the output as:</p>
<p>Param1-100-Cat1<br />
Parameter1-100-Cat2<br />
Paramw1-100-Cat3<br />
Parameeeeeee1-100-Cat4<br />
Paramee1-100-Cat5<br />
Param1-100-Cat6<br />
END OF FILE</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oraclecity.com/oracle-built-ins/utl-file-how-to-read-data-from-a-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

