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

<channel>
	<title>Oracle Tips and Techniques &#187; PL/SQL Tutorial</title>
	<atom:link href="http://www.oraclecity.com/category/plsql-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.oraclecity.com</link>
	<description>Oracle 11g, Oracle 10g, PL/SQL, Oracle Enterprise Manager 11g Grid Control, Oracle Performance Tuning, Microsoft SQL Server and T-SQL</description>
	<lastBuildDate>Thu, 12 Jan 2012 11:36:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Oracle Synonym example</title>
		<link>http://www.oraclecity.com/plsql-tutorial/oracle-synonym-example/</link>
		<comments>http://www.oraclecity.com/plsql-tutorial/oracle-synonym-example/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 12:24:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PL/SQL Tutorial]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[pl/sql]]></category>
		<category><![CDATA[SQL Tutorial]]></category>

		<guid isPermaLink="false">http://www.oraclecity.com/?p=339</guid>
		<description><![CDATA[Oracle synonym example will show you why and how to use Oracle synonyms]]></description>
			<content:encoded><![CDATA[<p>Oracle synonyms are short names (and easy to remember) for other Oracle object names. The other oracle objects can be tables, views, procedures, functions, packages and other Oracle objects and may be in the same schema or in another schema or in another database.</p>
<p>Synonyms can be PRIVATE or PUBLIC. If you create a private synonym then only you can use that synonym. The default is PRIVATE.</p>
<p>PUBLIC synonyms can be used by all database users.<span id="more-339"></span></p>
<p>See below some of the usage of Synonyms:</p>
<p>1.</p>
<h1>Create public Synonym to give short names to a table</h1>
<p>Create synonym tccg for schema1.t_country_city_group;</p>
<p>This statement will create a short name tccg for the table schema1.t_country_city_group. So you can now query the table using the statement:</p>
<p>Select * from tccg;</p>
<p>2.</p>
<h1>Create a synonym where schema name is long</h1>
<p>Create public synonym tccg for schema1isalongschema.t_country_city_group;</p>
<p>If you have to specify the name schema1isalongschema again and again, I am sure you will start pulling your hair. So if you create the synonym “tccg” then you can simply refer that table as</p>
<p>Select * from tccg;</p>
<p>3.</p>
<h1>Create a synonym for a table in another database.</h1>
<p>Create public synonym tccg for <a href="mailto:schema1.t_country_city_group@remote_db1.world">schema1.t_country_city_group@remote_db1.world</a>;</p>
<p>The above statement is creating a synonym called “tccg” for an object which is in another database – referenced by database link remote_db1.world.</p>
<p>4.</p>
<h1>Create synonym for a procedure with long name.</h1>
<p>Create public synonym dispdata for schema1.p_proceduretodisplaydata;</p>
<p>Here you are giving short name dispdata for stored procedure p_proceduretodisplaydata that reside in schema1 schema.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oraclecity.com/plsql-tutorial/oracle-synonym-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle Ref Cursor And Cursor Variable</title>
		<link>http://www.oraclecity.com/plsql-tutorial/oracle-ref-cursor-and-cursor-variable/</link>
		<comments>http://www.oraclecity.com/plsql-tutorial/oracle-ref-cursor-and-cursor-variable/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 13:23:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PL/SQL Tutorial]]></category>
		<category><![CDATA[cursor variable]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[pl/sql]]></category>
		<category><![CDATA[ref cursor]]></category>

		<guid isPermaLink="false">http://www.oraclecity.com/?p=336</guid>
		<description><![CDATA[Example of usage of Oracle PL/SQL ref cursor and cursor variable]]></description>
			<content:encoded><![CDATA[<p>HTML clipboardMany of you may have heard of the terms REF CURSOR and cursor variable. This article here  will explain you clearly – what is a ref cursor and cursor variable. Then it will  demonstrate you how to use a ref cursor and cursor variables with example.</p>
<p><span id="more-3358"> </span></p>
<p>A REF CURSOR is a cursor which does not have a fixed SQL query associated with it.  Basically you declare a TYPE of REF CURSOR type. Then you define a variable of that TYPE.  That variable is called and cursor variable.</p>
<p>Then you assign a query to that cursor variable. After that you can use that cursor  variable like any other cursor.</p>
<p>CURSOR VARIABLE is a variable whose data type is a TYPE of REF CURSOR.</p>
<p>On the other hand, in a normal cursor you write a select query and you give a name to  that select query. You can open, fetch data and close that cursor whenever you want.</p>
<p>For example, you declare a normal cursor as:</p>
<p><strong><big><em><span><span>cursor emp_cur is<br />
select * from emp where deptno =10;<span id="more-336"></span></span></span></em></big></strong></p>
<p>In this example you are giving a name “emp_cur” to the SQL query. The sql query is  always static. It will always return you all the employees who are in department id 10.  Now you can use open-fetch-close on this cursor as many times as you want in your pl/sql  program.</p>
<p>Coming back to REF CURSOR, you can define a REF CURSOR TYPE as</p>
<p><em><strong><span><span>TYPE RefCurTyp IS REF CURSOR;</span></span></strong></em></p>
<p>Here RefCurTyp is a user defined datatype of REF CURSOR. Now any variable that you  declare of datatype RefCurTyp will be a cursor variable.</p>
<p>For example here ref_cv is a cursor variable:</p>
<p><strong><em><big><span><span>ref_cv RefCurTyp;</span></span></big></em></strong></p>
<p>In the example below the package has two stored procedures. One of the procedure will  accept a number as input parameter. Based on that number the other procedure will OPEN a  REF cursor and return data to the calling procedure. Then the calling procedure can  process the ref cursor as any other normal static query cursor. Using two separate stored  procedure makes it clearer to understand and easier for future modifications.</p>
<p><em><strong><span><span>CREATE OR REPLACE PACKAGE pkg_ref_cursor_test IS<br />
TYPE RefCurTyp IS REF CURSOR;<br />
PROCEDURE p_process_data(in_indi number);<br />
PROCEDURE p_open_refcur (ref_cv IN OUT RefCurTyp, in_indi number);<br />
END pkg_ref_cursor_test;<br />
/</span></span></strong></em></p>
<p><em><strong>CREATE OR REPLACE PACKAGE BODY pkg_ref_cursor_test IS</strong></em></p>
<p><em><strong>PROCEDURE p_process_data(in_indi number) IS<br />
ref_cv RefCurTyp;<br />
l_count number;<br />
BEGIN<br />
p_open_refcur(ref_cv, in_indi);<br />
LOOP<br />
FETCH ref_cv INTO l_count;<br />
EXIT WHEN ref_cv%NOTFOUND;<br />
IF in_indi = 10 THEN<br />
dbms_output.put_line(‘Total emp=’||l_count);<br />
ELSIF in_indi =20 THEN<br />
dbms_output.put_line(‘Total dept=’||l_count);<br />
END IF;<br />
END LOOP;<br />
CLOSE ref_cv;<br />
EXCEPTION<br />
WHEN OTHERS THEN<br />
dbms_output.put_line(‘Error = ‘||SQLERRM);<br />
END;</strong></em></p>
<p><strong><em>PROCEDURE p_open_refcur (ref_cv IN OUT RefCurTyp, in_indi number) IS<br />
BEGIN<br />
IF in_indi = 10 THEN<br />
open ref_cv FOR SELECT COUNT(*) FROM emp;<br />
ELSIF in_indi =20 THEN<br />
open ref_cv FOR SELECT COUNT(*) FROM dept;<br />
END IF;<br />
END;<br />
END pkg_ref_cursor_test;<br />
/</em></strong></p>
<p>As you can see from the above example, the cursor variable ref_cv is associated either  with emp table or with dept table. So the SQL query for this cursor is not static – not  declared when the cursor<br />
is declared. The cursor returns either number of employee or number of departments based  on the input parameter</p>
<p>Also note the exception section in p_process_data stored procedure. When a cursor can  not be opened (a number other than 10 or 20 is passed) then the program will encounter  this error. So handling exception here should trap that as this stored program will try to  fetch from the cursor even the cursor was not opened at all.</p>
<p>I hope this article and example has given you a good understanding of Oracle REF  CURSOR.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oraclecity.com/plsql-tutorial/oracle-ref-cursor-and-cursor-variable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle create or replace Stored Procedure example basics</title>
		<link>http://www.oraclecity.com/plsql-tutorial/oracle-create-or-replace-stored-procedure-example-basics/</link>
		<comments>http://www.oraclecity.com/plsql-tutorial/oracle-create-or-replace-stored-procedure-example-basics/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 17:19:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PL/SQL Tutorial]]></category>
		<category><![CDATA[create or replace procedure]]></category>
		<category><![CDATA[SQL Tutorial]]></category>

		<guid isPermaLink="false">http://www.oraclecity.com/?p=190</guid>
		<description><![CDATA[A very simple and easy to understand example of Stored procedure. An Oracle stored procedure is an Oracle object that you create using PL/SQL. PL/SQL is a procedural language which is used by Oracle developers to process and manipulate data stored in an Oracle database.]]></description>
			<content:encoded><![CDATA[<p>An Oracle stored procedure is an Oracle object that you create using PL/SQL. PL/SQL is a procedural language which is used by Oracle developers to process and manipulate data stored in an Oracle database.</p>
<p>The basic form of an Oracle stored procedure is</p>
<p><em><strong>CREATE OR REPLACE PROCEDURE p_procedure1 IS<br />
BEGIN<br />
</strong></em></p>
<p style="padding-left: 30px;"><em><strong> null;</strong></em></p>
<p><em><strong>END p_procedure1;</strong></em></p>
<p><em><strong><span id="more-190"></span><br />
</strong></em></p>
<p>The stored procedure above will compile successfully.</p>
<p>Now lets discuss the procedure above.</p>
<p><strong>CREATE OR REPLACE</strong>: You must use CREATE command whereas REPLACE is optional. But if you do not put REPLACE then you can not recompile the stored procedure if it already exists. By putting CREATE OR REPLACE you are saying that if it already exists then replace it with the new version.</p>
<p><strong>PROCEDURE</strong>: This clause specifies the type of object that you want to create. Here we are saying it to be a procedure.</p>
<p><strong>BEGIN and END</strong>: This is the body of the procedure. All code that you want to put in the procedure should live here. In the example above I have put null; It means do nothing. However even if you do nothing and just want to create a stored procedure (as above) you still need to put the Begin and End to complete the frame of the stored procedure. For clarity I have put End p_procedure1 which is not required. Instead you could just put End;</p>
<p><strong>Example of Oracle Store Procedure with parameters:</strong></p>
<p>Most of the time when calling an Oracle procedure you would like to pass on some parameter to the procedure to process it or to do something with the data in the parameter. You can pass parameters to a stored procedure as below:</p>
<p><em><strong>CREATE OR REPLACE PROCEDURE p_procedure2(p_name varchar2) IS<br />
BEGIN<br />
</strong></em></p>
<p style="padding-left: 30px;"><em><strong> dbms_output.put_line(p_name);</strong></em></p>
<p><em><strong>END p_procedure2;</strong></em></p>
<p>Here I am passing a parameter called p_name to the stored procedure p_procedure1; Then in the body section I am displaying the value of the parameter.</p>
<p>You can use “dbms_output.put_line()” to display the values of parameters in SQL*Plus window. This is same PRINT/ECHO/PRINTLN used in other programming languages.</p>
<p>Now the parameters that you pass to a stored procedure can be of three types (IN, OUT and IN OUT).</p>
<p><strong>IN </strong>- is the default type. So if you do not specify the parameter type then IN is used. This is telling that I am supplying a parameter to the stored procedure.</p>
<p><strong>OUT</strong> – is used to get values back from the stored procedure.</p>
<p><strong>IN OUT</strong> – is used to pass values to a stored procedure and to get values from the stored procedure using the same parameter.</p>
<p>Unlike Oracle functions you can return multiple values using the out parameter.</p>
<p>For example: if you want to get back the employee name of an employee and you have just the emp_no (employee number) then you can pass the emp_no to a stored procedure using an IN parameter and then get the employee name using an out parameter. This is demonstrated below:</p>
<p><em><strong>CREATE OR REPLACE PROCEDURE p_procedure3(p_emp_no IN number, p_name OUT varchar2) IS<br />
BEGIN<br />
</strong></em></p>
<p style="padding-left: 30px;"><em><strong>SELECT ename<br />
INTO p_name<br />
FROM emp<br />
WHERE emp_no = p_emp_no;<br />
</strong></em></p>
<p><em><strong>END p_procedure3;</strong></em></p>
<p><em><strong><br />
</strong></em></p>
<p>Here we are passing the emp_no and using that we are getting ename from table emp. The returned value is put into p_name and the calling program will be able to access this p_name.</p>
<p>Now when we use out parameter it becomes a little bit different while executing a stored procedure.</p>
<p>For example:  to execute the first two stored procedures mentioned above you simple login to Oracle using SQL*Plus and run the commands:</p>
<p><em><strong>SQL&gt;exec p_procedure1;</strong></em></p>
<p>Or</p>
<p><em><strong>SQL&gt;set serverout on;</strong></em></p>
<p><em><strong>SQL&gt; exec p_procedure2(‘ITSME’);</strong></em></p>
<p>But when if use an OUT parameter (as in p_procedure3)  you can not simply run execute command like “exec p_procedure3(parameters here). Because the stored procedure is returning a value and you need to read that value.</p>
<p>In such case you can write a very simple PL/SQL block as below</p>
<p><em><strong>DECLARE<br />
l_name varchar2(20);<br />
BEGIN</strong></em></p>
<p style="padding-left: 30px;"><em><strong> &#8212; now call the stored procedure</strong></em></p>
<p style="padding-left: 30px;"><em><strong> p_procedure3(100, l_name);</strong></em></p>
<p style="padding-left: 30px;"><em><strong> &#8212; now display the returned value </strong></em></p>
<p style="padding-left: 30px;"><em><strong> dbms_output.put_line(l_name);</strong></em></p>
<p><em><strong>END;</strong></em></p>
<p>In the code above I have created a variable called l_name. While calling p_procedure3 I am passing the name of this variable. The procedure will find the name of the employee with emo_no=100 and return the name of the employee in my variable l_name. I can then display or do whatever I want with the value in the variable.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oraclecity.com/plsql-tutorial/oracle-create-or-replace-stored-procedure-example-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

