<?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>Dev Corner</title>
	<atom:link href="http://devcorner.georgievi.net/feed" rel="self" type="application/rss+xml" />
	<link>http://devcorner.georgievi.net</link>
	<description>Software Developer's Notepad</description>
	<lastBuildDate>Fri, 11 Dec 2009 07:49:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Oracle PL/SQL Collections and Associative Arrays</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_plsql/plsql_collections</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_plsql/plsql_collections#comments</comments>
		<pubDate>Thu, 10 Dec 2009 13:56:07 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[associative arrays]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[oracle database]]></category>
		<category><![CDATA[oracle plsql]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=370</guid>
		<description><![CDATA[Associative Arrays

DECLARE
  TYPE population_type IS TABLE OF NUMBER INDEX BY VARCHAR2&#40;64&#41;;
  country_population population_type;
  continent_population population_type;
  howmany NUMBER;
  which VARCHAR2&#40;64&#41;;
&#160;
BEGIN
  country_population&#40;'Greenland'&#41; := 100000;
  country_population&#40;'Iceland'&#41; := 750000;
  howmany := country_population&#40;'Greenland'&#41;;
&#160;
  continent_population&#40;'Australia'&#41; := 30000000;
  continent_population&#40;'Antarctica'&#41; := 1000; -- Creates new entry
  continent_population&#40;'Antarctica'&#41; := 1001; -- Replaces previous [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Associative Arrays</strong></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">DECLARE
  TYPE population_type <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">TABLE</span> OF NUMBER <span style="color: #993333; font-weight: bold;">INDEX</span> <span style="color: #993333; font-weight: bold;">BY</span> VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span>;
  country_population population_type;
  continent_population population_type;
  howmany NUMBER;
  which VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
BEGIN
  country_population<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Greenland'</span><span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">100000</span>;
  country_population<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Iceland'</span><span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">750000</span>;
  howmany :<span style="color: #66cc66;">=</span> country_population<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Greenland'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  continent_population<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Australia'</span><span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">30000000</span>;
  continent_population<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Antarctica'</span><span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1000</span>; <span style="color: #808080; font-style: italic;">-- Creates new entry</span>
  continent_population<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Antarctica'</span><span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1001</span>; <span style="color: #808080; font-style: italic;">-- Replaces previous value</span>
  dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Count: '</span> <span style="color: #66cc66;">||</span> continent_population<span style="color: #66cc66;">.</span>count<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  which :<span style="color: #66cc66;">=</span> continent_population<span style="color: #66cc66;">.</span>FIRST; <span style="color: #808080; font-style: italic;">-- Returns 'Antarctica'</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- as that comes first alphabetically.</span>
  which :<span style="color: #66cc66;">=</span> continent_population<span style="color: #66cc66;">.</span>LAST; <span style="color: #808080; font-style: italic;">-- Returns 'Australia'</span>
  howmany :<span style="color: #66cc66;">=</span> continent_population<span style="color: #66cc66;">&#40;</span>continent_population<span style="color: #66cc66;">.</span>LAST<span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">-- Returns the value corresponding to the last key, in this</span>
<span style="color: #808080; font-style: italic;">-- case the population of Australia.</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Traverse Continents Associative Array</span>
  dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Elements in continent_population: '</span> <span style="color: #66cc66;">||</span> continent_population<span style="color: #66cc66;">.</span>count<span style="color: #66cc66;">&#41;</span>;
  which :<span style="color: #66cc66;">=</span> continent_population<span style="color: #66cc66;">.</span>FIRST;
  WHILE which <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> LOOP
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>which <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">': '</span> <span style="color: #66cc66;">||</span> continent_population<span style="color: #66cc66;">&#40;</span>which<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    which :<span style="color: #66cc66;">=</span> continent_population<span style="color: #66cc66;">.</span>NEXT<span style="color: #66cc66;">&#40;</span>which<span style="color: #66cc66;">&#41;</span>;
  END LOOP;
  <span style="color: #808080; font-style: italic;">-- Outputs:</span>
  <span style="color: #808080; font-style: italic;">--   Count: 2</span>
  <span style="color: #808080; font-style: italic;">--   Elements in continent_population: 2</span>
  <span style="color: #808080; font-style: italic;">--   Antarctica: 1001</span>
  <span style="color: #808080; font-style: italic;">--   Australia: 30000000</span>
&nbsp;
END;</pre></div></div>

<p><strong>Extend Oracle PL/SQL Collection</strong></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">DECLARE
  TYPE CourseList <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">TABLE</span> OF VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
  courses CourseList :<span style="color: #66cc66;">=</span> CourseList<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
BEGIN
  dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Initial count= '</span> <span style="color: #66cc66;">||</span> courses<span style="color: #66cc66;">.</span>count <span style="color: #66cc66;">||</span>
                       <span style="color: #ff0000;">', first= '</span> <span style="color: #66cc66;">||</span> courses<span style="color: #66cc66;">.</span>first <span style="color: #66cc66;">||</span>
                       <span style="color: #ff0000;">', last= '</span> <span style="color: #66cc66;">||</span> courses<span style="color: #66cc66;">.</span>last <span style="color: #66cc66;">||</span>
                       <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;
  courses<span style="color: #66cc66;">.</span>extend<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
  dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'After extend(5) count= '</span> <span style="color: #66cc66;">||</span> courses<span style="color: #66cc66;">.</span>count <span style="color: #66cc66;">||</span>
                       <span style="color: #ff0000;">', first= '</span> <span style="color: #66cc66;">||</span> courses<span style="color: #66cc66;">.</span>first <span style="color: #66cc66;">||</span>
                       <span style="color: #ff0000;">', last= '</span> <span style="color: #66cc66;">||</span> courses<span style="color: #66cc66;">.</span>last <span style="color: #66cc66;">||</span>
                       <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;
  dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'After extend(5) count: '</span> <span style="color: #66cc66;">||</span> courses<span style="color: #66cc66;">.</span>count<span style="color: #66cc66;">&#41;</span>;
&nbsp;
END;</pre></div></div>

<pre>
Initial count= 0, first= , last=
After extend(5) count= 5, first= 1, last= 5
After extend(5) count: 5
</pre>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_plsql/plsql_collections/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle Database Multitable INSERT SQL</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_sql/sql_insert/oracle-multitable-insert</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_sql/sql_insert/oracle-multitable-insert#comments</comments>
		<pubDate>Thu, 10 Dec 2009 09:30:56 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[SQL INSERT]]></category>
		<category><![CDATA[oracle database]]></category>
		<category><![CDATA[oracle sql]]></category>
		<category><![CDATA[oracle sql insert]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=362</guid>
		<description><![CDATA[Multitable INSERT SQL can insert simultaneously into more than one tables. You can also specify optional conditions which determine which values are inserted into which tables.

SELECT * FROM sales_input_table;
&#160;
PRODUCT_ID CUSTOMER_ID WEEKLY_ST  SALES_SUN  SALES_MON  SALES_TUE  SALES_WED SALES_THU  SALES_FRI  SALES_SAT
---------- ----------- --------- ---------- ---------- ---------- -------------------- ---------- ----------
    [...]]]></description>
			<content:encoded><![CDATA[<p>Multitable INSERT SQL can insert simultaneously into more than one tables. You can also specify optional conditions which determine which values are inserted into which tables.<span id="more-362"></span></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> sales_input_table;
&nbsp;
PRODUCT_ID CUSTOMER_ID WEEKLY_ST  SALES_SUN  SALES_MON  SALES_TUE  SALES_WED SALES_THU  SALES_FRI  SALES_SAT
<span style="color: #808080; font-style: italic;">---------- ----------- --------- ---------- ---------- ---------- -------------------- ---------- ----------</span>
       <span style="color: #cc66cc;">111</span>         <span style="color: #cc66cc;">222</span> 01<span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00        <span style="color: #cc66cc;">100</span>        <span style="color: #cc66cc;">200</span>        <span style="color: #cc66cc;">300</span>        <span style="color: #cc66cc;">400</span>       <span style="color: #cc66cc;">500</span>        <span style="color: #cc66cc;">600</span>        <span style="color: #cc66cc;">700</span>
       <span style="color: #cc66cc;">222</span>         <span style="color: #cc66cc;">333</span> 08<span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00        <span style="color: #cc66cc;">200</span>        <span style="color: #cc66cc;">300</span>        <span style="color: #cc66cc;">400</span>        <span style="color: #cc66cc;">500</span>       <span style="color: #cc66cc;">600</span>        <span style="color: #cc66cc;">700</span>        <span style="color: #cc66cc;">800</span>
       <span style="color: #cc66cc;">333</span>         <span style="color: #cc66cc;">444</span> <span style="color: #cc66cc;">15</span><span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00        <span style="color: #cc66cc;">300</span>        <span style="color: #cc66cc;">400</span>        <span style="color: #cc66cc;">500</span>        <span style="color: #cc66cc;">600</span>       <span style="color: #cc66cc;">700</span>        <span style="color: #cc66cc;">800</span>        <span style="color: #cc66cc;">900</span></pre></div></div>

<p>The multitable insert statement looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">ALL</span>
      <span style="color: #993333; font-weight: bold;">INTO</span> sales <span style="color: #66cc66;">&#40;</span>prod_id<span style="color: #66cc66;">,</span> cust_id<span style="color: #66cc66;">,</span> time_id<span style="color: #66cc66;">,</span> amount<span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>product_id<span style="color: #66cc66;">,</span> customer_id<span style="color: #66cc66;">,</span> weekly_start_date<span style="color: #66cc66;">,</span> sales_sun<span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">INTO</span> sales <span style="color: #66cc66;">&#40;</span>prod_id<span style="color: #66cc66;">,</span> cust_id<span style="color: #66cc66;">,</span> time_id<span style="color: #66cc66;">,</span> amount<span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>product_id<span style="color: #66cc66;">,</span> customer_id<span style="color: #66cc66;">,</span> weekly_start_date<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> sales_mon<span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">INTO</span> sales <span style="color: #66cc66;">&#40;</span>prod_id<span style="color: #66cc66;">,</span> cust_id<span style="color: #66cc66;">,</span> time_id<span style="color: #66cc66;">,</span> amount<span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>product_id<span style="color: #66cc66;">,</span> customer_id<span style="color: #66cc66;">,</span> weekly_start_date<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> sales_tue<span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">INTO</span> sales <span style="color: #66cc66;">&#40;</span>prod_id<span style="color: #66cc66;">,</span> cust_id<span style="color: #66cc66;">,</span> time_id<span style="color: #66cc66;">,</span> amount<span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>product_id<span style="color: #66cc66;">,</span> customer_id<span style="color: #66cc66;">,</span> weekly_start_date<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> sales_wed<span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">INTO</span> sales <span style="color: #66cc66;">&#40;</span>prod_id<span style="color: #66cc66;">,</span> cust_id<span style="color: #66cc66;">,</span> time_id<span style="color: #66cc66;">,</span> amount<span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>product_id<span style="color: #66cc66;">,</span> customer_id<span style="color: #66cc66;">,</span> weekly_start_date<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">,</span> sales_thu<span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">INTO</span> sales <span style="color: #66cc66;">&#40;</span>prod_id<span style="color: #66cc66;">,</span> cust_id<span style="color: #66cc66;">,</span> time_id<span style="color: #66cc66;">,</span> amount<span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>product_id<span style="color: #66cc66;">,</span> customer_id<span style="color: #66cc66;">,</span> weekly_start_date<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span> sales_fri<span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">INTO</span> sales <span style="color: #66cc66;">&#40;</span>prod_id<span style="color: #66cc66;">,</span> cust_id<span style="color: #66cc66;">,</span> time_id<span style="color: #66cc66;">,</span> amount<span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>product_id<span style="color: #66cc66;">,</span> customer_id<span style="color: #66cc66;">,</span> weekly_start_date<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span> sales_sat<span style="color: #66cc66;">&#41;</span>
   <span style="color: #993333; font-weight: bold;">SELECT</span> product_id<span style="color: #66cc66;">,</span> customer_id<span style="color: #66cc66;">,</span> weekly_start_date<span style="color: #66cc66;">,</span> sales_sun<span style="color: #66cc66;">,</span>
      sales_mon<span style="color: #66cc66;">,</span> sales_tue<span style="color: #66cc66;">,</span> sales_wed<span style="color: #66cc66;">,</span> sales_thu<span style="color: #66cc66;">,</span> sales_fri<span style="color: #66cc66;">,</span> sales_sat
      <span style="color: #993333; font-weight: bold;">FROM</span> sales_input_table;</pre></div></div>

<p>Assuming these are the only rows in the sales table, the contents now look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> sales
   <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> prod_id<span style="color: #66cc66;">,</span> cust_id<span style="color: #66cc66;">,</span> time_id;
&nbsp;
   PROD_ID    CUST_ID TIME_ID   C   PROMO_ID QUANTITY_SOLD     AMOUNT       COST
<span style="color: #808080; font-style: italic;">---------- ---------- --------- - ---------- ------------- ---------- ----------</span>
       <span style="color: #cc66cc;">111</span>        <span style="color: #cc66cc;">222</span> 01<span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">100</span>
       <span style="color: #cc66cc;">111</span>        <span style="color: #cc66cc;">222</span> 02<span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">200</span>
       <span style="color: #cc66cc;">111</span>        <span style="color: #cc66cc;">222</span> 03<span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">300</span>
       <span style="color: #cc66cc;">111</span>        <span style="color: #cc66cc;">222</span> 04<span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">400</span>
       <span style="color: #cc66cc;">111</span>        <span style="color: #cc66cc;">222</span> 05<span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">500</span>
       <span style="color: #cc66cc;">111</span>        <span style="color: #cc66cc;">222</span> 06<span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">600</span>
       <span style="color: #cc66cc;">111</span>        <span style="color: #cc66cc;">222</span> 07<span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">700</span>
       <span style="color: #cc66cc;">222</span>        <span style="color: #cc66cc;">333</span> 08<span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">200</span>
       <span style="color: #cc66cc;">222</span>        <span style="color: #cc66cc;">333</span> 09<span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">300</span>
       <span style="color: #cc66cc;">222</span>        <span style="color: #cc66cc;">333</span> <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">400</span>
       <span style="color: #cc66cc;">222</span>        <span style="color: #cc66cc;">333</span> <span style="color: #cc66cc;">11</span><span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">500</span>
       <span style="color: #cc66cc;">222</span>        <span style="color: #cc66cc;">333</span> <span style="color: #cc66cc;">12</span><span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">600</span>
       <span style="color: #cc66cc;">222</span>        <span style="color: #cc66cc;">333</span> <span style="color: #cc66cc;">13</span><span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">700</span>
       <span style="color: #cc66cc;">222</span>        <span style="color: #cc66cc;">333</span> <span style="color: #cc66cc;">14</span><span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">800</span>
       <span style="color: #cc66cc;">333</span>        <span style="color: #cc66cc;">444</span> <span style="color: #cc66cc;">15</span><span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">300</span>
       <span style="color: #cc66cc;">333</span>        <span style="color: #cc66cc;">444</span> <span style="color: #cc66cc;">16</span><span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">400</span>
       <span style="color: #cc66cc;">333</span>        <span style="color: #cc66cc;">444</span> <span style="color: #cc66cc;">17</span><span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">500</span>
       <span style="color: #cc66cc;">333</span>        <span style="color: #cc66cc;">444</span> <span style="color: #cc66cc;">18</span><span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">600</span>
       <span style="color: #cc66cc;">333</span>        <span style="color: #cc66cc;">444</span> <span style="color: #cc66cc;">19</span><span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">700</span>
       <span style="color: #cc66cc;">333</span>        <span style="color: #cc66cc;">444</span> <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">800</span>
       <span style="color: #cc66cc;">333</span>        <span style="color: #cc66cc;">444</span> <span style="color: #cc66cc;">21</span><span style="color: #66cc66;">-</span>OCT<span style="color: #66cc66;">-</span>00                                   <span style="color: #cc66cc;">900</span></pre></div></div>

<p>The next examples insert into multiple tables. Suppose you want to provide to sales representatives some information on orders of various sizes. The following example creates tables for small, medium, large, and special orders and populates those tables with data from the sample table oe.orders:</p>
<pre>
CREATE TABLE small_orders
   (order_id       NUMBER(12)   NOT NULL,
    customer_id    NUMBER(6)    NOT NULL,
    order_total    NUMBER(8,2),
    sales_rep_id   NUMBER(6)
   );

CREATE TABLE medium_orders AS SELECT * FROM small_orders;

CREATE TABLE large_orders AS SELECT * FROM small_orders;

CREATE TABLE special_orders
   (order_id       NUMBER(12)    NOT NULL,
    customer_id    NUMBER(6)     NOT NULL,
    order_total    NUMBER(8,2),
    sales_rep_id   NUMBER(6),
    credit_limit   NUMBER(9,2),
    cust_email     VARCHAR2(30)
   );
</pre>
<p>The first multitable insert populates only the tables for small, medium, and large orders:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">ALL</span>
   WHEN order_total <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">1000000</span> THEN
      <span style="color: #993333; font-weight: bold;">INTO</span> small_orders
   WHEN order_total <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">1000000</span> <span style="color: #993333; font-weight: bold;">AND</span> order_total <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">2000000</span> THEN
      <span style="color: #993333; font-weight: bold;">INTO</span> medium_orders
   WHEN order_total <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">2000000</span> THEN
      <span style="color: #993333; font-weight: bold;">INTO</span> large_orders
   <span style="color: #993333; font-weight: bold;">SELECT</span> order_id<span style="color: #66cc66;">,</span> order_total<span style="color: #66cc66;">,</span> sales_rep_id<span style="color: #66cc66;">,</span> customer_id
      <span style="color: #993333; font-weight: bold;">FROM</span> orders;</pre></div></div>

<p>You can accomplish the same thing using the ELSE clause in place of the insert into the large_orders table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">ALL</span>
   WHEN order_total <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">100000</span> THEN
      <span style="color: #993333; font-weight: bold;">INTO</span> small_orders
   WHEN order_total <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">100000</span> <span style="color: #993333; font-weight: bold;">AND</span> order_total <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">200000</span> THEN
      <span style="color: #993333; font-weight: bold;">INTO</span> medium_orders
   ELSE
      <span style="color: #993333; font-weight: bold;">INTO</span> large_orders
   <span style="color: #993333; font-weight: bold;">SELECT</span> order_id<span style="color: #66cc66;">,</span> order_total<span style="color: #66cc66;">,</span> sales_rep_id<span style="color: #66cc66;">,</span> customer_id
      <span style="color: #993333; font-weight: bold;">FROM</span> orders;
<span style="color: #66cc66;">&lt;</span>pre<span style="color: #66cc66;">&gt;</span>
&nbsp;
The next example inserts <span style="color: #993333; font-weight: bold;">INTO</span> the small<span style="color: #66cc66;">,</span> medium<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">AND</span> large <span style="color: #993333; font-weight: bold;">TABLES</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">IN</span> the preceding example<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">AND</span> also puts orders greater than <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">900</span><span style="color: #66cc66;">,</span>000 <span style="color: #993333; font-weight: bold;">INTO</span> the special_orders <span style="color: #993333; font-weight: bold;">TABLE</span><span style="color: #66cc66;">.</span> This <span style="color: #993333; font-weight: bold;">TABLE</span> also shows how <span style="color: #993333; font-weight: bold;">TO</span> <span style="color: #993333; font-weight: bold;">USE</span> <span style="color: #993333; font-weight: bold;">COLUMN</span> aliases <span style="color: #993333; font-weight: bold;">TO</span> simplify the statement:
&nbsp;
<span style="color: #66cc66;">&lt;</span>pre lang<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sql&quot;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">ALL</span>
   WHEN ottl <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">100000</span> THEN
      <span style="color: #993333; font-weight: bold;">INTO</span> small_orders
         <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span>oid<span style="color: #66cc66;">,</span> ottl<span style="color: #66cc66;">,</span> sid<span style="color: #66cc66;">,</span> cid<span style="color: #66cc66;">&#41;</span>
   WHEN ottl <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">100000</span> <span style="color: #993333; font-weight: bold;">AND</span> ottl <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">200000</span> THEN
      <span style="color: #993333; font-weight: bold;">INTO</span> medium_orders 
         <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span>oid<span style="color: #66cc66;">,</span> ottl<span style="color: #66cc66;">,</span> sid<span style="color: #66cc66;">,</span> cid<span style="color: #66cc66;">&#41;</span>
   WHEN ottl <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">200000</span> THEN
      <span style="color: #993333; font-weight: bold;">INTO</span> large_orders
         <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span>oid<span style="color: #66cc66;">,</span> ottl<span style="color: #66cc66;">,</span> sid<span style="color: #66cc66;">,</span> cid<span style="color: #66cc66;">&#41;</span>
   WHEN ottl <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">290000</span> THEN
      <span style="color: #993333; font-weight: bold;">INTO</span> special_orders
   <span style="color: #993333; font-weight: bold;">SELECT</span> o<span style="color: #66cc66;">.</span>order_id oid<span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>customer_id cid<span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>order_total ottl<span style="color: #66cc66;">,</span>
      o<span style="color: #66cc66;">.</span>sales_rep_id sid<span style="color: #66cc66;">,</span> c<span style="color: #66cc66;">.</span>credit_limit cl<span style="color: #66cc66;">,</span> c<span style="color: #66cc66;">.</span>cust_email cem
      <span style="color: #993333; font-weight: bold;">FROM</span> orders o<span style="color: #66cc66;">,</span> customers c
      <span style="color: #993333; font-weight: bold;">WHERE</span> o<span style="color: #66cc66;">.</span>customer_id <span style="color: #66cc66;">=</span> c<span style="color: #66cc66;">.</span>customer_id;</pre></div></div>

<p>Finally, the next example uses the FIRST clause to put orders greater than 2,900,000 into the special_orders table and exclude those orders from the large_orders table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> FIRST
   WHEN ottl <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">100000</span> THEN
      <span style="color: #993333; font-weight: bold;">INTO</span> small_orders
         <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span>oid<span style="color: #66cc66;">,</span> ottl<span style="color: #66cc66;">,</span> sid<span style="color: #66cc66;">,</span> cid<span style="color: #66cc66;">&#41;</span>
   WHEN ottl <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">100000</span> <span style="color: #993333; font-weight: bold;">AND</span> ottl <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">200000</span> THEN
      <span style="color: #993333; font-weight: bold;">INTO</span> medium_orders
         <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span>oid<span style="color: #66cc66;">,</span> ottl<span style="color: #66cc66;">,</span> sid<span style="color: #66cc66;">,</span> cid<span style="color: #66cc66;">&#41;</span>
   WHEN ottl <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">290000</span> THEN
      <span style="color: #993333; font-weight: bold;">INTO</span> special_orders
   WHEN ottl <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">200000</span> THEN
      <span style="color: #993333; font-weight: bold;">INTO</span> large_orders
         <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span>oid<span style="color: #66cc66;">,</span> ottl<span style="color: #66cc66;">,</span> sid<span style="color: #66cc66;">,</span> cid<span style="color: #66cc66;">&#41;</span>
   <span style="color: #993333; font-weight: bold;">SELECT</span> o<span style="color: #66cc66;">.</span>order_id oid<span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>customer_id cid<span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>order_total ottl<span style="color: #66cc66;">,</span>
      o<span style="color: #66cc66;">.</span>sales_rep_id sid<span style="color: #66cc66;">,</span> c<span style="color: #66cc66;">.</span>credit_limit cl<span style="color: #66cc66;">,</span> c<span style="color: #66cc66;">.</span>cust_email cem
      <span style="color: #993333; font-weight: bold;">FROM</span> orders o<span style="color: #66cc66;">,</span> customers c
      <span style="color: #993333; font-weight: bold;">WHERE</span> o<span style="color: #66cc66;">.</span>customer_id <span style="color: #66cc66;">=</span> c<span style="color: #66cc66;">.</span>customer_id;</pre></div></div>

<p><strong>Oracle Multi-table Insert Syntax</strong></p>
<pre>

INSERT [hint]
   {
      ALL insert_into_clause [values_clause] [error_logging_clause]
            [insert_into_clause [values_clause] [error_logging_clause]]...
      |
      conditional_insert_clause
   }

conditional_insert_clause ::=
   [ALL | FIRST]
   WHEN condition
      THEN
	     insert_into_clause
            [ values_clause ]
            [ error_logging_clause ]
         [ insert_into_clause
            [ values_clause ]
            [ error_logging_clause ]
         ]...
   [ WHEN condition
      THEN
	     insert_into_clause
            [ values_clause ]
            [ error_logging_clause ]
         [ insert_into_clause
            [ values_clause ]
            [ error_logging_clause ]
         ]...
   ]...

   [ ELSE insert_into_clause
      [ values_clause ]
         [ error_logging_clause ]
      [ insert_into_clause
         [ values_clause ]
         [ error_logging_clause ]
      ]...
   ]

insert_into_clause :=
   INTO dml_table_expression_clause [ t_alias ]
      [ (column [, column ]...) ]

values_clause :=
   VALUES ({ expr | DEFAULT }
      [, { expr | DEFAULT } ]...
   )
</pre>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_sql/sql_insert/oracle-multitable-insert/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find SQL Executed by a Session &#8211; Oracle</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_dba/session-sql</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_dba/session-sql#comments</comments>
		<pubDate>Tue, 08 Dec 2009 13:32:07 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[Oracle DBA (Database Administration)]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=359</guid>
		<description><![CDATA[The following SQL shows the SQL text for the SQL being executed by a session.

SELECT s.sid,
       q.sql_text
  FROM v$sqltext q
       JOIN v$session s ON q.address = s.sql_address
 WHERE s.sid = &#38;sid
 ORDER BY piece;

]]></description>
			<content:encoded><![CDATA[<p>The following SQL shows the SQL text for the SQL being executed by a session.<span id="more-359"></span></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> s<span style="color: #66cc66;">.</span>sid<span style="color: #66cc66;">,</span>
       q<span style="color: #66cc66;">.</span>sql_text
  <span style="color: #993333; font-weight: bold;">FROM</span> v$sqltext q
       <span style="color: #993333; font-weight: bold;">JOIN</span> v$session s <span style="color: #993333; font-weight: bold;">ON</span> q<span style="color: #66cc66;">.</span>address <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>sql_address
 <span style="color: #993333; font-weight: bold;">WHERE</span> s<span style="color: #66cc66;">.</span>sid <span style="color: #66cc66;">=</span> &amp;sid
 <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> piece;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_dba/session-sql/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Show the Longest 10 Active User Sessions</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_dba/10-longest-active-oracle</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_dba/10-longest-active-oracle#comments</comments>
		<pubDate>Mon, 07 Dec 2009 13:34:54 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[Oracle DBA (Database Administration)]]></category>
		<category><![CDATA[dba]]></category>
		<category><![CDATA[dba sql]]></category>
		<category><![CDATA[oracle database]]></category>
		<category><![CDATA[session]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=352</guid>
		<description><![CDATA[The following SQL can be used by Oracle Database Administrator (DBA) to retrieve information about the currently active sessions with longest execution. The SQL statement is designed to run with SQL*Plus.
To show a list with top current sessions with longest acivity execute in SQL*Plus:

/*
  longest10active.sqlplus.sql
  shows the top 10 active sessions with longest [...]]]></description>
			<content:encoded><![CDATA[<p>The following SQL can be used by Oracle Database Administrator (DBA) to retrieve information about the currently active sessions with longest execution. The SQL statement is designed to run with SQL*Plus.<span id="more-352"></span></p>
<p>To show a list with top current sessions with longest acivity execute in SQL*Plus:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
  longest10active.sqlplus.sql
  shows the top 10 active sessions with longest execution activity
  execute in SQL*Plus
*/</span>
col osuser format a10 trunc
col LastCallET format <span style="color: #cc66cc;">99</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">999</span>
col sid format <span style="color: #cc66cc;">9999</span>
col spid formar <span style="color: #cc66cc;">999999</span>
col username format a10 trunc
col uprogram format a25 trunc
col machine format a10 trunc
<span style="color: #993333; font-weight: bold;">SET</span> linesize <span style="color: #cc66cc;">132</span>
<span style="color: #993333; font-weight: bold;">SET</span> verify off
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span>
   <span style="color: #993333; font-weight: bold;">SELECT</span> TO_CHAR<span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">.</span>logon_time<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'mm/dd hh:mi:ssAM'</span><span style="color: #66cc66;">&#41;</span> loggedon<span style="color: #66cc66;">,</span>
          s<span style="color: #66cc66;">.</span>sid<span style="color: #66cc66;">,</span> s<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">STATUS</span><span style="color: #66cc66;">,</span>
          floor<span style="color: #66cc66;">&#40;</span>last_call_et<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;LastCallET&quot;</span><span style="color: #66cc66;">,</span>
          s<span style="color: #66cc66;">.</span>username<span style="color: #66cc66;">,</span> s<span style="color: #66cc66;">.</span>osuser<span style="color: #66cc66;">,</span>
          p<span style="color: #66cc66;">.</span>spid<span style="color: #66cc66;">,</span> s<span style="color: #66cc66;">.</span>module <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' - '</span> <span style="color: #66cc66;">||</span> s<span style="color: #66cc66;">.</span>program uprogram<span style="color: #66cc66;">,</span>
          s<span style="color: #66cc66;">.</span>machine<span style="color: #66cc66;">,</span> s<span style="color: #66cc66;">.</span>sql_hash_value
     <span style="color: #993333; font-weight: bold;">FROM</span> v$session s<span style="color: #66cc66;">,</span> v$process p
    <span style="color: #993333; font-weight: bold;">WHERE</span> p<span style="color: #66cc66;">.</span>addr <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>paddr
          <span style="color: #993333; font-weight: bold;">AND</span> s<span style="color: #66cc66;">.</span>type <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'USER'</span>
          <span style="color: #993333; font-weight: bold;">AND</span> module <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
          <span style="color: #993333; font-weight: bold;">AND</span> s<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">STATUS</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'ACTIVE'</span>
    <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #cc66cc;">4</span> <span style="color: #993333; font-weight: bold;">DESC</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">WHERE</span> ROWNUM <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">11</span>;</pre></div></div>

<p>If you want the execute the statement in different tool, you can remove the formatting clauses:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
  longest10active.sql
  shows the top 10 active sessions with longest execution activity
*/</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span>
   <span style="color: #993333; font-weight: bold;">SELECT</span> TO_CHAR<span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">.</span>logon_time<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'mm/dd hh:mi:ssAM'</span><span style="color: #66cc66;">&#41;</span> loggedon<span style="color: #66cc66;">,</span>
          s<span style="color: #66cc66;">.</span>sid<span style="color: #66cc66;">,</span> s<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">STATUS</span><span style="color: #66cc66;">,</span>
          floor<span style="color: #66cc66;">&#40;</span>last_call_et<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;LastCallET&quot;</span><span style="color: #66cc66;">,</span>
          s<span style="color: #66cc66;">.</span>username<span style="color: #66cc66;">,</span> s<span style="color: #66cc66;">.</span>osuser<span style="color: #66cc66;">,</span>
          p<span style="color: #66cc66;">.</span>spid<span style="color: #66cc66;">,</span> s<span style="color: #66cc66;">.</span>module <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' - '</span> <span style="color: #66cc66;">||</span> s<span style="color: #66cc66;">.</span>program uprogram<span style="color: #66cc66;">,</span>
          s<span style="color: #66cc66;">.</span>machine<span style="color: #66cc66;">,</span> s<span style="color: #66cc66;">.</span>sql_hash_value
     <span style="color: #993333; font-weight: bold;">FROM</span> v$session s<span style="color: #66cc66;">,</span> v$process p
    <span style="color: #993333; font-weight: bold;">WHERE</span> p<span style="color: #66cc66;">.</span>addr <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>paddr
          <span style="color: #993333; font-weight: bold;">AND</span> s<span style="color: #66cc66;">.</span>type <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'USER'</span>
          <span style="color: #993333; font-weight: bold;">AND</span> module <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
          <span style="color: #993333; font-weight: bold;">AND</span> s<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">STATUS</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'ACTIVE'</span>
    <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #cc66cc;">4</span> <span style="color: #993333; font-weight: bold;">DESC</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">WHERE</span> ROWNUM <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">11</span>;</pre></div></div>

<h2>References</h2>
<ul>
<li><a href="http://allappsdba.blogspot.com/2009/04/show-top-10-longest-active-user.html">Oracle Applications World: Show the top 10 longest-active user sessions</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_dba/10-longest-active-oracle/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Select and Return Top N Rows from Oracle Database</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_sql/sql_select/select-top-rows</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_sql/sql_select/select-top-rows#comments</comments>
		<pubDate>Mon, 07 Dec 2009 11:55:11 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[SQL SELECT]]></category>
		<category><![CDATA[oracle database]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql select]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=345</guid>
		<description><![CDATA[We often need to limit the number of rows returned by a select statement. Some database servers provide extensions to the select statement. For example, MySql database sever provides LIMIT directive. In Oracle Database each result set has defined a ROWNUM pseudocolumn and ORDER BY clause interact.
Let&#8217;s create a test table with some data loaded [...]]]></description>
			<content:encoded><![CDATA[<p>We often need to limit the number of rows returned by a select statement. Some database servers provide extensions to the select statement. For example, MySql database sever provides LIMIT directive. In Oracle Database each result set has defined a ROWNUM pseudocolumn and ORDER BY clause interact.<span id="more-345"></span></p>
<p>Let&#8217;s create a test table with some data loaded in reverse order.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> numbers <span style="color: #66cc66;">&#40;</span>
  id  NUMBER
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> numbers <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> numbers <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> numbers <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> numbers <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> numbers <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> numbers <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> numbers <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> numbers <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> numbers <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> numbers <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
COMMIT;</pre></div></div>

<p>We want the first 3 rows from the SELECT result set:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> numbers <span style="color: #993333; font-weight: bold;">WHERE</span> ROWNUM <span style="color: #66cc66;">&lt;=</span> <span style="color: #cc66cc;">3</span>;
&nbsp;
     ID
<span style="color: #808080; font-style: italic;">-------</span>
     <span style="color: #cc66cc;">10</span>
      <span style="color: #cc66cc;">9</span>
      <span style="color: #cc66cc;">8</span></pre></div></div>

<p>We want to retrieve only the records with the smallest 3 IDs.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> numbers 
 <span style="color: #993333; font-weight: bold;">WHERE</span> ROWNUM <span style="color: #66cc66;">&lt;=</span> <span style="color: #cc66cc;">3</span>
 <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> id;
&nbsp;
     ID
<span style="color: #808080; font-style: italic;">-------</span>
      <span style="color: #cc66cc;">8</span>
      <span style="color: #cc66cc;">9</span>
     <span style="color: #cc66cc;">10</span></pre></div></div>

<p>The result is not what we expected. The reason is that Oracle first fetches the records based on selection predicate and than sort the resulting rows. In our case, Oracle first filters the first three rows with IDs 10, 9 and 8 and than sorts them.</p>
<p>To achieve the desired result we need first to sort the records and then select the first 3 rows. For this we can use nested selects:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> numbers
                <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> id<span style="color: #66cc66;">&#41;</span>
 <span style="color: #993333; font-weight: bold;">WHERE</span> ROWNUM <span style="color: #66cc66;">&lt;=</span> <span style="color: #cc66cc;">3</span>;
&nbsp;
     ID
<span style="color: #808080; font-style: italic;">-------</span>
      <span style="color: #cc66cc;">1</span>
      <span style="color: #cc66cc;">2</span>
      <span style="color: #cc66cc;">3</span></pre></div></div>

<p>The Oracle first executes the innermost sub-select. Thus Oracle fetches all the rows and orders them. Than Oracle Database executes the outer select which filters only the first 3 rows.</p>
<p>We can use the same technique to retrieve the top 2 rows, i.e. the 2 rows with the biggest ID:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> numbers
                <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> id <span style="color: #993333; font-weight: bold;">DESC</span><span style="color: #66cc66;">&#41;</span>
 <span style="color: #993333; font-weight: bold;">WHERE</span> ROWNUM <span style="color: #66cc66;">&lt;=</span> <span style="color: #cc66cc;">2</span>;
&nbsp;
     ID
<span style="color: #808080; font-style: italic;">-------</span>
     <span style="color: #cc66cc;">10</span>
      <span style="color: #cc66cc;">9</span></pre></div></div>

<p>Select queries that return the first N rows from an ordered result set are also known as Top-N queries.</p>
<h3>References</h3>
<ol>
<li><a href="Top-N Queries">Top-N Queries</a> [http://www.oracle-base.com/articles/misc/TopNQueries.php]</li>
<li><a href="http://codeguru7.blogspot.com/2009/12/select-and-return-top-n-rows-from.html">Top N Oracle Database SELECT Rows</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_sql/sql_select/select-top-rows/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Split long command on Windows / Linux into multiple lines &#8211; multiline command</title>
		<link>http://devcorner.georgievi.net/pages/tips/split-command-lines</link>
		<comments>http://devcorner.georgievi.net/pages/tips/split-command-lines#comments</comments>
		<pubDate>Sun, 06 Dec 2009 21:45:02 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[linux command prompt]]></category>
		<category><![CDATA[windows command prompt]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=342</guid>
		<description><![CDATA[You can easily split long command line into multiple lines.
How to split long commands on multiple lines on Linux
Add a backslash character at the end of line to tell the Linux shell that the next line continues the current line.

   ls \
     -a

How to split long commands on multiple [...]]]></description>
			<content:encoded><![CDATA[<p>You can easily split long command line into multiple lines.<span id="more-342"></span></p>
<p><strong>How to split long commands on multiple lines on Linux</strong></p>
<p>Add a backslash character at the end of line to tell the Linux shell that the next line continues the current line.</p>
<pre>
   ls \
     -a
</pre>
<p><strong>How to split long commands on multiple lines on Windows</strong></p>
<p>Add a ^ character at the end of line to tell the Windows Command Interpreter that the next line continues the current line.</p>
<pre>
  dir ^
    -s
</pre>
<p><strong>Discussion</strong><br />
In the Windows Command Prompt the ^ is used to escape the next character on the command line. (Like \ is used in strings.) Characters that need to be used in the command line as they are should have a ^ prefixed to them, hence that&#8217;s why it works for the newline.</p>
<p>For reference the characters that need escaping (if specified as command arguments and not within quotes) are: &#038;|()</p>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/tips/split-command-lines/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Plugin: bb:Usage Stats</title>
		<link>http://devcorner.georgievi.net/pages/wordpress/wp-plugins/bbusage-stats</link>
		<comments>http://devcorner.georgievi.net/pages/wordpress/wp-plugins/bbusage-stats#comments</comments>
		<pubDate>Sat, 05 Dec 2009 20:32:06 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[WordPress Plugins]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[stats]]></category>
		<category><![CDATA[visits]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=338</guid>
		<description><![CDATA[Track post, page, category and homepage views. Build usage statistics, display usage graphs, top pages, posts, categories.]]></description>
			<content:encoded><![CDATA[<p>bb: Usage Stats collects information about page, post, category and homepage views for your WordPress blog. Builds daily and monthly usage statistics.<span id="more-338"></span></p>
<p>To preserve database storage space, old usage information is purged from the database.</p>
<p>Graphical display of the daily usage statistics. Additional bar-graph chart that displays top 15 pages, posts and categories.</p>
<p>Adds dasboard widget that displays daily usage statistics chart and top 15 pages, post and categories.</p>
<p>You can download the current version from <a href="http://wordpress.org/extend/plugins/bb-usage-stats/" title="bb: Usage Stats home page at WordPress.org">WordPress.org</a>.</p>
<p><strong>Screenshots</strong></p>
<p>Sample daily usage chart.<br />
<img src="http://devcorner.georgievi.net/wp-content/plugins/bb-usage-stats-1.0/screenshot-1.gif" alt="bb: Usage Stats for WordPress: Sample daily usage chart." /></p>
<p>Sample &#8220;Top 15&#8243; bar-graph chart.<br />
<img src="http://devcorner.georgievi.net/wp-content/plugins/bb-usage-stats-1.0/screenshot-2.gif" alt="bb: Usage Stats for WordPress: Sample "Top 15" bar-graph chart." /></p>
<p>Thumbnail view for the full statistics display.<br />
<img src="http://devcorner.georgievi.net/wp-content/plugins/bb-usage-stats-1.0/screenshot-3.gif" alt="bb: Usage Stats for WordPress: Thumbnail view for the full statistics display." /></p>
<p><strong>Revision History</strong></p>
<p>= 1.0 =</p>
<ul>
<li>Fix: activation hook doesn&#8217;t create track table.</li>
</ul>
<p>= 1.0 =</p>
<ul>
<li>Dashboard widget with summary chart and Top 15 bar-graph chart</li>
<li>Display Top 15 Bar-graph chart</li>
<li>Display summary statistics graph chart</li>
<li>Administrative page</li>
<li>Track home page views</li>
<li>Track category views</li>
<li>Track post views</li>
<li>Track page views</li>
<li>Initial plugin release</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/wordpress/wp-plugins/bbusage-stats/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Plugin bb:Text Rotator Widget</title>
		<link>http://devcorner.georgievi.net/pages/wordpress/wp-plugins/text-rotator-wordpress-widget</link>
		<comments>http://devcorner.georgievi.net/pages/wordpress/wp-plugins/text-rotator-wordpress-widget#comments</comments>
		<pubDate>Sat, 05 Dec 2009 18:35:38 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[WordPress Plugins]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=335</guid>
		<description><![CDATA[WordPress Widget to randomly select and insert text/html snippet. Snippets have priority. Useful for rotating ads (advertisments), images, quotes etc.
]]></description>
			<content:encoded><![CDATA[<p>The bb:Text Rotator WordPress plugin contains bb:Text Rotator Widget.</p>
<p>As any WordPress widget, you can add the bb:Text Rotator Widget to any sidebar using the WordPress administrative interface (Appearance &#8211; Widgets menu). From the Widget administration form you can define text snippets. You can specify as many text snippets as you like.</p>
<p>When WordPress builds the output HTML, the widget randomly selects a snippet and inserts it in place of the widget.</p>
<p>Any widget have a priority. The higher is the priority of the snippet, the higher is the chance that it will be selected for display.</p>
<p>Current Stable Version: [TODO] [Download]</p>
<p>Previous Versions: [TODO]</p>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/wordpress/wp-plugins/text-rotator-wordpress-widget/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Data Expressions or Backus-Naur Form (BNF)</title>
		<link>http://devcorner.georgievi.net/pages/programming/data-expressions-bnf</link>
		<comments>http://devcorner.georgievi.net/pages/programming/data-expressions-bnf#comments</comments>
		<pubDate>Wed, 02 Dec 2009 13:20:59 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=328</guid>
		<description><![CDATA[Data expressions can be used to describe structure of data.
Example: Data Expression with plus as concatenator

booking request = guest data + period + room type

guest data = guest name + address + paymethod
           + [passport number]

passport number = letter + {digit}*8

room state = {free &#124; [...]]]></description>
			<content:encoded><![CDATA[<p>Data expressions can be used to describe structure of data.<span id="more-328"></span></p>
<p><strong>Example: Data Expression with plus as concatenator</strong></p>
<pre>
booking request = guest data + period + room type

guest data = guest name + address + paymethod
           + [passport number]

passport number = letter + {digit}*8

room state = {free | booked | occupied | repair}

account data = transfer + {account record}* + done
</pre>
<p><strong>Example: Data Expression with implicit concatenation</strong></p>
<pre>
guestData = guestName, address, paymethod
         [, passportNumber]

if_statement = 'if' condition 'Then' statement
              ['Else:' statement 'End If']
</pre>
<ul>
<li>+    Shows the sequence of data. What comes after what.</li>
<li>[ ]  Shows that something is optional. <br />
                [optional-expression]</li>
<li>{ }* Shows that something is repeated a number of times.<br />
                {expression}* <br />
                {expression}*8 <br />
                {expression}*(0:8) <br />
                {expression}*(4:*)</li>
<li>{ | | } Shows a choice between several possibilities.</li>
<li>B = C Shows that the composite data structure B is composed as shown by C.</li>
<li>&#8216;free&#8217;   Shows a literal value.</li>
<li>If we want a comma to separate the data parts, we<br />
show a comma in the formula.</li>
</ul>
<p>Software Requirements &#8211; Styles and Techniques</p>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/programming/data-expressions-bnf/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Tutorial: Create Shortcode</title>
		<link>http://devcorner.georgievi.net/pages/wordpress/wp-theme-design/wordpress-shortcode-tutorial</link>
		<comments>http://devcorner.georgievi.net/pages/wordpress/wp-theme-design/wordpress-shortcode-tutorial#comments</comments>
		<pubDate>Thu, 26 Nov 2009 18:32:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[WordPress Development]]></category>
		<category><![CDATA[Wordpress Theme Design Tips]]></category>
		<category><![CDATA[wordpress development shortcode]]></category>
		<category><![CDATA[wordpress tutorial development shortcode]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=318</guid>
		<description><![CDATA[Shortcodes are very effective way to include dynamic content into your posting. This tutorial will guide you through the process of creating your own shortcode. Just in few easy steps.
In just few minutes you will create a shortcode that includes the post publication date. When you include [post-date] in your post, WordPress will replace it [...]]]></description>
			<content:encoded><![CDATA[<p>Shortcodes are very effective way to include dynamic content into your posting. This tutorial will guide you through the process of creating your own shortcode. Just in few easy steps.<span id="more-318"></span></p>
<p>In just few minutes you will create a shortcode that includes the post publication date. When you include [post-date] in your post, WordPress will replace it automatically with the post date.</p>
<p><strong>1. Create a new post</strong></p>
<p>Let&#8217;s start with creating a post to play with. Publish the post. Here is the post I created.</p>
<p><img src="http://1.bp.blogspot.com/_OvKDL-vK2kY/Sw6-oHxILtI/AAAAAAAAAG0/1G3O24X5UKs/s320/sample-post.gif" alt="Simple WordPress post with post-date shortcode inserted." /></p>
<p>For this tutorial I am using the default WordPress theme. At the end of the post I have inserted <strong>[post-date]</strong> shortcode.<br />
Because the shortcode is not implemented yet, WordPress displays it literally without replacing it.</p>
<p><strong>2. Create a new plugin.</strong></p>
<p>Plugins are placed under WordPress plugins directory. It is good practice to place each plugin into a separate directory. </p>
<p>Create a new directory named <code>post-date</code> under WordPress plugins directory (<code>wp-content/plugins</code>).</p>
<p>Using a text editor, create a text file named <code>post-date.php</code>. You can use any file name that you like, but for this tutorial we will use the <code>post-date.php</code>.</p>
<p>Now you have an empty text file: <code>wp-content/plugins/post-date/post-date.php</code>. This file will contain the code for the shortcode handler.</p>
<p><strong>3. Define the plugin</strong></p>
<p>To identify plugins WordPress uses comments that conform to special format. Enter the following plugin definition in the <code>post-date.php</code> file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
Plugin Name: Post-date short code
Plugin URI: http://devcorner.georgievi.net
Description: This plugin defined a 'post-date' shortcode handler.
Author: Ivan Georgiev
Version: 1.5.1
Author URI: http://devcorner.georgievi.net/
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now you have a plugin defined. You are able to see it under Plugins list in WordPress administrator.</p>
<p><strong>4. Add shortcode handler code</strong></p>
<p>We will create a PHP function that will be invoked when WordPress needs to replace a post-date shortcode.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
Plugin Name: Post-date short code
Plugin URI: http://devcorner.georgievi.net
Description: This plugin defined a 'post-date' shortcode handler.
Author: Ivan Georgiev
Version: 1.5.1
Author URI: http://devcorner.georgievi.net/
*/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> post_date_handler<span style="color: #009900;">&#40;</span><span style="color: #000088;">$atts</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$post</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> mysql2date<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'M jS, Y'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
add_shortcode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post-date'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'post_date_handler'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The <code>add_shortcode</code> function instructs WordPress to invoke the <code>post_date_handler</code> function when it needs to replace a <code>post-date</code> shortcode.</p>
<p>The <code>post_date_handler</code> function returns the formatted date for the current post.</p>
<p>Now you have everyting ready. If you try to view the post you created at the beginning of this tutorial, you will see no change. Why?</p>
<p>One small thing left. You need to activate the newly created plugin.</p>
<p><strong>5. Activate the plugin</strong></p>
<p>Go to the Plugins section in WordPress administrative section. In the plugin list find the &#8216;Post-date shortcode&#8217; plugin and click the &#8216;Activate&#8217; link to activate it.</p>
<p>Open the post and voila! You see that the shortcode text is replaced by the post date.</p>
<p><img src="http://4.bp.blogspot.com/_OvKDL-vK2kY/Sw7I0WIOTBI/AAAAAAAAAHE/2NduEmH10cA/s400/sample-post_ready.gif" alt="The simple post with shortcode replaced" /></p>
<p>If you want to include &#8216;[post-date]&#8216; as a text, you need to use double brackets. You enter &#8216;[[post-date]]&#8217; to get &#8216;[post-date]&#8216;.</p>
<p>This tutorial covers only WordPress shortcode basics. For more detailed description see <a href="http://codex.wordpress.org/Shortcode_API">WordPress shortcode API</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/wordpress/wp-theme-design/wordpress-shortcode-tutorial/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
