<?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 &#187; Oracle Tips</title>
	<atom:link href="http://devcorner.georgievi.net/category/database/oracle_database/oracle_tips/feed" rel="self" type="application/rss+xml" />
	<link>http://devcorner.georgievi.net</link>
	<description>Software Developer's Notepad</description>
	<lastBuildDate>Fri, 12 Aug 2011 20:03:23 +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>Select Random Record from Oracle Database</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/select-random-record-from-oracle-database</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/select-random-record-from-oracle-database#comments</comments>
		<pubDate>Wed, 25 Nov 2009 15:54:14 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[Oracle Tips]]></category>
		<category><![CDATA[oracle sql select random record]]></category>
		<category><![CDATA[oracle sql tip select random record]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=298</guid>
		<description><![CDATA[There are lots of ways to select a random record or row from Oracle database table. Here is an example SQL statement.
To select a random record with Oracle:

   SELECT first_name, last_name
     FROM customer
    ORDER BY dbms_random.value;

To select only one random record from Oracle database table:

  [...]]]></description>
			<content:encoded><![CDATA[<p>There are lots of ways to select a random record or row from Oracle database table. Here is an example SQL statement.<span id="more-298"></span></p>
<p>To select a random record with Oracle:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">   <span style="color: #993333; font-weight: bold;">SELECT</span> first_name<span style="color: #66cc66;">,</span> last_name
     <span style="color: #993333; font-weight: bold;">FROM</span> customer
    <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> dbms_random<span style="color: #66cc66;">.</span>value;</pre></div></div>

<p>To select only one random record from Oracle database table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">   <span style="color: #993333; font-weight: bold;">SELECT</span> first_name<span style="color: #66cc66;">,</span> last_name
     <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> first_name<span style="color: #66cc66;">,</span> last_name
             <span style="color: #993333; font-weight: bold;">FROM</span> customer
            <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> dbms_random<span style="color: #66cc66;">.</span>value<span style="color: #66cc66;">&#41;</span>
    <span style="color: #993333; font-weight: bold;">WHERE</span> ROWNUM <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;</pre></div></div>

<p>To limit the number of random records returned by SELECT statement in Oracle to 10:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">   <span style="color: #993333; font-weight: bold;">SELECT</span> first_name<span style="color: #66cc66;">,</span> last_name
     <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> first_name<span style="color: #66cc66;">,</span> last_name
             <span style="color: #993333; font-weight: bold;">FROM</span> customer
            <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> dbms_random<span style="color: #66cc66;">.</span>value<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;">10</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/select-random-record-from-oracle-database/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Find/Show Oracle Table Tablespace</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/oracle_table_tablespace</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/oracle_table_tablespace#comments</comments>
		<pubDate>Mon, 23 Nov 2009 09:33:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Oracle Tips]]></category>
		<category><![CDATA[oracle dba script table tablespace]]></category>
		<category><![CDATA[oracle table tablespace]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=241</guid>
		<description><![CDATA[Problem:
Oracle tables are composed by extents. Extents are allocated from tablespace. You are given a table and you need to find in which tablespace it is stored.
Solution
To find tablespace for tables in current user&#8217;s schema:

SELECT table_name, tablespace_name, STATUS
  FROM user_tables
 WHERE table_name LIKE :p_table_pattern;

To find tablespaces for tables from all schemas that current user [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem:</strong><br />
Oracle tables are composed by extents. Extents are allocated from tablespace. You are given a table and you need to find in which tablespace it is stored.<span id="more-241"></span></p>
<p><strong>Solution</strong><br />
To find tablespace for tables in current user&#8217;s schema:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> table_name<span style="color: #66cc66;">,</span> tablespace_name<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">STATUS</span>
  <span style="color: #993333; font-weight: bold;">FROM</span> user_tables
 <span style="color: #993333; font-weight: bold;">WHERE</span> table_name <span style="color: #993333; font-weight: bold;">LIKE</span> :p_table_pattern;</pre></div></div>

<p>To find tablespaces for tables from all schemas that current user can access:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> table_name<span style="color: #66cc66;">,</span> tablespace_name<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">STATUS</span>
  <span style="color: #993333; font-weight: bold;">FROM</span> all_tables
 <span style="color: #993333; font-weight: bold;">WHERE</span> table_name <span style="color: #993333; font-weight: bold;">LIKE</span> :p_table_pattern;</pre></div></div>

<p>To find tablespaces for all tables from all schemas, you need to be granted SYSDBA system privilege:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> table_name<span style="color: #66cc66;">,</span> tablespace_name<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">STATUS</span>
  <span style="color: #993333; font-weight: bold;">FROM</span> dba_tables
 <span style="color: #993333; font-weight: bold;">WHERE</span> table_name <span style="color: #993333; font-weight: bold;">LIKE</span> :p_table_pattern;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/oracle_table_tablespace/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find Last Update Date/Time for an Oracle PL/SQL Procedure</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/last-update-oracle-plsql</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/last-update-oracle-plsql#comments</comments>
		<pubDate>Tue, 17 Nov 2009 07:14:18 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[Oracle Tips]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[oracle tips]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=219</guid>
		<description><![CDATA[Problem
You need to find when a PL/SQL procedure in an Oracle Database was updated.
Solution
The following SQL query returns information about a FIND_USER procedure, defined in the current user&#8217;s schema. The information returned includes procedure name, date procedure was created and date procedure was updated.

SELECT o.object_name AS object_name,
       o.created AS [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem</strong><br />
You need to find when a PL/SQL procedure in an Oracle Database was updated.<span id="more-219"></span></p>
<p><strong>Solution</strong><br />
The following SQL query returns information about a FIND_USER procedure, defined in the current user&#8217;s schema. The information returned includes procedure name, date procedure was created and date procedure was updated.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> o<span style="color: #66cc66;">.</span>object_name <span style="color: #993333; font-weight: bold;">AS</span> object_name<span style="color: #66cc66;">,</span>
       o<span style="color: #66cc66;">.</span>created <span style="color: #993333; font-weight: bold;">AS</span> date_created<span style="color: #66cc66;">,</span>
       o<span style="color: #66cc66;">.</span>last_ddl_time <span style="color: #993333; font-weight: bold;">AS</span> date_updated
  <span style="color: #993333; font-weight: bold;">FROM</span> user_objects o
 <span style="color: #993333; font-weight: bold;">WHERE</span> o<span style="color: #66cc66;">.</span>object_type <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'PROCEDURE'</span>
       <span style="color: #993333; font-weight: bold;">AND</span> o<span style="color: #66cc66;">.</span>object_name <span style="color: #66cc66;">=</span><span style="color: #ff0000;">'FIND_USER'</span>;</pre></div></div>

<p>The following SQL query retrieves similar information for any PL/SQL procedure in any Oracle Database schema you have granted access to.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> o<span style="color: #66cc66;">.</span>object_name <span style="color: #993333; font-weight: bold;">AS</span> object_name<span style="color: #66cc66;">,</span>
       o<span style="color: #66cc66;">.</span>owner <span style="color: #993333; font-weight: bold;">AS</span> object_schema<span style="color: #66cc66;">,</span>
       o<span style="color: #66cc66;">.</span>created <span style="color: #993333; font-weight: bold;">AS</span> date_created<span style="color: #66cc66;">,</span>
       o<span style="color: #66cc66;">.</span>last_ddl_time <span style="color: #993333; font-weight: bold;">AS</span> date_updated
  <span style="color: #993333; font-weight: bold;">FROM</span> all_objects o
 <span style="color: #993333; font-weight: bold;">WHERE</span> o<span style="color: #66cc66;">.</span>object_type <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'PROCEDURE'</span>
       <span style="color: #993333; font-weight: bold;">AND</span> o<span style="color: #66cc66;">.</span>object_name <span style="color: #66cc66;">=</span><span style="color: #ff0000;">'FIND_USER'</span>;</pre></div></div>

<p>Database Administrator can query information about procedures, defined in any schema.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> o<span style="color: #66cc66;">.</span>object_name <span style="color: #993333; font-weight: bold;">AS</span> object_name<span style="color: #66cc66;">,</span>
       o<span style="color: #66cc66;">.</span>owner <span style="color: #993333; font-weight: bold;">AS</span> object_schema<span style="color: #66cc66;">,</span>
       o<span style="color: #66cc66;">.</span>created <span style="color: #993333; font-weight: bold;">AS</span> date_created<span style="color: #66cc66;">,</span>
       o<span style="color: #66cc66;">.</span>last_ddl_time <span style="color: #993333; font-weight: bold;">AS</span> date_updated
  <span style="color: #993333; font-weight: bold;">FROM</span> dba_objects o
 <span style="color: #993333; font-weight: bold;">WHERE</span> o<span style="color: #66cc66;">.</span>object_type <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'PROCEDURE'</span>
       <span style="color: #993333; font-weight: bold;">AND</span> o<span style="color: #66cc66;">.</span>object_name <span style="color: #66cc66;">=</span><span style="color: #ff0000;">'FIND_USER'</span>;</pre></div></div>

<p><strong>See Also</strong> <a href="../oracle_tips/find-invalid-objects-in-oracle-database" title="SQL query that retrieves information about all invalid data dictionary objects in an Oracle Database">Find Invalid Objects in Oracle Databasea</a><br />
<strong>Back to</strong> <a href="../../../../category/database/oracle_database/oracle_tips">Oracle Database Tips</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/last-update-oracle-plsql/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find Invalid Objects in Oracle Database</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/find-invalid-objects-in-oracle-database</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/find-invalid-objects-in-oracle-database#comments</comments>
		<pubDate>Tue, 17 Nov 2009 07:13:55 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[Oracle Tips]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=224</guid>
		<description><![CDATA[Problem
You need to find information about all invalid database objects in an Oracle Database.
Solution

SELECT o.object_name AS object_name,
       o.object_type AS object_type,
       o.owner AS object_schema
  FROM all_objects o
 WHERE o.STATUS = 'INVALID';

Database Administrators (DBA) can query DBA_OBJECTS data dictionary view.

SELECT o.object_name AS object_name,
  [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem</strong><br />
You need to find information about all invalid database objects in an Oracle Database.<span id="more-224"></span></p>
<p><strong>Solution</strong></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> o<span style="color: #66cc66;">.</span>object_name <span style="color: #993333; font-weight: bold;">AS</span> object_name<span style="color: #66cc66;">,</span>
       o<span style="color: #66cc66;">.</span>object_type <span style="color: #993333; font-weight: bold;">AS</span> object_type<span style="color: #66cc66;">,</span>
       o<span style="color: #66cc66;">.</span>owner <span style="color: #993333; font-weight: bold;">AS</span> object_schema
  <span style="color: #993333; font-weight: bold;">FROM</span> all_objects o
 <span style="color: #993333; font-weight: bold;">WHERE</span> o<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">STATUS</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'INVALID'</span>;</pre></div></div>

<p>Database Administrators (DBA) can query DBA_OBJECTS data dictionary view.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> o<span style="color: #66cc66;">.</span>object_name <span style="color: #993333; font-weight: bold;">AS</span> object_name<span style="color: #66cc66;">,</span>
       o<span style="color: #66cc66;">.</span>object_type <span style="color: #993333; font-weight: bold;">AS</span> object_type<span style="color: #66cc66;">,</span>
       o<span style="color: #66cc66;">.</span>owner <span style="color: #993333; font-weight: bold;">AS</span> object_schema
  <span style="color: #993333; font-weight: bold;">FROM</span> dba_objects o
 <span style="color: #993333; font-weight: bold;">WHERE</span> o<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">STATUS</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'INVALID'</span>;</pre></div></div>

<p><strong>See Also</strong> <a href="../oracle_tips/last-update-oracle-plsql" title="SQL query that retrieves information about all invalid data dictionary objects in an Oracle Database">Find Invalid Objects in Oracle Databasea</a><br />
<strong>Back to</strong> <a href="../../../../category/database/oracle_database/oracle_tips">Oracle Database Tips</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/find-invalid-objects-in-oracle-database/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implicit Cursor &#8211; Get the Number of Rows Affected by SQL Query in Oracle PL/SQL</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/rows-affected-sql-oracle</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/rows-affected-sql-oracle#comments</comments>
		<pubDate>Fri, 06 Nov 2009 21:14:06 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[Oracle Tips]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[oracle pl sql]]></category>
		<category><![CDATA[oracle tips]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=216</guid>
		<description><![CDATA[You can use the SQL implicit cursor to retrieve the number of rows, affected by SQL query: SQL%ROWCOUNT.

BEGIN
  UPDATE employee
     SET salary = salary * 1.1
   WHERE salary_range = 'middle';
  dbms_output.put_line&#40;'Number of affrected rows: ' &#124;&#124; SQL%ROWCOUNT&#41;;
END;

]]></description>
			<content:encoded><![CDATA[<p>You can use the SQL implicit cursor to retrieve the number of rows, affected by SQL query: SQL%ROWCOUNT.<span id="more-216"></span></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">BEGIN
  <span style="color: #993333; font-weight: bold;">UPDATE</span> employee
     <span style="color: #993333; font-weight: bold;">SET</span> salary <span style="color: #66cc66;">=</span> salary <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">1.1</span>
   <span style="color: #993333; font-weight: bold;">WHERE</span> salary_range <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'middle'</span>;
  dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Number of affrected rows: '</span> <span style="color: #66cc66;">||</span> SQL%ROWCOUNT<span style="color: #66cc66;">&#41;</span>;
END;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/rows-affected-sql-oracle/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Declare Variables, Constants and Types in Oracle PL/SQL</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/declare-plsql-variables-constant</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/declare-plsql-variables-constant#comments</comments>
		<pubDate>Fri, 06 Nov 2009 21:01:04 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[Oracle Tips]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[oracle pl sql]]></category>
		<category><![CDATA[oracle tips]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=206</guid>
		<description><![CDATA[You declare PL/SQL variables, constants and types in declare block. The syntax is

     &#60;name&#62; [CONSTANT] &#60;datatype&#62; [NOT NULL] [:= &#124; DEFAULT &#60;expr&#62;]


&#60;name&#62; is the name of the variable or constant;
&#60;datatype&#62; may be scalar, composite datatype, reference or LOB;
&#60;expr&#62; is a literal value, another variable or any PL/SQL expression. The result of [...]]]></description>
			<content:encoded><![CDATA[<p>You declare PL/SQL variables, constants and types in declare block. The syntax is<span id="more-206"></span></p>
<pre>
     &lt;name&gt; [CONSTANT] &lt;datatype&gt; [NOT NULL] [:= | DEFAULT &lt;expr&gt;]
</pre>
<ul>
<li>&lt;name&gt; is the name of the variable or constant;</li>
<li>&lt;datatype&gt; may be scalar, composite datatype, reference or LOB;</li>
<li>&lt;expr&gt; is a literal value, another variable or any PL/SQL expression. The result of the expression is used to assign the default value of the variable or constant.</li>
</ul>
<ul>
<li>If you are defining a constant, you must provide the value of the constant in the definition.</li>
<li>TABLE, RECORD, NESTED TABLE and VARRAY are composite types.</li>
<li>To define scalar type you can refer to [schema.]object%TYPE or cursor%TYPE.</li>
<li>For record type you can refer to [schema.]object%ROWTYPE.</li>
</ul>
<p><strong>Examples:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">DECLARE
  <span style="color: #808080; font-style: italic;">--Define some scalar variables</span>
  v_year NUMBER<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
  v_pin VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>;
  v_min_amount NUMBER<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">25000</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">--Define a record type</span>
  TYPE t_customer <span style="color: #993333; font-weight: bold;">IS</span> RECORD <span style="color: #66cc66;">&#40;</span>
    id NUMBER<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">18</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
    first_name VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
    last_name VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #66cc66;">&#41;</span>;
  <span style="color: #808080; font-style: italic;">--Declare a record variable</span>
  v_customer t_customer;
&nbsp;
  <span style="color: #808080; font-style: italic;">--Declare a composite type to hold a list of records</span>
  TYPE t_customer_list <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">TABLE</span> OF t_customer;
&nbsp;
  <span style="color: #808080; font-style: italic;">--Declare a record variable refering an existing table</span>
  v_employee employee%ROWTYPE;
BEGIN
   v_year :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2010</span>;
   <span style="color: #808080; font-style: italic;">-- More PL/SQL code here.</span>
END;</pre></div></div>

<p><strong>Declare RECORD variables and types</strong></p>
<p>A specific RECORD TYPE corresponding to a fixed number (and datatype) of underlying table columns can simplify the job of defining variables.</p>
<p>%TYPE is used to declare a field with the same type as that of a specified table&#8217;s column.<br />
%ROWTYPE is used to declare a record with the same types as found in the specified database table, view or cursor.</p>
<p><strong>Declare RECORD type</strong></p>
<pre>
TYPE &lt;type_name&gt; IS RECORD(
    &lt;field_declaration&gt;,...);
</pre>
<p>Each record field is declared with its &#8216;field_declaration&#8217;:</p>
<pre>
   &lt;field_name> {&lt;field_type&gt; |
               &lt;variable&gt;%TYPE |
               &lt;table.column&gt;%TYPE |
               &lt;table&gt;%ROWTYPE}
               [ [NOT NULL] {:= | DEFAULT} &lt;expr&gt; ]
</pre>
<p>The &lt;field_type&gt; can be any PL/SQL data type except REF CURSOR.</p>
<p>To declare a record variable:</p>
<pre>
   &lt;variable_identifier&gt; &lt;type_name&gt;;
</pre>
<p>You can declare a variable based on a record reference using %ROWTYPE:</p>
<pre>
   &lt;variable_name&gt; &lt;table_or_curosr_name&gt;%ROWTYPE
</pre>
<p>The number of variables and their data type is evaluated at run-time.</p>
<p><strong>Back to</strong> <a href="../../../../category/database/oracle_database/oracle_tips">Oracle Database Tips</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/declare-plsql-variables-constant/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Function Based Virtual Columns in Oracle 11 g Database</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/virtual-columns-oracle</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/virtual-columns-oracle#comments</comments>
		<pubDate>Fri, 06 Nov 2009 17:32:02 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[Oracle Tips]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[oracle database]]></category>
		<category><![CDATA[oracle tips]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=204</guid>
		<description><![CDATA[Oracle Database 11g introduced new feature &#8211; virtual column. Instead storing data, the virtual column is calculated based on an expression stored in data dictionary. Let&#8217;s assume you have a table that stores customer information. The customer names are stored in two separate columns &#8211; first_name and last_name. The database queries often need to return [...]]]></description>
			<content:encoded><![CDATA[<p>Oracle Database 11g introduced new feature &#8211; virtual column. Instead storing data, the virtual column is calculated based on an expression stored in data dictionary. Let&#8217;s assume you have a table that stores customer information. The customer names are stored in two separate columns &#8211; first_name and last_name. The database queries often need to return the customer&#8217;s full name. Instead of storing the full name into the database or having to type it each time, you can define a new virtual column that is calculated, based on an expression: <code>first_name || ' ' || last_name</code>.<span id="more-204"></span></p>
<p><strong>Create a Oracle Database Table with calculated virtual column:</strong></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> customer<span style="color: #66cc66;">&#40;</span>
   first_name VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
   last_name VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
   full_name <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span>first_name <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' '</span> <span style="color: #66cc66;">||</span> last_name<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p><strong>Add a new calculated virtual column to an existing Oracle Database Table:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> customer
          <span style="color: #993333; font-weight: bold;">ADD</span> full_name <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span>first_name <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' '</span> <span style="color: #66cc66;">||</span> last_name<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Some of the virtual column benefits include:</p>
<ul>
<li>Automatic re-computation of derived columns for ad-hoc query tools.</li>
<li>Reduction in redundant disk space for columns that must be derived from other columns (e.g. a MONTH column that is derived from another DATE column).</li>
<li>Keep business logic in a single place.</li>
<li>Easier interval partitioning based on calculated columns.</li>
</ul>
<p>You should keep in mind that:</p>
<ul>
<li>virtual columns may not reference other virtual columns;</li>
<li>virtual columns only within the containing table. You cannot reference columns within other tables.</li>
</ul>
<p>Oracle Database virtual columns has the nice side effect of assisting in streamlining partitioning.  For example, assume that we have a table that is partitioned by year-month (i.e. 2007-07).  With 11g virtual columns, instead of creating separate column, we can simply compute the partition key virtually, using a DATE column.</p>
<p><strong>Back to</strong> <a href="../../../../category/database/oracle_database/oracle_tips">Oracle Database Tips</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/virtual-columns-oracle/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use TAB and Other Special Characters in Oracle SQL</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/tab-special-characters-oracle-sql</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/tab-special-characters-oracle-sql#comments</comments>
		<pubDate>Tue, 03 Nov 2009 22:24:22 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[Oracle Tips]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[oracle tips]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=202</guid>
		<description><![CDATA[You can add special characters to an Oracle SQL query by using the CHR function. For example, to add a Tab character, you can use CHR(9). Where 9 is the ASCII code for the Tab character.

SELECT first_name &#124;&#124; CHR&#40;9&#41; &#124;&#124; last_name
  FROM employee;

You can use this approach to add other special characters to Oracle [...]]]></description>
			<content:encoded><![CDATA[<p>You can add special characters to an Oracle SQL query by using the CHR function. For example, to add a Tab character, you can use CHR(9). Where 9 is the ASCII code for the Tab character.<span id="more-202"></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> first_name <span style="color: #66cc66;">||</span> CHR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> last_name
  <span style="color: #993333; font-weight: bold;">FROM</span> employee;</pre></div></div>

<p>You can use this approach to add other special characters to Oracle query.</p>
<p><strong>Back to</strong> <a href="../../../../category/database/oracle_database/oracle_tips">Oracle Database Tips</a><br />
.</p>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/tab-special-characters-oracle-sql/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic SQL Using OPEN FOR in Oracle PL/SQL</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/dynamic-curosr-oracle-plsql</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/dynamic-curosr-oracle-plsql#comments</comments>
		<pubDate>Tue, 03 Nov 2009 22:15:51 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[Oracle Tips]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[oracle pl sql]]></category>
		<category><![CDATA[oracle tips]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=198</guid>
		<description><![CDATA[This example illustrates how you can create and use dynamic cursor in Oracle PL/SQL. The example is pretty simple, but I hope you can get the idea and apply it to more complicated cases.

DECLARE
  t_cursor IS REF CURSOR;
  my_cursor t_cursor;
  v_customer RECORD &#40;
     customer_id NUMBER&#40;18&#41;,
	 amount NUMBER&#40;22, 2&#41;
 [...]]]></description>
			<content:encoded><![CDATA[<p>This example illustrates how you can create and use dynamic cursor in Oracle PL/SQL. The example is pretty simple, but I hope you can get the idea and apply it to more complicated cases.<span id="more-198"></span></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">DECLARE
  t_cursor <span style="color: #993333; font-weight: bold;">IS</span> REF CURSOR;
  my_cursor t_cursor;
  v_customer RECORD <span style="color: #66cc66;">&#40;</span>
     customer_id NUMBER<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">18</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	 amount NUMBER<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">22</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>;
BEGIN
  OPEN my_cursor <span style="color: #993333; font-weight: bold;">FOR</span> <span style="color: #993333; font-weight: bold;">SELECT</span> customer_id<span style="color: #66cc66;">,</span> 
                            amount 
                       <span style="color: #993333; font-weight: bold;">FROM</span> monthly_sales;
  LOOP
    FETCH my_cursor <span style="color: #993333; font-weight: bold;">INTO</span> v_customer;
	EXIT WHEN my_cursor%NOTFOUND;
&nbsp;
	dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>v_customer<span style="color: #66cc66;">.</span>amount<span style="color: #66cc66;">&#41;</span>;
  END LOOP;
END;</pre></div></div>

<p>Back To: <a href="../../../../category/database/oracle_database/oracle_tips">Oracle Tips</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/dynamic-curosr-oracle-plsql/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Remove Duplicate Rows from Oracle Database Table</title>
		<link>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/remove-duplicate-rows-oracle</link>
		<comments>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/remove-duplicate-rows-oracle#comments</comments>
		<pubDate>Fri, 23 Oct 2009 09:09:02 +0000</pubDate>
		<dc:creator>baobab</dc:creator>
				<category><![CDATA[Oracle Tips]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[oracle database]]></category>
		<category><![CDATA[oracle tips]]></category>

		<guid isPermaLink="false">http://devcorner.georgievi.net/?p=176</guid>
		<description><![CDATA[If you need to remove duplicate rows from an Oracle Database Table, you can use different approaches. For example, you can use the DELETE command with a criteria that selects only duplicates. Usually it is faster to create a new table with distinct rows and replace the old table with the new one.

-- Create a [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to remove duplicate rows from an Oracle Database Table, you can use different approaches. For example, you can use the DELETE command with a criteria that selects only duplicates. Usually it is faster to create a new table with distinct rows and replace the old table with the new one.<span id="more-176"></span></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Create a new table with distinct row values.</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> temp_table_name <span style="color: #993333; font-weight: bold;">AS</span>
   <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">DISTINCT</span> <span style="color: #66cc66;">*</span> 
      <span style="color: #993333; font-weight: bold;">FROM</span> source_table_name;
&nbsp;
<span style="color: #808080; font-style: italic;">-- Replace the original table with the new table.</span>
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> source_table_name;
<span style="color: #993333; font-weight: bold;">RENAME</span> temp_table_name <span style="color: #993333; font-weight: bold;">TO</span> source_table_name;</pre></div></div>

<p>Of course in real life the are always complications. For example, the above example doesn&#8217;t take in account constraints and indexes. You should carefully investigate all dependencies to avoid performance degradation and to preserve data integrity.</p>
]]></content:encoded>
			<wfw:commentRss>http://devcorner.georgievi.net/pages/database/oracle_database/oracle_tips/remove-duplicate-rows-oracle/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

