<?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>Techno Oracle &#187; Cursor in PL/SQL</title>
	<atom:link href="http://www.technooracle.com/oracle-tutorials/category/oracle-plsql/cursor-in-plsql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.technooracle.com</link>
	<description>Oracle information centre</description>
	<lastBuildDate>Tue, 17 Apr 2012 12:07:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Cursors with Parameters</title>
		<link>http://www.technooracle.com/oracle-tutorials/cursors-with-parameters/</link>
		<comments>http://www.technooracle.com/oracle-tutorials/cursors-with-parameters/#comments</comments>
		<pubDate>Sat, 26 Jun 2010 13:44:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Cursor in PL/SQL]]></category>

		<guid isPermaLink="false">http://www.technooracle.com/?p=100</guid>
		<description><![CDATA[Commercial applications require that the query, which, defines the cursor, be generic and the data that is retrieved from the table be allowed to change according to need. Oracle recognizes this and permits the creation of a parameterized cursor prior opening. Parameters allow values to be passed to a cursor when it is opened, and [...]]]></description>
			<content:encoded><![CDATA[<p>Commercial applications require that the query, which, defines the cursor, be generic and the data that is retrieved from the table be allowed to change according to need. Oracle recognizes this and permits the creation of a parameterized cursor prior opening. Parameters allow values to be passed to a cursor when it is opened, and used within a query when it executes. Using parameters explicit cursor may be opened more than once in a block, returning a different working set each time.The parameters can be either a constant or a variable.</p>
<p>Syntax to declare parameterized cursor:</p>
<p>CURSOR cursor_name (variable_name datatype) IS &lt;SELECT statement…&gt;</p>
<p>Syntax to open a Parameterized cursor and passing values to the<br />
cursor:<br />
OPEN cursor_name (value/variable/expression);</p>
<p><span id="more-100"></span><br />
The scope of cursor parameters is local to that cursor, which<br />
means that they can be referenced only within the query declared in the<br />
cursor declaration. Each parameter in the declaration must have a<br />
corresponding value in the open statement.</p>
<p><strong>Example:</strong> Consider a PL/SQL code to calculate the total salary of<br />
first n records of emp table. The value of n is passed to cursor as<br />
parameter.<br />
<strong>Solution</strong>:</p>
<p>DECLARE<br />
E NUMBER(5);<br />
S NUMBER(5):=0;<br />
CURSOR C1(N NUMBER) IS SELECT SAL FROM EMP WHERE ROWNUM&lt;=N;<br />
BEGIN<br />
OPEN C1(&amp;N);<br />
LOOP<br />
FETCH C1 INTO E;<br />
EXIT WHEN C1%NOTFOUND;<br />
S:=S+E;<br />
END LOOP;<br />
DBMS_OUTPUT.PUT_LINE(S);<br />
CLOSE C1;<br />
END;<br />
<strong>With Cursor FOR Loop</strong><br />
DECLARE<br />
S NUMBER(5):=0;<br />
CURSOR C1(N NUMBER) IS SELECT SAL FROM EMP WHERE<br />
ROWNUM&lt;=N;<br />
BEGIN<br />
FOR REC IN C1(&amp;N) LOOP<br />
S:=S+REC.SAL;<br />
END LOOP;<br />
DBMS_OUTPUT.PUT_LINE(S);<br />
END;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.technooracle.com/oracle-tutorials/cursors-with-parameters/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using For Loop in Cursor</title>
		<link>http://www.technooracle.com/oracle-tutorials/using-for-loop-in-cursor/</link>
		<comments>http://www.technooracle.com/oracle-tutorials/using-for-loop-in-cursor/#comments</comments>
		<pubDate>Sat, 26 Jun 2010 13:23:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Cursor in PL/SQL]]></category>

		<guid isPermaLink="false">http://www.technooracle.com/?p=98</guid>
		<description><![CDATA[The Cursor FOR Loop implicitly declares its loop index as a record of type %ROWTYPE, opens a cursor, repeatedly fetches rows of the values from the active set into fields in the record, then closes the cursor when all rows have been processed or when the EXIT command is encountered. The syntax for the FOR [...]]]></description>
			<content:encoded><![CDATA[<p>The Cursor FOR Loop implicitly declares its loop index as a record of<br />
type %ROWTYPE, opens a cursor, repeatedly fetches rows of the values<br />
from the active set into fields in the record, then closes the cursor when<br />
all rows have been processed or when the EXIT command is<br />
encountered.<br />
The syntax for the FOR Loop is:<br />
FOR &lt;variable name&gt; IN &lt;cursor_name&gt; LOOP<br />
&lt;statements&gt;;<br />
END LOOP;<br />
It is a machine defined loop exit i.e. when all the values in the FOR<br />
construct are exhausted looping stops.<br />
<span id="more-98"></span><br />
A cursor for loop automatically does the following:<br />
&gt;&gt; Implicitly declares its loop index or variable_name as a %rowtype<br />
record.</p>
<p>&gt;&gt; Opens a cursor.</p>
<p>&gt;&gt; Fetches a row from the cursor for each loop iteration.</p>
<p>&gt;&gt; Closes the cursor when all rows have been processed.<br />
<strong>Example</strong>:Consider a PL/SQL code to display the employee number, employee name, job of employees of  Department &#8216;Database Security&#8217; with CURSOR FOR Loop</p>
<p><strong>Solution:</strong></p>
<p>DECLARE<br />
CURSOR C1 IS SELECT EMP_NO, EMP_NAME, EMP_JOB  FROM EMP<br />
WHERE   EMP_DEPT=&#8217;DataBase Security&#8217;;</p>
<p>/*EMP_REC  is a row type variable for cursor c1    record,containing  empno, empname and job*/</p>
<p>EMP_REC C1%ROWTYPE;</p>
<p>BEGIN<br />
FOR REC IN C1<br />
LOOP<br />
FETCH C1 INTO EMP_REC;<br />
EXIT WHEN C1%NOTFOUND;<br />
&#8211;Printing Employee Number<br />
DBMS_OUTPUT.PUT_LINE(&#8216;Employee Number &#8216;||REC.EMP_NO);<br />
&#8211;Printing Employee Name<br />
DBMS_OUTPUT.PUT_LINE(&#8216;Employee Name &#8216;||REC.EMP_NAME);<br />
&#8211;Printing Employee Job<br />
DBMS_OUTPUT.PUT_LINE(&#8216;JOB &#8216;||REC.EMP_JOB);<br />
END LOOP;<br />
END;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.technooracle.com/oracle-tutorials/using-for-loop-in-cursor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Oracle Cursor : examples</title>
		<link>http://www.technooracle.com/oracle-tutorials/using-oracle-cursor-examples/</link>
		<comments>http://www.technooracle.com/oracle-tutorials/using-oracle-cursor-examples/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 19:35:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Cursor in PL/SQL]]></category>

		<guid isPermaLink="false">http://www.technooracle.com/?p=96</guid>
		<description><![CDATA[We will move to some examples showing how to Use Cursor in Oracle PL/SQL Example 1:Consider a PL/SQL code to display the employee number, employee name, job of employees of Department &#8216;Database Security&#8217; DECLARE CURSOR C1 IS SELECT EMP_NO, EMP_NAME, EMP_JOB FROM EMP WHERE EMP_DEPT=&#8217;DataBase Security&#8217;; /*EMP_REC is a row type variable for cursor c1    [...]]]></description>
			<content:encoded><![CDATA[<p>We will move to some examples showing how to Use Cursor in Oracle PL/SQL</p>
<p><strong>Example 1</strong>:Consider a PL/SQL code to display the employee number, employee name, job of employees of  Department &#8216;Database Security&#8217;</p>
<p>DECLARE<br />
CURSOR C1 IS SELECT EMP_NO, EMP_NAME, EMP_JOB  FROM EMP<br />
WHERE   EMP_DEPT=&#8217;DataBase Security&#8217;;</p>
<p>/*EMP_REC  is a row type variable for cursor c1    record,containing  empno, empname and job*/</p>
<p>EMP_REC C1%ROWTYPE;</p>
<p>BEGIN<br />
OPEN C1;<br />
LOOP<br />
FETCH C1 INTO EMP_REC;<br />
EXIT WHEN C1%NOTFOUND;<br />
&#8211;Printing Employee Number<br />
DBMS_OUTPUT.PUT_LINE(&#8216;Employee Number &#8216;||REC.EMP_NO);<br />
&#8211;Printing Employee Name<br />
DBMS_OUTPUT.PUT_LINE(&#8216;Employee Name &#8216;||REC.EMP_NAME);<br />
&#8211;Printing Employee Job<br />
DBMS_OUTPUT.PUT_LINE(&#8216;JOB &#8216;||REC.EMP_JOB);<br />
END LOOP;<br />
CLOSE C1;<br />
END;<br />
<span id="more-96"></span><br />
<strong> Example2:</strong> Consider a PL/SQL code to increase the salary of  employee according to following rule:</p>
<p>Salary of department &#8216;System Security&#8217; employees increased by 1000.<br />
Salary of deportment &#8220;DataBase Security&#8221; employees increased by 2000.<br />
Salary of department &#8220;Application Development&#8221; employees increased by 500.</p>
<p>DECLARE<br />
UPDATED_SAL  NUMBER(10);<br />
CURSOR C1 IS SELECT EMP_NO, EMP_DEPT FROM EMP;</p>
<p>/*EMP_REC  is a row type variable for cursor c1 record,containing empno, ename and job*/</p>
<p>EMP_REC C1%ROWTYPE;</p>
<p>BEGIN<br />
OPEN C1;<br />
LOOP<br />
FETCH C1 INTO EMP_REC;<br />
EXIT WHEN C1%NOTFOUND;<br />
&#8211;Checking if  Employee Belongs to Dept System Security<br />
IF REC.EMP_DEPT=&#8217;System Security&#8217; THEN<br />
UPDATE EMP SET  EMP_SAL=EMP_SAL+1000<br />
WHERE  EMP_NO=REC.EMP_NO;<br />
&#8211;Checking if  Employee Belongs to Dept DataBase Security<br />
ELSIF REC.EMP_DEPT=&#8217;DataBase Security&#8217; THEN<br />
UPDATE EMP SET  EMP_SAL=EMP_SAL+2000<br />
WHERE  EMP_NO=REC.EMP_NO;<br />
&#8211;Checking if  Employee Belongs to Dept Application Development<br />
ELSIF REC.EMP_DEPT=&#8217;Application Development&#8217; THEN<br />
UPDATE EMP SET  EMP_SAL=EMP_SAL+500<br />
WHERE  EMP_NO=REC.EMP_NO;<br />
ELSE<br />
NULL;<br />
END IF;<br />
END LOOP;<br />
CLOSE C1;<br />
END;</p>
<p><strong>Example 3</strong>:Consider a PL/SQL code to calculate the total and percentage of marks of the students in four subjects from table result<br />
The rollno and marks in each subject are stored in the database, PL/SQL<br />
code calculate the total and percentage of each student and update the<br />
database.</p>
<p>DECLARE<br />
MARK_TOTAL             NUMBER;<br />
MARK_PERCENTAGE NUMBER;<br />
CURSOR C1 IS SELECT * FROM RESULT;<br />
REC C1%ROWTYPE;<br />
BEGIN<br />
OPEN C1;<br />
LOOP<br />
FETCH C1 INTO REC;<br />
EXIT WHEN C1%NOTFOUND;<br />
TOTAL_MARK:=REC.S1+REC.S2+REC.S3+REC.S4;<br />
TOTAL_PERCENTAGE :=TOTAL_MARK/4;<br />
UPDATE RESULT SET TOTAL=TOTAL_MARK,    PERCENTAGE=TOTAL_PERCENTAGE<br />
WHERE RNO=REC.RNO;<br />
END LOOP;<br />
CLOSE C1;<br />
END;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.technooracle.com/oracle-tutorials/using-oracle-cursor-examples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

