<?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>Kwatog &#38; Co &#187; PL/SQL</title>
	<atom:link href="http://kwatog.com/tag/plsql/feed/" rel="self" type="application/rss+xml" />
	<link>http://kwatog.com</link>
	<description>tech notes and general nonsense</description>
	<lastBuildDate>Fri, 04 May 2012 07:34:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>UTF_FILE and Directories</title>
		<link>http://kwatog.com/blog/oracle/utf_file-and-directories/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=utf_file-and-directories</link>
		<comments>http://kwatog.com/blog/oracle/utf_file-and-directories/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 00:38:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[all_directories]]></category>
		<category><![CDATA[directories]]></category>
		<category><![CDATA[folders]]></category>
		<category><![CDATA[utf_file]]></category>

		<guid isPermaLink="false">http://kwatog.com/?p=43</guid>
		<description><![CDATA[In case you are maintaining a pl/sql program developed by someone else and that program writes into a file, then basically that file is written to a folder/directory. And since the directories are saved in codes, you have to find the meaning (or location) of those codes. The code below will help you display the&#8230;]]></description>
			<content:encoded><![CDATA[<p>In case you are maintaining a pl/sql program developed by someone else and that program writes into a file, then basically that file is written to a folder/directory. And since the directories are saved in codes, you have to find the meaning (or location) of those codes. The code below will help you display the information required.</p>
<p><code>SELECT * FROM ALL_DIRECTORIES;</code></p>
<p>The query above will display all the directories accessible to the current user. They are folders in the server where you can write your output files. For some reason, I forgot about this twice already and had to scour the web to find it. The problem is, I also forgot the keywords. So, I&#8217;m writing it now.</p>
<p>To register new directories, you can follow the example below.</p>
<p>Example<br />
<code>create or replace directory mydir as 'c:mydirectory';</code></p>
<p>To use, run the code below.</p>
<p><code>declare<br />
f utl_file.file_type;<br />
begin<br />
f := utl_file.fopen('MYDIR', 'myfilename.txt', 'w');<br />
utl_file.put_line(f, 'just another line text 1');<br />
utl_file.put_line(f, 'just another line text 2');<br />
utl_file.fclose(f);<br />
end;</code></p>
]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/oracle/utf_file-and-directories/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle WITH Clause  (Subquery Refactoring)</title>
		<link>http://kwatog.com/blog/oracle/oracle-with-clause-subquery-factoring/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=oracle-with-clause-subquery-factoring</link>
		<comments>http://kwatog.com/blog/oracle/oracle-with-clause-subquery-factoring/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 11:48:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[subquery optimization]]></category>
		<category><![CDATA[WITH]]></category>

		<guid isPermaLink="false">http://kwatog.com/?p=187</guid>
		<description><![CDATA[WITH clause was introduced in Oracle 9i to provide powerful new syntax for enhancing query performance. It optimizes query speed by eliminating redundant processing in complex queries. WITH Clause Syntax WITH alias_name -- alias for the aggregate_query AS (aggregate_query_here) SELECT... -- Beginning of the query main body It should be noted that multiple aggregate queries&#8230;]]></description>
			<content:encoded><![CDATA[<p>WITH clause was introduced in Oracle 9i to provide powerful new syntax for enhancing query performance. It optimizes query speed by eliminating redundant processing in complex queries. </p>
<h3>WITH Clause Syntax</h3>
<pre name="code" class="sql">
WITH
 alias_name         -- alias for the aggregate_query
AS
 (aggregate_query_here)
SELECT...            -- Beginning of the query main body
</pre>
<p>It should be noted that multiple aggregate queries can be defined in the WITH clause as below.</p>
<pre name="code" class="sql">
WITH
 alias_name1
AS
 (agg_query1) ,
 alias_name2
AS
 (agg_query2)
SELECT...
</pre>
<p>When using subquery factoring, think of the aggregate_query as an in-line view. Actually, it is a view but a temporary one. Instead of creating a permanent view accessible in the database, you are creating a temporary one exclusively used by your main query. </p>
<h3>Usage</h3>
<p>I primarily use this in creating adhoc queries, reports and extractions. Of course, you can just create a view to accomplish this task. However, there are times wherein you are not allowed to create additional objects in the database for one reason or another. In that case, this subquery optimization comes very handy. </p>
<h3>Caveat</h3>
<p>You can&#8217;t use this inside Oracle Forms. It also doesn&#8217;t work inside stored procedures or anything with PL/SQL in it. At least I&#8217;m talking about until 11g. </p>
]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/oracle/oracle-with-clause-subquery-factoring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle CASE WHEN Statement</title>
		<link>http://kwatog.com/blog/oracle/oracle-case-when-statement/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=oracle-case-when-statement</link>
		<comments>http://kwatog.com/blog/oracle/oracle-case-when-statement/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 16:02:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[10g]]></category>
		<category><![CDATA[11g]]></category>
		<category><![CDATA[9i]]></category>
		<category><![CDATA[case]]></category>
		<category><![CDATA[when]]></category>

		<guid isPermaLink="false">http://kwatog.com/?p=182</guid>
		<description><![CDATA[Another oracle sql statement that you may need in order to simplify and optimize your code is the use of CASE statement. Here&#8217;s how it is being used. Syntax #1 CASE [ expression ] WHEN condition_1 THEN result_1 WHEN condition_2 THEN result_2 ... WHEN condition_n THEN result_n ELSE result END Example SELECT last_name, commission_pct, (CASE&#8230;]]></description>
			<content:encoded><![CDATA[<p>Another oracle sql statement that you may need in order to simplify and optimize your code is the use of CASE statement. Here&#8217;s how it is being used. </p>
<h3>Syntax #1</h3>
<p><code><br />
CASE  [ expression ]<br />
  WHEN condition_1 THEN result_1<br />
  WHEN condition_2 THEN result_2<br />
  ...<br />
  WHEN condition_n THEN result_n<br />
  ELSE result<br />
END<br />
</code></p>
<h3>Example</h3>
<p><code><br />
SELECT last_name, commission_pct,<br />
  (CASE commission_pct<br />
    WHEN 0.1 THEN ‘Low’<br />
    WHEN 0.15 THEN ‘Average’<br />
    WHEN 0.2 THEN ‘High’<br />
    ELSE ‘N/A’<br />
  END ) Commission<br />
FROM employees ORDER BY last_name;<br />
</code></p>
<h3>Syntax #2</h3>
<p><code><br />
CASE<br />
  WHEN [ condition_1 ] THEN result_1<br />
  WHEN [ condition_2 ] THEN result_2<br />
  ...<br />
  WHEN [ condition_n ] THEN result_n<br />
  ELSE result<br />
END<br />
</code></p>
<h3>Example</h3>
<p><code><br />
    SELECT m.paper_type    ,<br />
       m.paper_brand   ,<br />
       m.paper_grammage,<br />
       m.paper_width   ,<br />
       m.paper_length  ,<br />
       m.paper_stock_no,<br />
       (<br />
       CASE<br />
       WHEN NVL(smc.qty_reserved_onhand,0)  != NVL(st.sum_qty_r_oh,0) THEN 'QROH NOT TALLY'<br />
       WHEN NVL(smc.qty_reserved_incom,0)   != NVL(st.sum_qty_r_incom,0) THEN 'QRINC NOT TALLY'<br />
       WHEN NVL(smc.ton_reserved_onhand,0)  != NVL(st.sum_ton_r_oh,0) THEN 'TROH NOT TALLY'<br />
       WHEN NVL(smc.ton_reserved_incom,0)   != NVL(st.sum_ton_r_incom,0) THEN 'TRINC NOT TALLY'<br />
       END<br />
       ) ERROR_MESSAGE<br />
from  paper_stock_master m<br />
</code></p>
<p>
Personally, I use Syntax #2 most of the time. I don&#8217;t know why but maybe it just happened that I find it more suitable to use in the situations that I needed the CASE statement.
</p>
<h3>Difference Between DECODE and CASE WHEN</h3>
<p>The most fundamental difference between the two oracle statements is that in DECODE, you are comparing a field or value in a given set of values or fields. It&#8217;s actually a shorthand for IF..ELSIF..ELSE statement with the condition all set to equals (=). CASE WHEN is almost the same but you can use any conditional operator (i.e, =,!=, >, >= , <, <=, etc) thereby giving you more freedom and flexibility in writing your code.
</p>
</p>
<p>In may instances CASE WHEN helped me reduce the number of lines of my codes as well as optimize the performance. On top of that, I no longer need a full pl/sql program with multiple queries and even cursors to be able to provide the result that a simple CASE statement can provide. However CASE, like any other sql commands, is only applicable for certain scenarios. It&#8217;s not a magic keyword that will solve/apply to everything. So you have to weigh your options if you need it or not.
</p>
<p>Ciao!</p>
]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/oracle/oracle-case-when-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rename Table in Oracle</title>
		<link>http://kwatog.com/blog/oracle/rename-table-in-oracle/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rename-table-in-oracle</link>
		<comments>http://kwatog.com/blog/oracle/rename-table-in-oracle/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 10:35:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[rename table]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://kwatog.com/?p=137</guid>
		<description><![CDATA[SYNTAX alter table table_name rename to new_table_name; Example alter table employee rename to employee_bak;]]></description>
			<content:encoded><![CDATA[<p>SYNTAX<br />
<code><br />
alter table<br />
   table_name<br />
rename to<br />
   new_table_name;<br />
</code></p>
<p>Example<br />
<code><br />
alter table<br />
   employee<br />
rename to<br />
   employee_bak;<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/oracle/rename-table-in-oracle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Long to Varchar2</title>
		<link>http://kwatog.com/blog/oracle/long-to-varchar2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=long-to-varchar2</link>
		<comments>http://kwatog.com/blog/oracle/long-to-varchar2/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 16:07:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[c onversion]]></category>
		<category><![CDATA[long]]></category>
		<category><![CDATA[varchar2]]></category>

		<guid isPermaLink="false">http://kwatog.com/?p=104</guid>
		<description><![CDATA[I got tangled with a problem on the datatype LONG in Oracle which is still being used in a number of applications. Siebel, is actually one of them. When I was tasked to extract some data from one of the tables in EIM using C++, I run into this problem. I googled for answer and&#8230;]]></description>
			<content:encoded><![CDATA[<p>I got tangled with a problem on the datatype LONG in Oracle which is still being used in a number of applications. Siebel, is actually one of them. When I was tasked to extract some data from one of the tables in EIM using C++, I run into this problem.</p>
<p>I googled for answer and here&#8217;s what I got.<br />
<code><br />
CREATE OR REPLACE FUNCTION LONG_TO_CHAR( in_rowid rowid,in_owner<br />
varchar,in_table_name varchar,in_column varchar2)<br />
RETURN varchar AS<br />
text_c1 varchar2(32767);<br />
sql_cur varchar2(2000);<br />
--<br />
begin<br />
  sql_cur := 'select '||in_column||' from<br />
'||in_owner||'.'||in_table_name||' where rowid =<br />
'||chr(39)||in_rowid||chr(39);<br />
  dbms_output.put_line (sql_cur);<br />
  execute immediate sql_cur into text_c1;</p>
<p>  text_c1 := substr(text_c1, 1, 5000);<br />
  RETURN TEXT_C1;<br />
END;<br />
</code></p>
<p>It&#8217;s pretty simple but unfortunately, we can&#8217;t do this without using function.</p>
<p>Example use:<br />
<code><br />
SELECT MI_F_LONG_TO_CHAR(ROWID, 'SIEBEL','EIM_ORDER','ORD_X_COMMENTS')<br />
FROM  SIEBEL.EIM_ORDER<br />
</code></p>
<p>credit to <a href="http://www.oracle.com/technology/oramag/code/tips2003/052503.html">Oracle Mag</a> for this entry.</p>
]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/oracle/long-to-varchar2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Explain Plan in Oracle SQL*Plus</title>
		<link>http://kwatog.com/blog/oracle/explain-plan-in-oracle-sqlplus/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=explain-plan-in-oracle-sqlplus</link>
		<comments>http://kwatog.com/blog/oracle/explain-plan-in-oracle-sqlplus/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 10:29:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[explain plan]]></category>

		<guid isPermaLink="false">http://kwatog.com/?p=61</guid>
		<description><![CDATA[In case you only have SQL*Plus as your editor, you can use the following codes to show the execution plan of your query. First Step : create plan EXPLAIN PLAN FOR SELECT field1, field2 FROM table; Second Step: show results SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY); You will need the necessary privileges to execute this. You&#8217;ll also&#8230;]]></description>
			<content:encoded><![CDATA[<p>In case you only have SQL*Plus as your editor, you can use the following codes to show the execution plan of your query. </p>
<p>First Step : create plan<br />
<code>EXPLAIN PLAN FOR<br />
SELECT field1, field2<br />
FROM table;<br />
</code><br />
Second Step: show results<br />
<code><br />
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);<br />
</code></p>
<p>You will need the necessary privileges to execute this. You&#8217;ll also need to have the PLAN table. If not, you&#8217;ll have to create it as that&#8217;s the placeholder where execution plan is written.</p>
<p>This technique is important especially when you don&#8217;t have a GUI-based SQL editor like PL/SQL Developer or TOAD. </p>
]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/oracle/explain-plan-in-oracle-sqlplus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

