<?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; Oracle</title>
	<atom:link href="http://kwatog.com/tag/oracle/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>Hide sqlldr Oracle Password in PS Command</title>
		<link>http://kwatog.com/blog/oracle/hide-sqlldr-oracle-password-in-unix/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=hide-sqlldr-oracle-password-in-unix</link>
		<comments>http://kwatog.com/blog/oracle/hide-sqlldr-oracle-password-in-unix/#comments</comments>
		<pubDate>Fri, 10 Feb 2012 03:34:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[ps]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[sqlldr]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[vulnerability]]></category>

		<guid isPermaLink="false">http://kwatog.com/?p=734</guid>
		<description><![CDATA[Our internal audit team has raised an issue on some scripts exposing database user, password and tns. You see, all you need is an access to unix shell and run ps command and you&#8217;ll be able to get the user id and password of the database. On the otherhand, there&#8217;s not a lot of people&#8230;]]></description>
			<content:encoded><![CDATA[<p>Our internal audit team has raised an issue on some scripts exposing database user, password and tns. You see, all you need is an access to unix shell and run ps command and you&#8217;ll be able to get the user id and password of the database. On the otherhand, there&#8217;s not a lot of people who have access to the unix shell. But regardless, it&#8217;s still a security vulnerability and must be rectified. Here&#8217;s how.<br />
<span id="more-734"></span><br />
Basically, the steps are as follows.</p>
<ol>
<li>Write the connection string to a temp file. This would be your parameter file. The format is as below.<br />
   <code>userid=scott/tiger@</code><br />
  As it is a parameter file, you can actually put all the other sqlldr parameters in there.
</li>
<li>Call sqlldr with parfile parameter.<br />
Example : <code>sqlldr parfile=paramfile.txt data=mydata.csv control=mytable.ctl log=mydata101.log</code>
</li>
<li>Delete the temp file</li>
</ol>
<p>To be more clear, here&#8217;s the actual code I used.<br />
<code><br />
  umask 0077<br />
  PWDFILE=$(mktemp)<br />
  echo "userid=${SYS_JOB_BSS_ORACLE_UID}/${PASWD}@${SYS_JOB_BSS_ORACLE_SID}" > $PWDFILE<br />
  sqlldr  parfile=$PWDFILE  data=${data_file} control=${APP_CTL_FILE_NAME} log=${data_file}.log rows=100 errors=100<br />
  retCode=`echo $?`<br />
  rm $PWDFILE<br />
 </code> </p>
<p> I would like to explain it a bit but I realized that you won&#8217;t be here if you don&#8217;t understand what it does. If anything, the umask there just sets the permission of the temp file to be created as 077. The rest are pretty much self explanatory.</p>
<p>http://laurentschneider.com/wordpress/2009/05/hide-password-from-ps-output-sql-loader.html</p>
]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/oracle/hide-sqlldr-oracle-password-in-unix/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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 Power Function</title>
		<link>http://kwatog.com/blog/oracle-power-function/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=oracle-power-function</link>
		<comments>http://kwatog.com/blog/oracle-power-function/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 03:30:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[Oracle SQL]]></category>
		<category><![CDATA[power function]]></category>

		<guid isPermaLink="false">http://kwatog.com/?p=681</guid>
		<description><![CDATA[There are some pl/sql functions that you rarely use and we always forget the syntax.And here&#8217;s another example &#8212; the power function. You know, that&#8217;s the n to the power of x (n^x). Again, I feel a bit ashamed for forgetting this one. Here&#8217;s the syntax. power(m,n); whereby m is the base. n is the&#8230;]]></description>
			<content:encoded><![CDATA[<p>There are some pl/sql functions that you rarely use and we always forget the syntax.And here&#8217;s another example &#8212; the power function.  You know, that&#8217;s the n to the power of x (n^x). Again, I feel a bit ashamed for forgetting this one. Here&#8217;s the syntax.<br />
<span id="more-681"></span><br />
<code><br />
power(m,n);<br />
</code></p>
<p>whereby<br />
m is the base.<br />
n is the exponent.</p>
<p>Example<br />
<code><br />
power(36,5);<br />
</code><br />
will return 1679616</p>
<p>Take note that this function returns NUMBER as the data type. That means the maximum value can only be 1.0 x 10^126. Later versions (10g onwards) can use binary_double as the base which makes the function output binary_double. I don&#8217;t know where you would need that large number, though. Also, if m is negative, then n must be an integer.</p>
<p>In case you are asking where I need it, there&#8217;s this code conversion whereby I needed to use the power function to get the value of a particular character. The exponent(n) is not fixed so I needed this function. To be specific, the function involves the pl/sql version of RowIdToRowIdNum of Siebel. I&#8217;ll post the code later.</p>
<p>References:</p>
<p>http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1594387900346571874</p>
]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/oracle-power-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle MERGE command</title>
		<link>http://kwatog.com/blog/oracle-merge-command/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=oracle-merge-command</link>
		<comments>http://kwatog.com/blog/oracle-merge-command/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 02:57:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[DML]]></category>
		<category><![CDATA[insert]]></category>
		<category><![CDATA[merge]]></category>
		<category><![CDATA[statement]]></category>
		<category><![CDATA[update]]></category>
		<category><![CDATA[upsert]]></category>

		<guid isPermaLink="false">http://kwatog.com/?p=468</guid>
		<description><![CDATA[Oracle MERGE command was added in 9i but I only learned about it lately. This command is sometimes referred to as UPSERT because in one statement, you can both update and insert records into a target. Based on the matching condition, MERGE can update existing records if a match is found or insert new record&#8230;]]></description>
			<content:encoded><![CDATA[<p>Oracle MERGE command was added in 9i but I only learned about it lately. This command is sometimes referred to as UPSERT because in one statement, you can both update and insert records into a target. Based on the matching condition, MERGE can update existing records if a match is found or insert new record if not. Prior to 9i, we need separate sql statements or a PL/SQL program to do this.</p>
<p><span id="more-468"></span><br />
<code><br />
MERGE INTO tstg_idl_approach a<br />
USING tmdb_idl_approach b<br />
ON (a.approach_id = b.approach_id)<br />
WHEN MATCHED THEN<br />
   UPDATE SET   a.approach_code = b.approach_code ,<br />
                a.current_ind   = b.current_ind<br />
WHEN NOT MATCHED THEN<br />
	INSERT (approach_id  ,<br />
		    approach_code,<br />
            current_ind  )<br />
	VALUES (b.approach_id  ,<br />
            b.approach_code,<br />
            b.current_ind   );<br />
</code><br />
For further reading, I recommend visiting <a href="http://www.oracle-developer.net/display.php?id=203">http://www.oracle-developer.net/display.php?id=203</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/oracle-merge-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convert Rows to One Column</title>
		<link>http://kwatog.com/blog/oracle/convert-rows-to-one-column/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=convert-rows-to-one-column</link>
		<comments>http://kwatog.com/blog/oracle/convert-rows-to-one-column/#comments</comments>
		<pubDate>Sat, 07 Aug 2010 10:23:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[convert row to col]]></category>

		<guid isPermaLink="false">http://kwatog.com/?p=262</guid>
		<description><![CDATA[Converting rows to column is a breeze if you are using Microsoft Access or reporting tools like Oracle Reports Builder. However, there are times that you&#8217;ll also need it for something else. I mean like displaying rows to one column in a SQL*Plus report. So how leh? (imagine that saying with a Singaporean accent) Here&#8217;s&#8230;]]></description>
			<content:encoded><![CDATA[<p>Converting rows to column is a breeze if you are using <strong>Microsoft Access </strong>or reporting tools like <strong>Oracle Reports Builder</strong>. However, there are times that you&#8217;ll also need it for something else. I mean like displaying rows to one column in a <strong>SQL*Plus</strong> report. So how leh? (<em>imagine that saying with a Singaporean accent</em>)<br />
<span id="more-262"></span><br />
Here&#8217;s one.</p>
<h5>XMLAGG (Available since Oracle 9i)</h5>
<p><code><br />
select rtrim (xmlagg (xmlelement (e, email_add || ',')).extract ('//text()'), ',') email_add<br />
from email_groups<br />
where email_group = '1-A945-9';<br />
</code><br />
In that example, I needed to display all the email addresses in an email group. The output should be separated by comma so that I can pass it onto the email client without any problem nor any additional codes.</p>
<p>There&#8217;s more at <a href="http://www.dba-oracle.com/t_converting_rows_columns.htm">Burleson Consulting</a> in case you want other techniques. </p>
]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/oracle/convert-rows-to-one-column/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Date Functions in MySQL</title>
		<link>http://kwatog.com/blog/date-function-in-mysql/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=date-function-in-mysql</link>
		<comments>http://kwatog.com/blog/date-function-in-mysql/#comments</comments>
		<pubDate>Sat, 07 Aug 2010 01:23:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[add_months]]></category>
		<category><![CDATA[date_add]]></category>
		<category><![CDATA[months_between]]></category>
		<category><![CDATA[Oracle]]></category>

		<guid isPermaLink="false">http://kwatog.com/?p=382</guid>
		<description><![CDATA[Here&#8217;s few of the usual equivalent Oracle data functions in MySQL. DATE ADD (SYSDATE +/-1) SELECT DATE_ADD(SYSDATE(),INTERVAL 1 DAY); SELECT DATE_ADD(SYSDATE(),INTERVAL -10 DAY); ADD_MONTHS SELECT DATE_ADD(SYSDATE(),INTERVAL 1 MONTH); TRUNC() SELECT DATE(SYSDATE()); TO_DATE() SELECT STR_TO_DATE( '07-08-2010', '%d-%m-%Y' ); TIME DIFFERENCE SELECT TIMESTAMPDIFF( SECOND , '2010-01-07 23:50:59', SYSDATE( ) ) /3600 /24 day, TIMESTAMPDIFF( SECOND , '2010-01-07&#8230;]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s few of the usual equivalent Oracle data functions in MySQL.</p>
<p><strong>DATE ADD (SYSDATE +/-1)</strong><br />
<code><br />
SELECT DATE_ADD(SYSDATE(),INTERVAL 1 DAY);<br />
SELECT DATE_ADD(SYSDATE(),INTERVAL -10 DAY);<br />
</code><br />
<span id="more-382"></span><br />
<strong>ADD_MONTHS</strong><br />
<code><br />
SELECT DATE_ADD(SYSDATE(),INTERVAL 1 MONTH);<br />
</code></p>
<p><strong>TRUNC()</strong><br />
<code><br />
SELECT DATE(SYSDATE());<br />
</code></p>
<p><strong>TO_DATE()</strong><br />
<code><br />
SELECT STR_TO_DATE( '07-08-2010', '%d-%m-%Y' );</code></p>
<p><strong>TIME DIFFERENCE</strong><br />
<code><br />
SELECT TIMESTAMPDIFF(<br />
SECOND , '2010-01-07 23:50:59', SYSDATE( ) ) /3600 /24 day,<br />
TIMESTAMPDIFF(<br />
SECOND , '2010-01-07 23:50:59', SYSDATE( ) )  /3600 hours,<br />
TIMESTAMPDIFF(<br />
SECOND , '2010-01-07 23:50:59', SYSDATE( ) )  /60 mins,<br />
TIMESTAMPDIFF(<br />
SECOND , '2010-01-07 23:50:59', SYSDATE( ) )  secs,<br />
</code><br />
In the preceding example, I could have just used HOUR, MINUTE, DAY, MONTH or YEAR interval but in real world, I mostly use SECOND for a more accurate results. That&#8217;s not to say timestampdiff is not accurate. Maybe because I&#8217;m used in how I do it in Oracle. </p>
<p><strong>MONTHS_BETWEEN</strong><br />
<code><br />
SELECT TIMESTAMPDIFF(<br />
MONTH , '2010-01-07', SYSDATE( ) )  no_months<br />
</code></p>
<table>
<tr>
<td><strong>Specifier</strong></td>
<td><strong>Description</strong></td>
</tr>
<tr>
<td><code>%a</code></td>
<td>Abbreviated weekday name                   (<code>Sun</code>..<code>Sat</code>)</td>
</tr>
<tr>
<td><code>%b</code></td>
<td>Abbreviated month name (<code>Jan</code>..<code>Dec</code>)</td>
</tr>
<tr>
<td><code>%c</code></td>
<td>Month, numeric (<code>0</code>..<code>12</code>)</td>
</tr>
<tr>
<td><code>%D</code></td>
<td>Day of the month with English suffix (<code>0th</code>,                   <code>1st</code>, <code>2nd</code>,                   <code>3rd</code>, …)</td>
</tr>
<tr>
<td><code>%d</code></td>
<td>Day of the month, numeric (<code>00</code>..<code>31</code>)</td>
</tr>
<tr>
<td><code>%e</code></td>
<td>Day of the month, numeric (<code>0</code>..<code>31</code>)</td>
</tr>
<tr>
<td><code>%f</code></td>
<td>Microseconds (<code>000000</code>..<code>999999</code>)</td>
</tr>
<tr>
<td><code>%H</code></td>
<td>Hour (<code>00</code>..<code>23</code>)</td>
</tr>
<tr>
<td><code>%h</code></td>
<td>Hour (<code>01</code>..<code>12</code>)</td>
</tr>
<tr>
<td><code>%I</code></td>
<td>Hour (<code>01</code>..<code>12</code>)</td>
</tr>
<tr>
<td><code>%i</code></td>
<td>Minutes, numeric (<code>00</code>..<code>59</code>)</td>
</tr>
<tr>
<td><code>%j</code></td>
<td>Day of year (<code>001</code>..<code>366</code>)</td>
</tr>
<tr>
<td><code>%k</code></td>
<td>Hour (<code>0</code>..<code>23</code>)</td>
</tr>
<tr>
<td><code>%l</code></td>
<td>Hour (<code>1</code>..<code>12</code>)</td>
</tr>
<tr>
<td><code>%M</code></td>
<td>Month name (<code>January</code>..<code>December</code>)</td>
</tr>
<tr>
<td><code>%m</code></td>
<td>Month, numeric (<code>00</code>..<code>12</code>)</td>
</tr>
<tr>
<td><code>%p</code></td>
<td><code>AM</code> or <code>PM</code></td>
</tr>
<tr>
<td><code>%r</code></td>
<td>Time, 12-hour (<code>hh:mm:ss</code> followed by                   <code>AM</code> or <code>PM</code>)</td>
</tr>
<tr>
<td><code>%S</code></td>
<td>Seconds (<code>00</code>..<code>59</code>)</td>
</tr>
<tr>
<td><code>%s</code></td>
<td>Seconds (<code>00</code>..<code>59</code>)</td>
</tr>
<tr>
<td><code>%T</code></td>
<td>Time, 24-hour (<code>hh:mm:ss</code>)</td>
</tr>
<tr>
<td><code>%U</code></td>
<td>Week (<code>00</code>..<code>53</code>), where Sunday is the                   first day of the week</td>
</tr>
<tr>
<td><code>%u</code></td>
<td>Week (<code>00</code>..<code>53</code>), where Monday is the                   first day of the week</td>
</tr>
<tr>
<td><code>%V</code></td>
<td>Week (<code>01</code>..<code>53</code>), where Sunday is the                   first day of the week; used with <code>%X</code></td>
</tr>
<tr>
<td><code>%v</code></td>
<td>Week (<code>01</code>..<code>53</code>), where Monday is the                   first day of the week; used with <code>%x</code></td>
</tr>
<tr>
<td><code>%W</code></td>
<td>Weekday name (<code>Sunday</code>..<code>Saturday</code>)</td>
</tr>
<tr>
<td><code>%w</code></td>
<td>Day of the week                   (<code>0</code>=Sunday..<code>6</code>=Saturday)</td>
</tr>
<tr>
<td><code>%X</code></td>
<td>Year for the week where Sunday is the first day of the week, numeric,                   four digits; used with <code>%V</code></td>
</tr>
<tr>
<td><code>%x</code></td>
<td>Year for the week, where Monday is the first day of the week, numeric,                   four digits; used with <code>%v</code></td>
</tr>
<tr>
<td><code>%Y</code></td>
<td>Year, numeric, four digits</td>
</tr>
<tr>
<td><code>%y</code></td>
<td>Year, numeric (two digits)</td>
</tr>
<tr>
<td><code>%%</code></td>
<td>A literal “<code>%</code>” character</td>
</tr>
<tr>
<td><code>%<em><code>x</code></em></code></td>
<td><em><code>x</code></em>, for any                   “<em><code>x</code></em>” not listed                   above</td>
</tr>
</table>
<p>Reference:</p>
<p>http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html</p>
]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/date-function-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle NVL Equivalent in MySQL</title>
		<link>http://kwatog.com/blog/mysql/oracle-nvl-equivalent-in-mysql/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=oracle-nvl-equivalent-in-mysql</link>
		<comments>http://kwatog.com/blog/mysql/oracle-nvl-equivalent-in-mysql/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 01:59:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[isnull]]></category>
		<category><![CDATA[nvl]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[sybase]]></category>

		<guid isPermaLink="false">http://kwatog.com/?p=204</guid>
		<description><![CDATA[Here&#8217;s the MySQL equivalent of Oracle&#8217;s NVL and MSSQL/Sybase&#8217;s ISNULL functions. Syntax IFNULL(expr1,expr2) Usage If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. Take note that IFNULL() returns a numeric or string value, depending on the context in which it is used. I haven&#8217;t used it for other data types yet but&#8230;]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s the MySQL equivalent of Oracle&#8217;s NVL and MSSQL/Sybase&#8217;s ISNULL functions.</p>
<h3>Syntax</h3>
<p><code><br />
IFNULL(expr1,expr2)<br />
</code></p>
<h3>Usage</h3>
<p>If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. Take note that IFNULL() returns a numeric or string value, depending on the context in which it is used. I haven&#8217;t used it for other data types yet but I reckon it would have the same results. That is to return the same data type as that of expr1. Another important thing to remember is that expr1 and expr2 should have the same data type</p>
]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/mysql/oracle-nvl-equivalent-in-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQL SEQUENCE Equivalent</title>
		<link>http://kwatog.com/blog/mysql/mysql-sequence-equivalent/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mysql-sequence-equivalent</link>
		<comments>http://kwatog.com/blog/mysql/mysql-sequence-equivalent/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 03:37:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[nextval]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[sequence]]></category>

		<guid isPermaLink="false">http://kwatog.com/?p=197</guid>
		<description><![CDATA[Most Oracle guys (and I heard PostgreSQL, too) will be missing sequence in MySQL. The nearest equivalent that MySQL has for this feature is the AUTO INCREMENT field. However, auto increment is sometimes not sufficient and time will come that you will be missing it. Case in point is the alpha-numeric auto-generated code. In Oracle,&#8230;]]></description>
			<content:encoded><![CDATA[<p>Most Oracle guys (and I heard PostgreSQL, too) will be missing sequence in MySQL. The nearest equivalent that MySQL has for this feature is the AUTO INCREMENT field. However, auto increment is sometimes not sufficient and time will come that you will be missing it. </p>
<p>Case in point is the alpha-numeric auto-generated code. In Oracle, it&#8217;s easy to generate AA123, AA124, AA125, etc., etc. All we have to do is concat the alpha part (AA) and with the nextval of sequence. For MySQL, the alpha part and the numeric part have to be saved in separate fields.</p>
<p>However, Maresa of Microshell has created an alternative solution for it. The article discusses a way to emulate the nextval function in mysql.  It a nutshell, they created a table similar to the all_sequences of Oracle. This is where you insert your sequences. Then, create a function named nextval() function which increments and returns value of the sequence.<br />
You can check the procedure <a href="http://www.microshell.com/database/mysql/emulating-nextval-function-to-get-sequence-in-mysql/1942">here</a>.</p>
<p>It&#8217;s pretty neat, actually. It&#8217;s an excellent alternative and you can even create similar functions like currval(), prevval(), among others. However, I have some reservations on his approach especially on the issue of concurrency. You see in Oracle, calling the nextval() will increment the value and that&#8217;s already persisted (saved) in the sequence table even without saving your query/dml statement. On this approach, other queries accessing the pseudo-nextval will have to wait until your query finishes. Not a good idea. </p>
<p>So for now, I&#8217;ll will still have to make do with whatever MySQL is providing. If there&#8217;s any consolation, MySQL provides LAST_INSERT_ID()  function in case you want to get the latest id inserted. See details here <a href="http://dev.mysql.com/doc/refman/5.0/en/information-functions.html">here</a>. </p>
<p>So I&#8217;m just hoping that MySQL will implement an equivalent feature in the near future. With the imminent takeover by Oracle, I&#8217;m actually hoping that development of MySQL will push through and take a steam. We all know that the development of this widely used open-source database engine has been hampered ever since Sun bought it. </p>
<p>Sequence is a little feature. But little as it seems, it solves a lot of problems and helps improve code development time.</p>
]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/mysql/mysql-sequence-equivalent/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>
	</channel>
</rss>

