<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title><![CDATA[Oracle DBA Discussion Forum - ORACLE PL/SQL Tutorial]]></title>
		<link>http://naturebios.com/natureboard/</link>
		<description><![CDATA[Oracle DBA Discussion Forum - http://naturebios.com/natureboard]]></description>
		<pubDate>Mon, 06 Sep 2010 16:14:16 -0700</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[Oracle PL/SQL programming Pdf]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=3099</link>
			<pubDate>Fri, 29 Jan 2010 01:23:27 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=3099</guid>
			<description><![CDATA[Oracle PL/SQL Programming (Paperback) By (author) Steven Feuerstein, By (author) Bill Pribyll pdf download]]></description>
			<content:encoded><![CDATA[Oracle PL/SQL Programming (Paperback) By (author) Steven Feuerstein, By (author) Bill Pribyll pdf download]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[PL/SQL programming Ppt Powerpoint Presentation]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=3098</link>
			<pubDate>Fri, 29 Jan 2010 01:20:39 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=3098</guid>
			<description><![CDATA[Oracle PL/SQL ppts  Programming, Third Edition by o reilly download]]></description>
			<content:encoded><![CDATA[Oracle PL/SQL ppts  Programming, Third Edition by o reilly download]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[PL/SQL Ppt Powerpoint Presentation]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=3097</link>
			<pubDate>Fri, 29 Jan 2010 01:17:07 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=3097</guid>
			<description><![CDATA[PL/SQL Ppt Powerpoint Presentation download  by Steven Feuerstein O'Reilly Media]]></description>
			<content:encoded><![CDATA[PL/SQL Ppt Powerpoint Presentation download  by Steven Feuerstein O'Reilly Media]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Plsql Cursor Life Cycle]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2853</link>
			<pubDate>Wed, 18 Nov 2009 10:57:00 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2853</guid>
			<description><![CDATA[Applications are built out of SQL statements. For optimal application performance, good understanding of cursor life cycle is needed.<br />
<br />
<br />
<span style="font-weight: bold;">Processing of a cursor involves the following steps :</span><br />
<br />
<br />
<span style="font-weight: bold;">Open cursor :</span><br />
<br />
1) Server-side private memory of the server process is UGA(user global area).<br />
<br />
2) A memory structure for the cursor is allocated in UGA.<br />
<br />
3) Just a portion of memory is allocated for the cursor and SQL statement is not associated with the memory structure.<br />
<br />
<span style="font-weight: bold;"><br />
Parse cursor :</span><br />
<br />
<br />
<br />
1)A SQL statement is associated with the cursor. Its parsed representation that<br />
includes the execution plan (which describes how the SQL engine will execute the SQL<br />
statement) is loaded in the shared pool, specifically, in the library cache. The structure in<br />
the UGA is updated to store a pointer to the location of the shareable cursor in the library<br />
cache. The next section will describe parsing in more detail.<br />
Define output variables: If the SQL statement returns data, the variables receiving it must<br />
be defined. This is necessary not only for queries but also for DELETE, INSERT, and UPDATE<br />
statements that use the RETURNING clause.<br />
Bind input variables: If the SQL statement uses bind variables, their values must be provided.<br />
No check is performed during the binding. If invalid data is passed, a runtime error will be<br />
raised during the execution.<br />
Execute cursor: The SQL statement is executed. But be careful, because the database engine<br />
doesn’t always do anything significant during this phase. In fact, for many types of queries,<br />
the real processing is usually delayed to the fetch phase.<br />
Fetch cursor: If the SQL statement returns data, this step retrieves it. Especially for queries,<br />
this step is where most of the processing is performed. In the case of queries, rows might<br />
be partially fetched. In other words, the cursor might be closed before fetching all the rows.<br />
Close cursor: The resources associated with the cursor in the UGA are freed and consequently<br />
made available for other cursors. The shareable cursor in the library cache is not<br />
removed. It remains there in the hope of being reused in the future.]]></description>
			<content:encoded><![CDATA[Applications are built out of SQL statements. For optimal application performance, good understanding of cursor life cycle is needed.<br />
<br />
<br />
<span style="font-weight: bold;">Processing of a cursor involves the following steps :</span><br />
<br />
<br />
<span style="font-weight: bold;">Open cursor :</span><br />
<br />
1) Server-side private memory of the server process is UGA(user global area).<br />
<br />
2) A memory structure for the cursor is allocated in UGA.<br />
<br />
3) Just a portion of memory is allocated for the cursor and SQL statement is not associated with the memory structure.<br />
<br />
<span style="font-weight: bold;"><br />
Parse cursor :</span><br />
<br />
<br />
<br />
1)A SQL statement is associated with the cursor. Its parsed representation that<br />
includes the execution plan (which describes how the SQL engine will execute the SQL<br />
statement) is loaded in the shared pool, specifically, in the library cache. The structure in<br />
the UGA is updated to store a pointer to the location of the shareable cursor in the library<br />
cache. The next section will describe parsing in more detail.<br />
Define output variables: If the SQL statement returns data, the variables receiving it must<br />
be defined. This is necessary not only for queries but also for DELETE, INSERT, and UPDATE<br />
statements that use the RETURNING clause.<br />
Bind input variables: If the SQL statement uses bind variables, their values must be provided.<br />
No check is performed during the binding. If invalid data is passed, a runtime error will be<br />
raised during the execution.<br />
Execute cursor: The SQL statement is executed. But be careful, because the database engine<br />
doesn’t always do anything significant during this phase. In fact, for many types of queries,<br />
the real processing is usually delayed to the fetch phase.<br />
Fetch cursor: If the SQL statement returns data, this step retrieves it. Especially for queries,<br />
this step is where most of the processing is performed. In the case of queries, rows might<br />
be partially fetched. In other words, the cursor might be closed before fetching all the rows.<br />
Close cursor: The resources associated with the cursor in the UGA are freed and consequently<br />
made available for other cursors. The shareable cursor in the library cache is not<br />
removed. It remains there in the hope of being reused in the future.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Oracle Pl Sql Interview Questions]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2835</link>
			<pubDate>Wed, 18 Nov 2009 01:19:42 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2835</guid>
			<description><![CDATA[<span style="font-weight: bold;">What should be the return type for a cursor variable. Can we use a scalar data type as return type?</span><br />
The return type for a cursor must be a record type.It can be declared explicitly as a user-defined or %ROWTYPE can be used. eg TYPE t_studentsref IS REF CURSOR RETURN students%ROWTYPE<br />
<span style="font-weight: bold;"><br />
What are different Oracle database objects?</span><br />
-TABLES<br />
-VIEWS<br />
-INDEXES<br />
-SYNONYMS<br />
-SEQUENCES<br />
-TABLESPACES etc<br />
<br />
<span style="font-weight: bold;">What is difference between SUBSTR and INSTR?</span><br />
SUBSTR returns a specified portion of a string eg SUBSTR('BCDEF',4) output BCDE INSTR provides character position in which a pattern is found in a string. eg INSTR('ABC-DC-F','-',2) output 7 (2nd occurence of '-')<br />
<span style="font-weight: bold;"><br />
Display the number value in Words?</span><br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>SQL&gt; select sal, (to_char(to_date(sal,'j'), 'jsp'))<br />
from emp;<br />
the output like,<br />
SAL (TO_CHAR(TO_DATE(SAL,'J'),'JSP'))<br />
--------- ----------------------------------------<br />
800 eight hundred<br />
1600 one thousand six hundred<br />
1250 one thousand two hundred fifty<br />
If you want to add some text like, Rs. Three Thousand only.<br />
SQL&gt; select sal "Salary ",<br />
(' Rs. '|| (to_char(to_date(sal,'j'), 'Jsp'))|| ' only.'))<br />
"Sal in Words" from emp<br />
/<br />
Salary Sal in Words<br />
------- -----------------------------------------------<br />
800 Rs. Eight Hundred only.<br />
1600 Rs. One Thousand Six Hundred only.<br />
1250 Rs. One Thousand Two Hundred Fifty only.</code></div></div>
<span style="font-weight: bold;"><br />
What is difference between SQL and SQL*PLUS?</span><br />
SQL*PLUS is a command line tool where as SQL and PL/SQL language interface and reporting tool. Its a command line tool that allows user to type SQL commands to be executed directly against an Oracle database. SQL is a language used to query the relational database(DML,DCL,DDL). SQL*PLUS commands are used to format query result, Set options, Edit SQL commands and PL/SQL.<br />
<br />
<span style="font-weight: bold;">What are various joins used while writing SUBQUERIES?</span><br />
Self join-Its a join foreign key of a table references the same table. Outer Join--Its a join condition used where One can query all the rows of one of the tables in the join condition even though they don't satisfy the join condition.<br />
Equi-join--Its a join condition that retrieves rows from one or more tables in which one or more columns in one table are equal to one or more columns in the second table.<br />
<br />
<span style="font-weight: bold;">What a SELECT FOR UPDATE cursor represent.?</span><br />
SELECT......FROM......FOR......UPDATE[OF column-reference&#93;[NOWAIT&#93;<br />
The processing done in a fetch loop modifies the rows that have been retrieved by the cursor. A convenient way of modifying the rows is done by a method with two parts: the FOR UPDATE clause in the cursor declaration, WHERE CURRENT OF CLAUSE in an UPDATE or declaration statement.<br />
<br />
<span style="font-weight: bold;">What are various privileges that a user can grant to another user?</span><br />
-SELECT<br />
-CONNECT<br />
-RESOURCES<br />
<br />
<span style="font-weight: bold;">Display the records between two range?</span><br />
select rownum, empno, ename from emp where rowid in (select rowid from emp where rownum <=&#x26;upto minus select rowid from emp where rownum<&#x26;Start);<br />
<span style="font-weight: bold;"><br />
minvalue.sql Select the Nth lowest value from a table?</span><br />
select level, min('col_name') from my_table where level = '&#x26;n' connect by prior ('col_name') < 'col_name')<br />
group by level;<br />
Example:<br />
Given a table called emp with the following columns:<br />
-- id number<br />
-- name varchar2(20)<br />
-- sal number<br />
--<br />
-- For the second lowest salary:<br />
-- select level, min(sal) from emp<br />
-- where level=2<br />
-- connect by prior sal < sal<br />
-- group by level<br />
<span style="font-weight: bold;"><br />
What is difference between Rename and Alias?</span><br />
Rename is a permanent name given to a table or column whereas Alias is a temporary name given to a table or column which do not exist once the SQL statement is executed.<br />
<span style="font-weight: bold;"><br />
Difference between an implicit &#x26; an explicit cursor.?</span><br />
only one row. However,queries that return more than one row you must declare an explicit cursor or use a cursor FOR loop. Explicit cursor is a cursor in which the cursor name is explicitly assigned to a SELECT statement via the CURSOR...IS statement. An implicit cursor is used for all SQL statements Declare, Open, Fetch, Close. An explicit cursors are used to process multirow SELECT statements An implicit cursor is used to process INSERT, UPDATE, DELETE and single row SELECT. .INTO statements.<br />
<span style="font-weight: bold;"><br />
What is a OUTER JOIN?</span><br />
Outer Join--Its a join condition used where you can query all the rows of one of the tables in the join condition even though they don?t satisfy the join condition.<br />
<span style="font-weight: bold;"><br />
What is a cursor?</span><br />
Oracle uses work area to execute SQL statements and store processing information PL/SQL construct called a cursor lets you name a work area and access its stored information A cursor is a mechanism used to fetch more than one row in a Pl/SQl block.<br />
<br />
<span style="font-weight: bold;">What is the purpose of a cluster?</span><br />
Oracle does not allow a user to specifically locate tables, since that is a part of the function of the RDBMS. However, for the purpose of increasing performance, oracle allows a developer to create a CLUSTER. A CLUSTER provides a means for storing data from different tables together for faster retrieval than if the table placement were left to the RDBMS.<br />
<br />
<span style="font-weight: bold;">What is OCI. What are its uses?</span><br />
Oracle Call Interface is a method of accesing database from a 3GL program. Uses--No precompiler is required,PL/SQL blocks are executed like other DML statements.<br />
The OCI library provides<br />
--functions to parse SQL statemets<br />
--bind input variables<br />
--bind output variables<br />
--execute statements<br />
--fetch the results<br />
<span style="font-weight: bold;"><br />
How you open and close a cursor variable. Why it is required?</span><br />
OPEN cursor variable FOR SELECT...Statement<br />
CLOSE cursor variable In order to associate a cursor variable with a particular SELECT statement OPEN syntax is used. In order to free the resources used for the query CLOSE statement is used.<br />
<span style="font-weight: bold;"><br />
Display Odd/ Even number of records?</span><br />
Odd number of records:<br />
select * from emp where (rowid,1) in (select rowid, mod(rownum,2) from emp);<br />
Output:-<br />
1<br />
3<br />
5<br />
Even number of records:<br />
select * from emp where (rowid,0) in (select rowid, mod(rownum,2) from emp)<br />
Output:-<br />
2<br />
4<br />
6<br />
<span style="font-weight: bold;"><br />
What are various constraints used in SQL?</span><br />
-NULL<br />
-NOT NULL<br />
-CHECK<br />
-DEFAULT<br />
<br />
<span style="font-weight: bold;">Can cursor variables be stored in PL/SQL tables. If yes how. If not why?</span><br />
No, a cursor variable points a row which cannot be stored in a two-dimensional PL/SQL table.<br />
<span style="font-weight: bold;"><br />
Difference between NO DATA FOUND and %NOTFOUND?</span><br />
NO DATA FOUND is an exception raised only for the SELECT....INTO statements when the where clause of the querydoes not match any rows. When the where clause of the explicit cursor does not match any rows the %NOTFOUND attribute is set to TRUE instead.<br />
<br />
<span style="font-weight: bold;">Can you use a commit statement within a database trigger?</span><br />
No<br />
<span style="font-weight: bold;"><br />
What WHERE CURRENT OF clause does in a cursor?</span><br />
LOOP<br />
SELECT num_credits INTO v_numcredits FROM classes<br />
WHERE dept=123 and course=101;<br />
UPDATE students<br />
FHKO;;;;;;;;;SET current_credits=current_credits+v_numcredits<br />
WHERE CURRENT OF X;<br />
<br />
<span style="font-weight: bold;">There is a string 120000 12 0 .125 , how you will find the position of the decimal place?</span><br />
INSTR('120000 12 0 .125',1,'.')<br />
output 13<br />
<br />
<span style="font-weight: bold;">What are different modes of parameters used in functions and procedures?</span><br />
-IN -OUT -INOUT<br />
<br />
<span style="font-weight: bold;">How you were passing cursor variables in PL/SQL 2.2?</span><br />
In PL/SQL 2.2 cursor variables cannot be declared in a package.This is because the storage for a cursor variable has to be allocated using Pro*C or OCI with version 2.2, the only means of passing a cursor variable to a PL/SQL block is via bind variable or a procedure parameter.<br />
<span style="font-weight: bold;"><br />
When do you use WHERE clause and when do you use HAVING clause?</span><br />
HAVING clause is used when you want to specify a condition for a group function and it is written after GROUP BY clause. The WHERE clause is used when you want to specify a condition for columns, single row functions except group functions and it is written before GROUP BY clause if it is used.<br />
<br />
<span style="font-weight: bold;">Difference between procedure and function.?</span><br />
Functions are named PL/SQL blocks that return a value and can be called with arguments procedure a named block that can be called with parameter. A procedure all is a PL/SQL statement by itself, while a Function call is called as part of an expression.<br />
<span style="font-weight: bold;"><br />
Which is more faster - IN or EXISTS?</span><br />
EXISTS is more faster than IN because EXISTS returns a Boolean value whereas IN returns a value.<br />
<br />
<span style="font-weight: bold;">What is syntax for dropping a procedure and a function .Are these operations possible?</span><br />
Drop Procedure procedure_name<br />
Drop Function function_name<br />
<span style="font-weight: bold;"><br />
How will you delete duplicating rows from a base table?</span><br />
delete from table_name where rowid not in (select max(rowid) from table group by duplicate_values_field_name); or delete duplicate_values_field_name dv from table_name ta where rowid <(select min(rowid) from table_name tb where ta.dv=tb.dv);<br />
<br />
<span style="font-weight: bold;">Difference between database triggers and form triggers?</span><br />
-Data base trigger(DBT) fires when a DML operation is performed on a data base table. Form trigger(FT) Fires when user presses a key or navigates between fields on the screen<br />
-Can be row level or statement level No distinction between row level and statement level.<br />
-Can manipulate data stored in Oracle tables via SQL Can manipulate data in Oracle tables as well as variables in forms.<br />
-Can be fired from any session executing the triggering DML statements. Can be fired only from the form that define the trigger.<br />
-Can cause other database triggers to fire. Can cause other database triggers to fire, but not other form triggers.<br />
<br />
<span style="font-weight: bold;">What is a cursor for loop?</span><br />
Cursor For Loop is a loop where oracle implicitly declares a loop variable, the loop index that of the same record type as the cursor's record.<br />
<br />
<span style="font-weight: bold;">How you will avoid duplicating records in a query?</span><br />
By using DISTINCT<br />
<br />
<span style="font-weight: bold;">What is a view ?</span><br />
A view is stored procedure based on one or more tables, it?s a virtual table.<br />
<span style="font-weight: bold;"><br />
What is difference between UNIQUE and PRIMARY KEY constraints?</span><br />
A table can have only one PRIMARY KEY whereas there can be any number of UNIQUE keys. The columns that compose PK are automatically define NOT NULL, whereas a column that compose a UNIQUE is not automatically defined to be mandatory must also specify the column is NOT NULL.<br />
<span style="font-weight: bold;"><br />
What is use of a cursor variable? How it is defined?</span><br />
A cursor variable is associated with different statements at run time, which can hold different values at run time. Static cursors can only be associated with one run time query. A cursor variable is reference type (like a pointer in C).<br />
<span style="font-weight: bold;">Declaring a cursor variable:</span><br />
TYPE type_name IS REF CURSOR RETURN return_type type_name is the name of the reference type,return_type is a record type indicating the types of the select list that will eventually be returned by the cursor variable.<br />
<br />
<span style="font-weight: bold;">How do you find the numbert of rows in a Table ?</span><br />
A bad answer is count them (SELECT COUNT(*) FROM table_name)<br />
A good answer is :-<br />
'By generating SQL to ANALYZE TABLE table_name COUNT STATISTICS by querying Oracle System Catalogues (e.g. USER_TABLES or ALL_TABLES).<br />
The best answer is to refer to the utility which Oracle released which makes it unnecessary to do ANALYZE TABLE for each Table individually.<br />
<br />
<span style="font-weight: bold;">What is the maximum buffer size that can be specified using the DBMS_OUTPUT.ENABLE function?</span><br />
1,000,00<br />
<span style="font-weight: bold;"><br />
What are cursor attributes?</span><br />
-%ROWCOUNT<br />
-%NOTFOUND<br />
-%FOUND<br />
-%ISOPEN<br />
<br />
<span style="font-weight: bold;">There is a % sign in one field of a column. What will be the query to find it?</span><br />
'' Should be used before '%'.<br />
<br />
<span style="font-weight: bold;">What is ON DELETE CASCADE ?</span><br />
When ON DELETE CASCADE is specified ORACLE maintains referential integrity by automatically removing dependent foreign key values if a referenced primary or unique key value is removed.<br />
<span style="font-weight: bold;"><br />
What is the fastest way of accessing a row in a table ?</span><br />
Using ROWID.CONSTRAINTS<br />
<br />
<span style="font-weight: bold;">What is difference between TRUNCATE &#x26; DELETE ?</span><br />
TRUNCATE commits after deleting entire table i.e., can not be rolled back. Database triggers do not fire on TRUNCATEDELETE allows the filtered deletion. Deleted records can be rolled back or committed. Database triggers fire on DELETE.<br />
<span style="font-weight: bold;"><br />
What is a transaction ?</span><br />
Transaction is logical unit between two commits and commit and rollback.<br />
<span style="font-weight: bold;"><br />
What are the advantages of VIEW ?</span><br />
To protect some of the columns of a table from other users.To hide complexity of a query.To hide complexity of calculations.<br />
<br />
<span style="font-weight: bold;">How will you a activate/deactivate integrity constraints ?</span><br />
The integrity constraints can be enabled or disabled by ALTER TABLE ENABLE constraint/DISABLE constraint.<br />
<br />
<span style="font-weight: bold;">Where the integrity constraints are stored in Data Dictionary ?</span><br />
The integrity constraints are stored in USER_CONSTRAINTS.<br />
<span style="font-weight: bold;"><br />
What is the Subquery ?</span><br />
Sub query is a query whose return values are used in filtering conditions of the main query.<br />
<br />
<span style="font-weight: bold;">How to access the current value and next value from a sequence ? Is it possible to access the current value in a session before accessing next value ?</span><br />
Sequence name CURRVAL, Sequence name NEXTVAL.It is not possible. Only if you access next value in the session, current value can be accessed.<br />
<br />
<span style="font-weight: bold;">What are the usage of SAVEPOINTS ?value in a session before accessing next value ?</span><br />
SAVEPOINTS are used to subdivide a transaction into smaller parts. It enables rolling back part of a transaction. Maximum of five save points are allowed.<br />
<span style="font-weight: bold;"><br />
What is ROWID ?in a session before accessing next value ?</span><br />
ROWID is a pseudo column attached to each row of a table. It is 18 character long, blockno, rownumber are the components of ROWID.<br />
<span style="font-weight: bold;"><br />
Explain Connect by Prior ?in a session before accessing next value ?</span><br />
Retrieves rows in hierarchical order.e.g. select empno, ename from emp where.<br />
<span style="font-weight: bold;"><br />
How many LONG columns are allowed in a table ? Is it possible to use LONG columns in WHERE clause or ORDER BY ?</span><br />
Only one LONG columns is allowed. It is not possible to use LONG column in WHERE or ORDER BY clause.<br />
<br />
<span style="font-weight: bold;">What is Referential Integrity ?</span><br />
Maintaining data integrity through a set of rules that restrict the values of one or more columns of the tables based on the values of primary key or unique key of the referenced table.<br />
<br />
<span style="font-weight: bold;">What is a join ? Explain the different types of joins ?</span><br />
Join is a query which retrieves related columns or rows from multiple tables.Self Join - Joining the table with itself.Equi Join - Joining two tables by equating two common columns.Non-Equi Join - Joining two tables by equating two common columns.Outer Join - Joining two tables in such a way that query can also retrieve rows that do not have corresponding join value in the other table.<br />
<span style="font-weight: bold;"><br />
If an unique key constraint on DATE column is created, will it validate the rows that are inserted with SYSDATE ?</span><br />
It won't, Because SYSDATE format contains time attached with it.]]></description>
			<content:encoded><![CDATA[<span style="font-weight: bold;">What should be the return type for a cursor variable. Can we use a scalar data type as return type?</span><br />
The return type for a cursor must be a record type.It can be declared explicitly as a user-defined or %ROWTYPE can be used. eg TYPE t_studentsref IS REF CURSOR RETURN students%ROWTYPE<br />
<span style="font-weight: bold;"><br />
What are different Oracle database objects?</span><br />
-TABLES<br />
-VIEWS<br />
-INDEXES<br />
-SYNONYMS<br />
-SEQUENCES<br />
-TABLESPACES etc<br />
<br />
<span style="font-weight: bold;">What is difference between SUBSTR and INSTR?</span><br />
SUBSTR returns a specified portion of a string eg SUBSTR('BCDEF',4) output BCDE INSTR provides character position in which a pattern is found in a string. eg INSTR('ABC-DC-F','-',2) output 7 (2nd occurence of '-')<br />
<span style="font-weight: bold;"><br />
Display the number value in Words?</span><br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>SQL&gt; select sal, (to_char(to_date(sal,'j'), 'jsp'))<br />
from emp;<br />
the output like,<br />
SAL (TO_CHAR(TO_DATE(SAL,'J'),'JSP'))<br />
--------- ----------------------------------------<br />
800 eight hundred<br />
1600 one thousand six hundred<br />
1250 one thousand two hundred fifty<br />
If you want to add some text like, Rs. Three Thousand only.<br />
SQL&gt; select sal "Salary ",<br />
(' Rs. '|| (to_char(to_date(sal,'j'), 'Jsp'))|| ' only.'))<br />
"Sal in Words" from emp<br />
/<br />
Salary Sal in Words<br />
------- -----------------------------------------------<br />
800 Rs. Eight Hundred only.<br />
1600 Rs. One Thousand Six Hundred only.<br />
1250 Rs. One Thousand Two Hundred Fifty only.</code></div></div>
<span style="font-weight: bold;"><br />
What is difference between SQL and SQL*PLUS?</span><br />
SQL*PLUS is a command line tool where as SQL and PL/SQL language interface and reporting tool. Its a command line tool that allows user to type SQL commands to be executed directly against an Oracle database. SQL is a language used to query the relational database(DML,DCL,DDL). SQL*PLUS commands are used to format query result, Set options, Edit SQL commands and PL/SQL.<br />
<br />
<span style="font-weight: bold;">What are various joins used while writing SUBQUERIES?</span><br />
Self join-Its a join foreign key of a table references the same table. Outer Join--Its a join condition used where One can query all the rows of one of the tables in the join condition even though they don't satisfy the join condition.<br />
Equi-join--Its a join condition that retrieves rows from one or more tables in which one or more columns in one table are equal to one or more columns in the second table.<br />
<br />
<span style="font-weight: bold;">What a SELECT FOR UPDATE cursor represent.?</span><br />
SELECT......FROM......FOR......UPDATE[OF column-reference][NOWAIT]<br />
The processing done in a fetch loop modifies the rows that have been retrieved by the cursor. A convenient way of modifying the rows is done by a method with two parts: the FOR UPDATE clause in the cursor declaration, WHERE CURRENT OF CLAUSE in an UPDATE or declaration statement.<br />
<br />
<span style="font-weight: bold;">What are various privileges that a user can grant to another user?</span><br />
-SELECT<br />
-CONNECT<br />
-RESOURCES<br />
<br />
<span style="font-weight: bold;">Display the records between two range?</span><br />
select rownum, empno, ename from emp where rowid in (select rowid from emp where rownum <=&upto minus select rowid from emp where rownum<&Start);<br />
<span style="font-weight: bold;"><br />
minvalue.sql Select the Nth lowest value from a table?</span><br />
select level, min('col_name') from my_table where level = '&n' connect by prior ('col_name') < 'col_name')<br />
group by level;<br />
Example:<br />
Given a table called emp with the following columns:<br />
-- id number<br />
-- name varchar2(20)<br />
-- sal number<br />
--<br />
-- For the second lowest salary:<br />
-- select level, min(sal) from emp<br />
-- where level=2<br />
-- connect by prior sal < sal<br />
-- group by level<br />
<span style="font-weight: bold;"><br />
What is difference between Rename and Alias?</span><br />
Rename is a permanent name given to a table or column whereas Alias is a temporary name given to a table or column which do not exist once the SQL statement is executed.<br />
<span style="font-weight: bold;"><br />
Difference between an implicit & an explicit cursor.?</span><br />
only one row. However,queries that return more than one row you must declare an explicit cursor or use a cursor FOR loop. Explicit cursor is a cursor in which the cursor name is explicitly assigned to a SELECT statement via the CURSOR...IS statement. An implicit cursor is used for all SQL statements Declare, Open, Fetch, Close. An explicit cursors are used to process multirow SELECT statements An implicit cursor is used to process INSERT, UPDATE, DELETE and single row SELECT. .INTO statements.<br />
<span style="font-weight: bold;"><br />
What is a OUTER JOIN?</span><br />
Outer Join--Its a join condition used where you can query all the rows of one of the tables in the join condition even though they don?t satisfy the join condition.<br />
<span style="font-weight: bold;"><br />
What is a cursor?</span><br />
Oracle uses work area to execute SQL statements and store processing information PL/SQL construct called a cursor lets you name a work area and access its stored information A cursor is a mechanism used to fetch more than one row in a Pl/SQl block.<br />
<br />
<span style="font-weight: bold;">What is the purpose of a cluster?</span><br />
Oracle does not allow a user to specifically locate tables, since that is a part of the function of the RDBMS. However, for the purpose of increasing performance, oracle allows a developer to create a CLUSTER. A CLUSTER provides a means for storing data from different tables together for faster retrieval than if the table placement were left to the RDBMS.<br />
<br />
<span style="font-weight: bold;">What is OCI. What are its uses?</span><br />
Oracle Call Interface is a method of accesing database from a 3GL program. Uses--No precompiler is required,PL/SQL blocks are executed like other DML statements.<br />
The OCI library provides<br />
--functions to parse SQL statemets<br />
--bind input variables<br />
--bind output variables<br />
--execute statements<br />
--fetch the results<br />
<span style="font-weight: bold;"><br />
How you open and close a cursor variable. Why it is required?</span><br />
OPEN cursor variable FOR SELECT...Statement<br />
CLOSE cursor variable In order to associate a cursor variable with a particular SELECT statement OPEN syntax is used. In order to free the resources used for the query CLOSE statement is used.<br />
<span style="font-weight: bold;"><br />
Display Odd/ Even number of records?</span><br />
Odd number of records:<br />
select * from emp where (rowid,1) in (select rowid, mod(rownum,2) from emp);<br />
Output:-<br />
1<br />
3<br />
5<br />
Even number of records:<br />
select * from emp where (rowid,0) in (select rowid, mod(rownum,2) from emp)<br />
Output:-<br />
2<br />
4<br />
6<br />
<span style="font-weight: bold;"><br />
What are various constraints used in SQL?</span><br />
-NULL<br />
-NOT NULL<br />
-CHECK<br />
-DEFAULT<br />
<br />
<span style="font-weight: bold;">Can cursor variables be stored in PL/SQL tables. If yes how. If not why?</span><br />
No, a cursor variable points a row which cannot be stored in a two-dimensional PL/SQL table.<br />
<span style="font-weight: bold;"><br />
Difference between NO DATA FOUND and %NOTFOUND?</span><br />
NO DATA FOUND is an exception raised only for the SELECT....INTO statements when the where clause of the querydoes not match any rows. When the where clause of the explicit cursor does not match any rows the %NOTFOUND attribute is set to TRUE instead.<br />
<br />
<span style="font-weight: bold;">Can you use a commit statement within a database trigger?</span><br />
No<br />
<span style="font-weight: bold;"><br />
What WHERE CURRENT OF clause does in a cursor?</span><br />
LOOP<br />
SELECT num_credits INTO v_numcredits FROM classes<br />
WHERE dept=123 and course=101;<br />
UPDATE students<br />
FHKO;;;;;;;;;SET current_credits=current_credits+v_numcredits<br />
WHERE CURRENT OF X;<br />
<br />
<span style="font-weight: bold;">There is a string 120000 12 0 .125 , how you will find the position of the decimal place?</span><br />
INSTR('120000 12 0 .125',1,'.')<br />
output 13<br />
<br />
<span style="font-weight: bold;">What are different modes of parameters used in functions and procedures?</span><br />
-IN -OUT -INOUT<br />
<br />
<span style="font-weight: bold;">How you were passing cursor variables in PL/SQL 2.2?</span><br />
In PL/SQL 2.2 cursor variables cannot be declared in a package.This is because the storage for a cursor variable has to be allocated using Pro*C or OCI with version 2.2, the only means of passing a cursor variable to a PL/SQL block is via bind variable or a procedure parameter.<br />
<span style="font-weight: bold;"><br />
When do you use WHERE clause and when do you use HAVING clause?</span><br />
HAVING clause is used when you want to specify a condition for a group function and it is written after GROUP BY clause. The WHERE clause is used when you want to specify a condition for columns, single row functions except group functions and it is written before GROUP BY clause if it is used.<br />
<br />
<span style="font-weight: bold;">Difference between procedure and function.?</span><br />
Functions are named PL/SQL blocks that return a value and can be called with arguments procedure a named block that can be called with parameter. A procedure all is a PL/SQL statement by itself, while a Function call is called as part of an expression.<br />
<span style="font-weight: bold;"><br />
Which is more faster - IN or EXISTS?</span><br />
EXISTS is more faster than IN because EXISTS returns a Boolean value whereas IN returns a value.<br />
<br />
<span style="font-weight: bold;">What is syntax for dropping a procedure and a function .Are these operations possible?</span><br />
Drop Procedure procedure_name<br />
Drop Function function_name<br />
<span style="font-weight: bold;"><br />
How will you delete duplicating rows from a base table?</span><br />
delete from table_name where rowid not in (select max(rowid) from table group by duplicate_values_field_name); or delete duplicate_values_field_name dv from table_name ta where rowid <(select min(rowid) from table_name tb where ta.dv=tb.dv);<br />
<br />
<span style="font-weight: bold;">Difference between database triggers and form triggers?</span><br />
-Data base trigger(DBT) fires when a DML operation is performed on a data base table. Form trigger(FT) Fires when user presses a key or navigates between fields on the screen<br />
-Can be row level or statement level No distinction between row level and statement level.<br />
-Can manipulate data stored in Oracle tables via SQL Can manipulate data in Oracle tables as well as variables in forms.<br />
-Can be fired from any session executing the triggering DML statements. Can be fired only from the form that define the trigger.<br />
-Can cause other database triggers to fire. Can cause other database triggers to fire, but not other form triggers.<br />
<br />
<span style="font-weight: bold;">What is a cursor for loop?</span><br />
Cursor For Loop is a loop where oracle implicitly declares a loop variable, the loop index that of the same record type as the cursor's record.<br />
<br />
<span style="font-weight: bold;">How you will avoid duplicating records in a query?</span><br />
By using DISTINCT<br />
<br />
<span style="font-weight: bold;">What is a view ?</span><br />
A view is stored procedure based on one or more tables, it?s a virtual table.<br />
<span style="font-weight: bold;"><br />
What is difference between UNIQUE and PRIMARY KEY constraints?</span><br />
A table can have only one PRIMARY KEY whereas there can be any number of UNIQUE keys. The columns that compose PK are automatically define NOT NULL, whereas a column that compose a UNIQUE is not automatically defined to be mandatory must also specify the column is NOT NULL.<br />
<span style="font-weight: bold;"><br />
What is use of a cursor variable? How it is defined?</span><br />
A cursor variable is associated with different statements at run time, which can hold different values at run time. Static cursors can only be associated with one run time query. A cursor variable is reference type (like a pointer in C).<br />
<span style="font-weight: bold;">Declaring a cursor variable:</span><br />
TYPE type_name IS REF CURSOR RETURN return_type type_name is the name of the reference type,return_type is a record type indicating the types of the select list that will eventually be returned by the cursor variable.<br />
<br />
<span style="font-weight: bold;">How do you find the numbert of rows in a Table ?</span><br />
A bad answer is count them (SELECT COUNT(*) FROM table_name)<br />
A good answer is :-<br />
'By generating SQL to ANALYZE TABLE table_name COUNT STATISTICS by querying Oracle System Catalogues (e.g. USER_TABLES or ALL_TABLES).<br />
The best answer is to refer to the utility which Oracle released which makes it unnecessary to do ANALYZE TABLE for each Table individually.<br />
<br />
<span style="font-weight: bold;">What is the maximum buffer size that can be specified using the DBMS_OUTPUT.ENABLE function?</span><br />
1,000,00<br />
<span style="font-weight: bold;"><br />
What are cursor attributes?</span><br />
-%ROWCOUNT<br />
-%NOTFOUND<br />
-%FOUND<br />
-%ISOPEN<br />
<br />
<span style="font-weight: bold;">There is a % sign in one field of a column. What will be the query to find it?</span><br />
'' Should be used before '%'.<br />
<br />
<span style="font-weight: bold;">What is ON DELETE CASCADE ?</span><br />
When ON DELETE CASCADE is specified ORACLE maintains referential integrity by automatically removing dependent foreign key values if a referenced primary or unique key value is removed.<br />
<span style="font-weight: bold;"><br />
What is the fastest way of accessing a row in a table ?</span><br />
Using ROWID.CONSTRAINTS<br />
<br />
<span style="font-weight: bold;">What is difference between TRUNCATE & DELETE ?</span><br />
TRUNCATE commits after deleting entire table i.e., can not be rolled back. Database triggers do not fire on TRUNCATEDELETE allows the filtered deletion. Deleted records can be rolled back or committed. Database triggers fire on DELETE.<br />
<span style="font-weight: bold;"><br />
What is a transaction ?</span><br />
Transaction is logical unit between two commits and commit and rollback.<br />
<span style="font-weight: bold;"><br />
What are the advantages of VIEW ?</span><br />
To protect some of the columns of a table from other users.To hide complexity of a query.To hide complexity of calculations.<br />
<br />
<span style="font-weight: bold;">How will you a activate/deactivate integrity constraints ?</span><br />
The integrity constraints can be enabled or disabled by ALTER TABLE ENABLE constraint/DISABLE constraint.<br />
<br />
<span style="font-weight: bold;">Where the integrity constraints are stored in Data Dictionary ?</span><br />
The integrity constraints are stored in USER_CONSTRAINTS.<br />
<span style="font-weight: bold;"><br />
What is the Subquery ?</span><br />
Sub query is a query whose return values are used in filtering conditions of the main query.<br />
<br />
<span style="font-weight: bold;">How to access the current value and next value from a sequence ? Is it possible to access the current value in a session before accessing next value ?</span><br />
Sequence name CURRVAL, Sequence name NEXTVAL.It is not possible. Only if you access next value in the session, current value can be accessed.<br />
<br />
<span style="font-weight: bold;">What are the usage of SAVEPOINTS ?value in a session before accessing next value ?</span><br />
SAVEPOINTS are used to subdivide a transaction into smaller parts. It enables rolling back part of a transaction. Maximum of five save points are allowed.<br />
<span style="font-weight: bold;"><br />
What is ROWID ?in a session before accessing next value ?</span><br />
ROWID is a pseudo column attached to each row of a table. It is 18 character long, blockno, rownumber are the components of ROWID.<br />
<span style="font-weight: bold;"><br />
Explain Connect by Prior ?in a session before accessing next value ?</span><br />
Retrieves rows in hierarchical order.e.g. select empno, ename from emp where.<br />
<span style="font-weight: bold;"><br />
How many LONG columns are allowed in a table ? Is it possible to use LONG columns in WHERE clause or ORDER BY ?</span><br />
Only one LONG columns is allowed. It is not possible to use LONG column in WHERE or ORDER BY clause.<br />
<br />
<span style="font-weight: bold;">What is Referential Integrity ?</span><br />
Maintaining data integrity through a set of rules that restrict the values of one or more columns of the tables based on the values of primary key or unique key of the referenced table.<br />
<br />
<span style="font-weight: bold;">What is a join ? Explain the different types of joins ?</span><br />
Join is a query which retrieves related columns or rows from multiple tables.Self Join - Joining the table with itself.Equi Join - Joining two tables by equating two common columns.Non-Equi Join - Joining two tables by equating two common columns.Outer Join - Joining two tables in such a way that query can also retrieve rows that do not have corresponding join value in the other table.<br />
<span style="font-weight: bold;"><br />
If an unique key constraint on DATE column is created, will it validate the rows that are inserted with SYSDATE ?</span><br />
It won't, Because SYSDATE format contains time attached with it.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[CYCLIC CASCADING in a TRIGGER]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2731</link>
			<pubDate>Sat, 14 Nov 2009 15:37:53 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2731</guid>
			<description><![CDATA[This is an undesirable situation where more than one trigger enter into an infinite loop. while creating a trigger we should ensure the such a situtation does not exist.<br />
<br />
The below example shows how Trigger's can enter into cyclic cascading.<br />
Let's consider we have two tables 'abc' and 'xyz'. Two triggers are created.<br />
1) The INSERT Trigger, triggerA on table 'abc' issues an UPDATE on table 'xyz'.<br />
2) The UPDATE Trigger, triggerB on table 'xyz' issues an INSERT on table 'abc'.<br />
<br />
In such a situation, when there is a row inserted in table 'abc', triggerA fires and will update table 'xyz'.<br />
When the table 'xyz' is updated, triggerB fires and will insert a row in table 'abc'.<br />
This cyclic situation continues and will enter into a infinite loop, which will crash the database.]]></description>
			<content:encoded><![CDATA[This is an undesirable situation where more than one trigger enter into an infinite loop. while creating a trigger we should ensure the such a situtation does not exist.<br />
<br />
The below example shows how Trigger's can enter into cyclic cascading.<br />
Let's consider we have two tables 'abc' and 'xyz'. Two triggers are created.<br />
1) The INSERT Trigger, triggerA on table 'abc' issues an UPDATE on table 'xyz'.<br />
2) The UPDATE Trigger, triggerB on table 'xyz' issues an INSERT on table 'abc'.<br />
<br />
In such a situation, when there is a row inserted in table 'abc', triggerA fires and will update table 'xyz'.<br />
When the table 'xyz' is updated, triggerB fires and will insert a row in table 'abc'.<br />
This cyclic situation continues and will enter into a infinite loop, which will crash the database.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[How To know Information about Triggers]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2730</link>
			<pubDate>Sat, 14 Nov 2009 15:37:22 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2730</guid>
			<description><![CDATA[We can use the data dictionary view 'USER_TRIGGERS' to obtain information about any trigger.<br />
<br />
The below statement shows the structure of the view 'USER_TRIGGERS'<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>DESC USER_TRIGGERS; <br />
<br />
NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Type<br />
<br />
--------------------------------------------------------<br />
<br />
TRIGGER_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VARCHAR2(30)<br />
TRIGGER_TYPE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(16)<br />
TRIGGER_EVENT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(75)<br />
TABLE_OWNER&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(30)<br />
BASE_OBJECT_TYPE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VARCHAR2(16)<br />
TABLE_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VARCHAR2(30)<br />
COLUMN_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(4000)<br />
REFERENCING_NAMES&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(128)<br />
WHEN_CLAUSE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(4000)<br />
STATUS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(8)<br />
DESCRIPTION&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(4000)<br />
ACTION_TYPE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VARCHAR2(11)<br />
TRIGGER_BODY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LONG</code></div></div>
<br />
This view stores information about header and body of the trigger.<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>SELECT * FROM user_triggers WHERE trigger_name = 'Before_Update_Stat_product';</code></div></div>
<br />
The above sql query provides the header and body of the trigger 'Before_Update_Stat_product'.<br />
<br />
You can drop a trigger using the following command.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>DROP TRIGGER trigger_name;</code></div></div>
]]></description>
			<content:encoded><![CDATA[We can use the data dictionary view 'USER_TRIGGERS' to obtain information about any trigger.<br />
<br />
The below statement shows the structure of the view 'USER_TRIGGERS'<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>DESC USER_TRIGGERS; <br />
<br />
NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Type<br />
<br />
--------------------------------------------------------<br />
<br />
TRIGGER_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VARCHAR2(30)<br />
TRIGGER_TYPE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(16)<br />
TRIGGER_EVENT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(75)<br />
TABLE_OWNER&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(30)<br />
BASE_OBJECT_TYPE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VARCHAR2(16)<br />
TABLE_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VARCHAR2(30)<br />
COLUMN_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(4000)<br />
REFERENCING_NAMES&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(128)<br />
WHEN_CLAUSE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(4000)<br />
STATUS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(8)<br />
DESCRIPTION&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VARCHAR2(4000)<br />
ACTION_TYPE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; VARCHAR2(11)<br />
TRIGGER_BODY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LONG</code></div></div>
<br />
This view stores information about header and body of the trigger.<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>SELECT * FROM user_triggers WHERE trigger_name = 'Before_Update_Stat_product';</code></div></div>
<br />
The above sql query provides the header and body of the trigger 'Before_Update_Stat_product'.<br />
<br />
You can drop a trigger using the following command.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>DROP TRIGGER trigger_name;</code></div></div>
]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[PL/SQL Trigger Execution Hierarchy]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2729</link>
			<pubDate>Sat, 14 Nov 2009 15:36:13 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2729</guid>
			<description><![CDATA[The following hierarchy is followed when a trigger is fired.<br />
1) BEFORE statement trigger fires first.<br />
2) Next BEFORE row level trigger fires, once for each row affected.<br />
3) Then AFTER row level trigger fires once for each affected row. This events will alternates between BEFORE and AFTER row level triggers.<br />
4) Finally the AFTER statement level trigger fires.<br />
<br />
For Example: Let's create a table 'product_check' which we can use to store messages when triggers are fired.<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE TABLE product<br />
<br />
(Message varchar2(50), <br />
<br />
 Current_Date number(32)<br />
<br />
);</code></div></div>
Let's create a BEFORE and AFTER statement and row level triggers for the product table.<br />
<br />
1) BEFORE UPDATE, Statement Level: This trigger will insert a record into the table 'product_check' before a sql update statement is executed, at the <div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>statement level.<br />
<br />
CREATE or REPLACE TRIGGER Before_Update_Stat_product <br />
<br />
BEFORE <br />
<br />
UPDATE ON product <br />
<br />
Begin <br />
<br />
INSERT INTO product_check <br />
<br />
Values('Before update, statement level',sysdate); <br />
<br />
END; <br />
<br />
/</code></div></div>
2) BEFORE UPDATE, Row Level: This trigger will insert a record into the table 'product_check' before each row is updated.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE or REPLACE TRIGGER Before_Upddate_Row_product <br />
<br />
 BEFORE <br />
<br />
 UPDATE ON product <br />
<br />
 FOR EACH ROW <br />
<br />
 BEGIN <br />
<br />
 INSERT INTO product_check <br />
<br />
 Values('Before update row level',sysdate); <br />
<br />
 END; <br />
<br />
 /</code></div></div>
<br />
3) AFTER UPDATE, Statement Level: This trigger will insert a record into the table 'product_check' after a sql update statement is executed, at the statement level.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE or REPLACE TRIGGER After_Update_Stat_product <br />
<br />
 AFTER <br />
<br />
 UPDATE ON product <br />
<br />
 BEGIN <br />
<br />
 INSERT INTO product_check <br />
<br />
 Values('After update, statement level', sysdate); <br />
<br />
 End; <br />
<br />
 / <br />
<br />
4) AFTER UPDATE, Row Level: This trigger will insert a record into the table 'product_check' after each row is updated.<br />
<br />
 CREATE or REPLACE TRIGGER After_Update_Row_product <br />
<br />
 AFTER&nbsp;&nbsp;<br />
<br />
 insert On product <br />
<br />
 FOR EACH ROW <br />
<br />
 BEGIN <br />
<br />
 INSERT INTO product_check <br />
<br />
 Values('After update, Row level',sysdate); <br />
<br />
 END; <br />
<br />
 / <br />
<br />
Now lets execute a update statement on table product.<br />
<br />
 UPDATE PRODUCT SET unit_price = 800&nbsp;&nbsp;<br />
<br />
 WHERE product_id in (100,101); <br />
<br />
Lets check the data in 'product_check' table to see the order in which the trigger is fired.<br />
<br />
 SELECT * FROM product_check; <br />
<br />
Output:<br />
<br />
Mesage&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Current_Date<br />
<br />
------------------------------------------------------------<br />
<br />
Before update, statement level&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;26-Nov-2008<br />
Before update, row level&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;26-Nov-2008<br />
After update, Row level&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 26-Nov-2008<br />
Before update, row level&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;26-Nov-2008<br />
After update, Row level&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 26-Nov-2008<br />
After update, statement level&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;26-Nov-2008</code></div></div>
<br />
The above result shows 'before update' and 'after update' row level events have occured twice, since two records were updated. But 'before update' and 'after update' statement level events are fired only once per sql statement.<br />
<br />
The above rules apply similarly for INSERT and DELETE statements.]]></description>
			<content:encoded><![CDATA[The following hierarchy is followed when a trigger is fired.<br />
1) BEFORE statement trigger fires first.<br />
2) Next BEFORE row level trigger fires, once for each row affected.<br />
3) Then AFTER row level trigger fires once for each affected row. This events will alternates between BEFORE and AFTER row level triggers.<br />
4) Finally the AFTER statement level trigger fires.<br />
<br />
For Example: Let's create a table 'product_check' which we can use to store messages when triggers are fired.<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE TABLE product<br />
<br />
(Message varchar2(50), <br />
<br />
 Current_Date number(32)<br />
<br />
);</code></div></div>
Let's create a BEFORE and AFTER statement and row level triggers for the product table.<br />
<br />
1) BEFORE UPDATE, Statement Level: This trigger will insert a record into the table 'product_check' before a sql update statement is executed, at the <div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>statement level.<br />
<br />
CREATE or REPLACE TRIGGER Before_Update_Stat_product <br />
<br />
BEFORE <br />
<br />
UPDATE ON product <br />
<br />
Begin <br />
<br />
INSERT INTO product_check <br />
<br />
Values('Before update, statement level',sysdate); <br />
<br />
END; <br />
<br />
/</code></div></div>
2) BEFORE UPDATE, Row Level: This trigger will insert a record into the table 'product_check' before each row is updated.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE or REPLACE TRIGGER Before_Upddate_Row_product <br />
<br />
 BEFORE <br />
<br />
 UPDATE ON product <br />
<br />
 FOR EACH ROW <br />
<br />
 BEGIN <br />
<br />
 INSERT INTO product_check <br />
<br />
 Values('Before update row level',sysdate); <br />
<br />
 END; <br />
<br />
 /</code></div></div>
<br />
3) AFTER UPDATE, Statement Level: This trigger will insert a record into the table 'product_check' after a sql update statement is executed, at the statement level.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE or REPLACE TRIGGER After_Update_Stat_product <br />
<br />
 AFTER <br />
<br />
 UPDATE ON product <br />
<br />
 BEGIN <br />
<br />
 INSERT INTO product_check <br />
<br />
 Values('After update, statement level', sysdate); <br />
<br />
 End; <br />
<br />
 / <br />
<br />
4) AFTER UPDATE, Row Level: This trigger will insert a record into the table 'product_check' after each row is updated.<br />
<br />
 CREATE or REPLACE TRIGGER After_Update_Row_product <br />
<br />
 AFTER&nbsp;&nbsp;<br />
<br />
 insert On product <br />
<br />
 FOR EACH ROW <br />
<br />
 BEGIN <br />
<br />
 INSERT INTO product_check <br />
<br />
 Values('After update, Row level',sysdate); <br />
<br />
 END; <br />
<br />
 / <br />
<br />
Now lets execute a update statement on table product.<br />
<br />
 UPDATE PRODUCT SET unit_price = 800&nbsp;&nbsp;<br />
<br />
 WHERE product_id in (100,101); <br />
<br />
Lets check the data in 'product_check' table to see the order in which the trigger is fired.<br />
<br />
 SELECT * FROM product_check; <br />
<br />
Output:<br />
<br />
Mesage&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Current_Date<br />
<br />
------------------------------------------------------------<br />
<br />
Before update, statement level&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;26-Nov-2008<br />
Before update, row level&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;26-Nov-2008<br />
After update, Row level&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 26-Nov-2008<br />
Before update, row level&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;26-Nov-2008<br />
After update, Row level&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 26-Nov-2008<br />
After update, statement level&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;26-Nov-2008</code></div></div>
<br />
The above result shows 'before update' and 'after update' row level events have occured twice, since two records were updated. But 'before update' and 'after update' statement level events are fired only once per sql statement.<br />
<br />
The above rules apply similarly for INSERT and DELETE statements.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Types of PL/SQL Triggers]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2728</link>
			<pubDate>Sat, 14 Nov 2009 15:34:57 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2728</guid>
			<description><![CDATA[There are two types of triggers based on the which level it is triggered.<br />
<br />
1) <span style="font-weight: bold;">Row level trigger</span> - An event is triggered for each row upated, inserted or deleted.<br />
<br />
2) <span style="font-weight: bold;">Statement level trigger</span> - An event is triggered for each sql statement executed.]]></description>
			<content:encoded><![CDATA[There are two types of triggers based on the which level it is triggered.<br />
<br />
1) <span style="font-weight: bold;">Row level trigger</span> - An event is triggered for each row upated, inserted or deleted.<br />
<br />
2) <span style="font-weight: bold;">Statement level trigger</span> - An event is triggered for each sql statement executed.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[What is a PL/SQL Trigger]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2727</link>
			<pubDate>Sat, 14 Nov 2009 15:34:05 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2727</guid>
			<description><![CDATA[A trigger is a pl/sql block structure which is fired when a DML statements like Insert, Delete, Update is executed on a database table. A trigger is triggered automatically when an associated DML statement is executed.<br />
<br />
<span style="font-weight: bold;">Syntax of Triggers</span><br />
<br />
The Syntax for creating a trigger is:<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE [OR REPLACE &#93; TRIGGER trigger_name <br />
<br />
 {BEFORE | AFTER | INSTEAD OF } <br />
<br />
 {INSERT [OR&#93; | UPDATE [OR&#93; | DELETE} <br />
<br />
 [OF col_name&#93; <br />
<br />
 ON table_name <br />
<br />
 [REFERENCING OLD AS o NEW AS n&#93; <br />
<br />
 [FOR EACH ROW&#93; <br />
<br />
 WHEN (condition)&nbsp;&nbsp;<br />
<br />
 BEGIN <br />
<br />
&nbsp;&nbsp; --- sql statements&nbsp;&nbsp;<br />
<br />
 END;</code></div></div>
<br />
    * CREATE [OR REPLACE &#93; TRIGGER trigger_name - This clause creates a trigger with the given name or overwrites an existing trigger with the same name.<br />
    * {BEFORE | AFTER | INSTEAD OF } - This clause indicates at what time should the trigger get fired. i.e for example: before or after updating a table. INSTEAD OF is used to create a trigger on a view. before and after cannot be used to create a trigger on a view.<br />
    * {INSERT [OR&#93; | UPDATE [OR&#93; | DELETE} - This clause determines the triggering event. More than one triggering events can be used together separated by OR keyword. The trigger gets fired at all the specified triggering event.<br />
    * [OF col_name&#93; - This clause is used with update triggers. This clause is used when you want to trigger an event only when a specific column is updated.<br />
    * CREATE [OR REPLACE &#93; TRIGGER trigger_name - This clause creates a trigger with the given name or overwrites an existing trigger with the same name.<br />
    * [ON table_name&#93; - This clause identifies the name of the table or view to which the trigger is associated.<br />
    * [REFERENCING OLD AS o NEW AS n&#93; - This clause is used to reference the old and new values of the data being changed. By default, you reference the values as :old.column_name or :new.column_name. The reference names can also be changed from old (or new) to any other user-defined name. You cannot reference old values when inserting a record, or new values when deleting a record, because they do not exist.<br />
    * [FOR EACH ROW&#93; - This clause is used to determine whether a trigger must fire when each row gets affected ( i.e. a Row Level Trigger) or just once when the entire sql statement is executed(i.e.statement level Trigger).<br />
    * WHEN (condition) - This clause is valid only for row level triggers. The trigger is fired only for rows that satisfy the condition specified.<br />
<br />
For Example: The price of a product changes constantly. It is important to maintain the history of the prices of the products.<br />
<br />
We can create a trigger to update the 'product_price_history' table when the price of the product is updated in the 'product' table.<br />
<br />
1) Create the 'product' table and 'product_price_history' table<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE TABLE product_price_history <br />
<br />
(product_id number(5), <br />
<br />
product_name varchar2(32), <br />
<br />
supplier_name varchar2(32), <br />
<br />
unit_price number(7,2) ); <br />
<br />
<br />
CREATE TABLE product <br />
<br />
(product_id number(5), <br />
<br />
product_name varchar2(32), <br />
<br />
supplier_name varchar2(32), <br />
<br />
unit_price number(7,2) );</code></div></div>
<br />
2) Create the price_history_trigger and execute it.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE or REPLACE TRIGGER price_history_trigger <br />
<br />
BEFORE UPDATE OF unit_price <br />
<br />
ON product <br />
<br />
FOR EACH ROW <br />
<br />
BEGIN <br />
<br />
INSERT INTO product_price_history <br />
<br />
VALUES <br />
<br />
(:old.product_id, <br />
<br />
 :old.product_name, <br />
<br />
 :old.supplier_name, <br />
<br />
 :old.unit_price); <br />
<br />
END; <br />
<br />
/</code></div></div>
<br />
3) Lets update the price of a product.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>UPDATE PRODUCT SET unit_price = 800 WHERE product_id = 100</code></div></div>
<br />
Once the above update query is executed, the trigger fires and updates the 'product_price_history' table.<br />
<br />
4)If you ROLLBACK the transaction before committing to the database, the data inserted to the table is also rolled back.]]></description>
			<content:encoded><![CDATA[A trigger is a pl/sql block structure which is fired when a DML statements like Insert, Delete, Update is executed on a database table. A trigger is triggered automatically when an associated DML statement is executed.<br />
<br />
<span style="font-weight: bold;">Syntax of Triggers</span><br />
<br />
The Syntax for creating a trigger is:<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE [OR REPLACE ] TRIGGER trigger_name <br />
<br />
 {BEFORE | AFTER | INSTEAD OF } <br />
<br />
 {INSERT [OR] | UPDATE [OR] | DELETE} <br />
<br />
 [OF col_name] <br />
<br />
 ON table_name <br />
<br />
 [REFERENCING OLD AS o NEW AS n] <br />
<br />
 [FOR EACH ROW] <br />
<br />
 WHEN (condition)&nbsp;&nbsp;<br />
<br />
 BEGIN <br />
<br />
&nbsp;&nbsp; --- sql statements&nbsp;&nbsp;<br />
<br />
 END;</code></div></div>
<br />
    * CREATE [OR REPLACE ] TRIGGER trigger_name - This clause creates a trigger with the given name or overwrites an existing trigger with the same name.<br />
    * {BEFORE | AFTER | INSTEAD OF } - This clause indicates at what time should the trigger get fired. i.e for example: before or after updating a table. INSTEAD OF is used to create a trigger on a view. before and after cannot be used to create a trigger on a view.<br />
    * {INSERT [OR] | UPDATE [OR] | DELETE} - This clause determines the triggering event. More than one triggering events can be used together separated by OR keyword. The trigger gets fired at all the specified triggering event.<br />
    * [OF col_name] - This clause is used with update triggers. This clause is used when you want to trigger an event only when a specific column is updated.<br />
    * CREATE [OR REPLACE ] TRIGGER trigger_name - This clause creates a trigger with the given name or overwrites an existing trigger with the same name.<br />
    * [ON table_name] - This clause identifies the name of the table or view to which the trigger is associated.<br />
    * [REFERENCING OLD AS o NEW AS n] - This clause is used to reference the old and new values of the data being changed. By default, you reference the values as :old.column_name or :new.column_name. The reference names can also be changed from old (or new) to any other user-defined name. You cannot reference old values when inserting a record, or new values when deleting a record, because they do not exist.<br />
    * [FOR EACH ROW] - This clause is used to determine whether a trigger must fire when each row gets affected ( i.e. a Row Level Trigger) or just once when the entire sql statement is executed(i.e.statement level Trigger).<br />
    * WHEN (condition) - This clause is valid only for row level triggers. The trigger is fired only for rows that satisfy the condition specified.<br />
<br />
For Example: The price of a product changes constantly. It is important to maintain the history of the prices of the products.<br />
<br />
We can create a trigger to update the 'product_price_history' table when the price of the product is updated in the 'product' table.<br />
<br />
1) Create the 'product' table and 'product_price_history' table<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE TABLE product_price_history <br />
<br />
(product_id number(5), <br />
<br />
product_name varchar2(32), <br />
<br />
supplier_name varchar2(32), <br />
<br />
unit_price number(7,2) ); <br />
<br />
<br />
CREATE TABLE product <br />
<br />
(product_id number(5), <br />
<br />
product_name varchar2(32), <br />
<br />
supplier_name varchar2(32), <br />
<br />
unit_price number(7,2) );</code></div></div>
<br />
2) Create the price_history_trigger and execute it.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE or REPLACE TRIGGER price_history_trigger <br />
<br />
BEFORE UPDATE OF unit_price <br />
<br />
ON product <br />
<br />
FOR EACH ROW <br />
<br />
BEGIN <br />
<br />
INSERT INTO product_price_history <br />
<br />
VALUES <br />
<br />
(:old.product_id, <br />
<br />
 :old.product_name, <br />
<br />
 :old.supplier_name, <br />
<br />
 :old.unit_price); <br />
<br />
END; <br />
<br />
/</code></div></div>
<br />
3) Lets update the price of a product.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>UPDATE PRODUCT SET unit_price = 800 WHERE product_id = 100</code></div></div>
<br />
Once the above update query is executed, the trigger fires and updates the 'product_price_history' table.<br />
<br />
4)If you ROLLBACK the transaction before committing to the database, the data inserted to the table is also rolled back.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Exception Handling in PL/SQL]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2726</link>
			<pubDate>Sat, 14 Nov 2009 15:31:24 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2726</guid>
			<description><![CDATA[<html>
<body>
<br />
<h2 class="title">Exception Handling</h2> 	<br />
			<br />
			<p>In this section we will discuss about the following,<br />
			<br>1) What is Exception Handling.<br />
			<br>2) Structure of Exception Handling.<br />
			<br>3) Types of Exception Handling.<br />
			</p>
<br />
			<br />
<br />
<h3 class="subtitle">1) What is Exception Handling?</h3><br />
<br />
<p>PL/SQL provides a feature to handle the Exceptions which occur in a PL/SQL Block known as exception Handling.<br />
Using Exception Handling we can test the code and avoid it from exiting abruptly. When an exception occurs a messages which explains its cause is recieved. <br> PL/SQL Exception message consists of three parts. <br />
  <strong>1) Type of Exception<br />
  2) An Error Code<br />
  3) A message </strong><br />
<br />
  By Handling the exceptions we can ensure a PL/SQL block does not exit abruptly. </p>
  <br />
<br />
<h3 class="subtitle">2) Structure of Exception Handling.</h3><br />
<p>
The General Syntax for coding the exception section<br />
</p>
 <pre class="style1"> DECLARE</pre>
 <pre class="style1">   Declaration section </pre>
<br />
 <pre class="style1"> BEGIN </pre>
 <pre class="style1">   Exception section </pre>
 <pre class="style1"> EXCEPTION </pre>
 <pre class="style1"> WHEN ex_name1 THEN </pre>
 <pre class="style1">    -Error handling statements </pre>
<br />
 <pre class="style1"> WHEN ex_name2 THEN </pre>
 <pre class="style1">    -Error handling statements </pre>
 <pre class="style1"> WHEN Others THEN </pre>
 <pre class="style1">   -Error handling statements </pre>
 <pre class="style1">END; </pre>
<br />
<p>General PL/SQL statments can be used in the Exception Block.</p>
<p>
When an exception is raised, Oracle searches for an appropriate exception handler in the exception section. For example in the above example, if the error raised is 'ex_name1 ', then the error is handled according to the statements under it. Since, it is not possible to determine all the possible runtime errors during testing fo the code, the 'WHEN Others' exception is used to manage the exceptions that are not explicitly handled. Only one exception can be raised in a Block and the control does not return to the Execution Section after the error is handled. </p>
<p>
If there are nested PL/SQL blocks like this.<br />
</p>
<br />
<pre class="style1"> DELCARE</pre>
<pre class="style1">   Declaration section </pre>
<pre class="style1"> BEGIN</pre>
<br />
       <pre class="style4"> <spacer>   DECLARE</pre>
  <pre class="style4">   <spacer>   Declaration section </pre>
  <pre class="style4">  <spacer>  BEGIN </pre>
  <pre class="style4">    <spacer>  Execution section </pre>
  <pre class="style4">  <spacer>  EXCEPTION </pre>
<br />
  <pre class="style4">   <spacer>   Exception section </pre>
  <pre class="style4"> <spacer>   END; </pre>
<pre class="style1"> EXCEPTION</pre>
<pre class="style1">   Exception section </pre>
<pre class="style1"> END; </pre>
<br />
<br />
<p>In the above case, if the exception is raised in the inner block it should be handled in the exception block of the inner PL/SQL block else the control moves to the Exception block of the next upper PL/SQL Block. If none of the blocks handle the exception the program ends abruptly with an error.</p>
<br />
<br />
<br />
<h3 class="subtitle">3) Types of Exception.</h3><br />
<p>
	There are 3 types of Exceptions.<br />
<br />
	<br>a) Named System Exceptions <br />
	<br>b) Unnamed System Exceptions <br />
	<br>c) User-defined Exceptions<br />
</p>
<br />
<h3 class="subtitle">a) Named System Exceptions </h3><br />
<p>
System exceptions are automatically raised by Oracle, when a program violates a RDBMS rule. There are some system exceptions which are raised frequently, so they are pre-defined and given a name in Oracle which are known as Named System Exceptions.<br />
</p>
<br />
<p><strong>For example:</strong> NO_DATA_FOUND and ZERO_DIVIDE are called Named System exceptions. <br />
</p>
<br />
<p>Named system exceptions are: <br />
<br>1) Not Declared explicitly,<br />
<br>2) Raised implicitly when a predefined Oracle error occurs, <br />
<br>3) caught by referencing the standard name within an exception-handling routine.<br />
</p>
<br />
<table border="1" cellpadding="0" cellspacing="0" width="602">
                  <tbody><tr>
                    <td align="center" valign="top"><strong>Exception Name</strong></td>
                    <td align="center" valign="top"><strong>Reason</strong></td>
					<td align="center" valign="top"><strong>Error Number</strong></td>
                  </tr>
                  <tr>
                    <td valign="top"><p>CURSOR_ALREADY_OPEN</p></td>
<br />
                    <td valign="top"><p>When you open a cursor that is already open.</p></td>
					<td valign="top"><p>ORA-06511</p></td>
                  </tr>
                  <tr>
                    <td valign="top"><p>INVALID_CURSOR</p></td>
                    <td valign="top"><p>When you perform an invalid operation on a cursor like closing a cursor, fetch data from a cursor that is not opened.</p></td>
					<td valign="top"><p>ORA-01001</p></td>
<br />
                  </tr>
                  <tr>
                    <td valign="top"><p>NO_DATA_FOUND</p></td>
                    <td valign="top"><p>When a SELECT...INTO clause does not return any row from a table.</p></td>
					<td valign="top"><p>ORA-01403</p></td>
                  </tr>
                  <tr>
<br />
                    <td valign="top"><p>TOO_MANY_ROWS</p></td>
                    <td valign="top"><p>When you SELECT or fetch more than one row into a record or variable.</p></td>
					<td valign="top"><p>ORA-01422</p></td>
                  </tr>
				  <tr>
				    <td valign="top"><p>ZERO_DIVIDE</p></td>
                    <td valign="top"><p>When you attempt to divide a number by zero.</p></td>
<br />
					<td valign="top"><p>ORA-01476</p></td>
                  </tr>
                </tbody></table>
<br />
<br />
<br />
<p>
<strong>For Example: </strong>Suppose a NO_DATA_FOUND exception is raised in a proc, we can write a code to handle the exception as given below.<br />
</p>
<br />
<br />
  <pre class="style1">BEGIN </pre>
  <pre class="style1">  Execution section</pre>
  <pre class="style1">EXCEPTION </pre>
  <pre class="style1">WHEN NO_DATA_FOUND THEN </pre>
  <pre class="style1"> dbms_output.put_line ('A SELECT...INTO did not return any row.'); </pre>
  <pre class="style1"> END; </pre>
<br />
<br />
<br />
<br />
<h3 class="subtitle">b) Unnamed System Exceptions</h3><br />
<br />
<p>Those system exception for which oracle does not provide a name is known as unamed system exception.<br />
These exception do not occur frequently. These Exceptions have a code and an associated message. <br />
</p>
<br />
<p>There are two ways to handle unnamed sysyem exceptions:<br />
<br />
<br>1. By using the WHEN OTHERS exception handler, or <br />
<br>2. By associating the exception code to a name and using it as a named exception. <br />
</p>
<br />
<p>We can assign a name to unnamed system exceptions using a <strong>Pragma</strong> called <strong>EXCEPTION_INIT.</strong><br />
<br />
<br />
<strong>EXCEPTION_INIT</strong>  will associate a predefined Oracle error number to a programmer_defined exception name. <br />
 </p>
 <p>
Steps to be followed to use unnamed system exceptions are <br />
<br />
<br>•  They are raised implicitly.<br />
<br>•  If they are not handled in WHEN Others they must be handled explicity.<br />
<br>•  To handle the exception explicity, they must be declared using Pragma EXCEPTION_INIT as given above and handled referecing the user-defined exception name in the exception section.<br />
</p>
<br />
<p>The general syntax to declare unnamed system exception using EXCEPTION_INIT is:<br />
<br />
</p><pre class="style1">DECLARE </pre>
<pre class="style1">   exception_name EXCEPTION; </pre>
<br />
<pre class="style1">   PRAGMA </pre>
<pre class="style1">   EXCEPTION_INIT (exception_name, Err_code); </pre>
<pre class="style1">BEGIN </pre>
<pre class="style1">Execution section</pre>
<pre class="style1">EXCEPTION</pre>
<pre class="style1">  WHEN exception_name THEN</pre>
<pre class="style1">     handle the exception</pre>
<br />
<pre class="style1">END;</pre>
<br />
<br />
<p>
<strong>For Example:</strong> Lets consider the product table and order_items table from sql joins.<br />
</p>
<br />
<p>Here product_id is a primary key in product table and a foreign key in order_items table.<br />
<br />
If we try to delete a product_id from the product table when it has child records in order_id table an exception will be thrown with oracle code number -2292.<br />
<br />
We can provide a name to this exception and handle it in the exception section as given below.<br />
</p>
<br />
<pre class="style1"> DECLARE </pre>
<br />
<pre class="style1">  Child_rec_exception EXCEPTION; </pre>
<pre class="style1">  PRAGMA </pre>
<pre class="style1">   EXCEPTION_INIT (Child_rec_exception, -2292); </pre>
<pre class="style1">BEGIN </pre>
<pre class="style1">  Delete FROM product where product_id= 104; </pre>
<pre class="style1">EXCEPTION </pre>
<pre class="style1">   WHEN Child_rec_exception </pre>
<br />
<pre class="style1">   THEN Dbms_output.put_line('Child records are present for this product_id.'); </pre>
<pre class="style1">END; </pre>
<pre class="style1">/ </pre>
  <br />
  <br />
<br />
<h3 class="subtitle">c) User-defined Exceptions</h3><br />
<p>
Apart from sytem exceptions we can explicity define exceptions based on business rules. These are known as user-defined exceptions. <br />
</p>
<p>Steps to be followed to use user-defined exceptions:<br />
 <br />
<br>• They should be explicitly declared in the declaration section. <br />
<br>• They should be explicitly raised in the Execution Section.<br />
<br>• They should be handled by referencing the user-defined exception name in the exception section. <br />
</p>
<br />
<p><strong>For Example:</strong> Lets consider the product table and order_items table from sql joins to explain user-defined exception.<br />
<br />
Lets create a business rule that if the total no of units of any particular product sold is more than 20, then it is a huge quantity and a special discount should be provided. <br />
</p>
<br />
<pre class="style1">DECLARE </pre>
<pre class="style1">  huge_quantity EXCEPTION; </pre>
<br />
<pre class="style1">  CURSOR product_quantity is </pre>
<pre class="style1">  SELECT p.product_name as name, sum(o.total_units) as units</pre>
<br />
<pre class="style1">  FROM order_tems o, product p</pre>
<pre class="style1">  WHERE o.product_id = p.product_id; </pre>
<pre class="style1">  quantity order_tems.total_units%type; </pre>
<pre class="style1">  up_limit CONSTANT order_tems.total_units%type := 20; </pre>
<pre class="style1">  message VARCHAR2(50); </pre>
<pre class="style1">BEGIN </pre>
<br />
<pre class="style1">  FOR product_rec in product_quantity LOOP </pre>
<pre class="style1">    quantity := product_rec.units;</pre>
<pre class="style1">     IF quantity &gt; up_limit THEN </pre>
<pre class="style1">      message := 'The number of units of product ' || product_rec.name ||  </pre>
<pre class="style1">                 ' is more than 20. Special discounts should be provided. </pre>
<br />
<pre class="style1">		 Rest of the records are skipped. '</pre>
<pre class="style1">     RAISE huge_quantity; </pre>
<pre class="style1">     ELSIF quantity &lt; up_limit THEN </pre>
<pre class="style1">      v_message:= 'The number of unit is below the discount limit.'; </pre>
<pre class="style1">     END IF; </pre>
<br />
<pre class="style1">     dbms_output.put_line (message); </pre>
<pre class="style1">  END LOOP; </pre>
<pre class="style1"> EXCEPTION </pre>
<pre class="style1">   WHEN huge_quantity THEN </pre>
<pre class="style1">     dbms_output.put_line (message); </pre>
<pre class="style1"> END; </pre>
<br />
<pre class="style1">/ </pre>
 <br />
<br />
<h3 class="subtitle">RAISE_APPLICATION_ERROR ( ) </h3><br />
<p>
<strong>RAISE_APPLICATION_ERROR</strong> is a built-in procedure in oracle which is used to display the user-defined error messages along with the error number whose range is in between -20000 and -20999. <br />
</p>
<p>Whenever a message is displayed using RAISE_APPLICATION_ERROR, all previous transactions which are not committed  within the PL/SQL Block are rolled back automatically (i.e. change due to INSERT, UPDATE, or DELETE statements). <br />
</p>
<p>RAISE_APPLICATION_ERROR raises an exception but does not handle it.<br />
</p>
<p>RAISE_APPLICATION_ERROR is used for the following reasons,<br />
<br />
<br>a) to create a unique id for an user-defined exception.<br />
<br>b) to make the user-defined exception look like an Oracle error. <br />
<br />
</p>
<p>The General Syntax to use this procedure is: <br />
<br />
</p><pre class="style1"><strong>RAISE_APPLICATION_ERROR (error_number, error_message); </strong></pre>
<br />
<br>• The Error number must be between -20000 and -20999 <br />
<br>• The Error_message is the message you want to display when the error occurs. <br />
<br />
<br />
<p>Steps to be folowed to use RAISE_APPLICATION_ERROR procedure:<br />
<br />
<br>1. Declare a user-defined exception in the declaration section.<br />
<br>2. Raise the user-defined exception based on a specific business rule in the execution section.<br />
<br>3. Finally, catch the exception and link the exception to a user-defined error number in RAISE_APPLICATION_ERROR.<br />
</p>
<br />
<p>Using the above example we can display a error message using RAISE_APPLICATION_ERROR.<br />
</p>
<br />
<pre class="style1">DECLARE</pre>
<br />
<pre class="style1">  huge_quantity EXCEPTION; </pre>
<pre class="style1">  CURSOR product_quantity is </pre>
<pre class="style1">  SELECT p.product_name as name, sum(o.total_units) as units</pre>
<pre class="style1">  FROM order_tems o, product p</pre>
<pre class="style1">  WHERE o.product_id = p.product_id; </pre>
<pre class="style1">  quantity order_tems.total_units%type; </pre>
<br />
<pre class="style1">  up_limit CONSTANT order_tems.total_units%type := 20; </pre>
<pre class="style1">  message VARCHAR2(50); </pre>
<pre class="style1">BEGIN </pre>
<pre class="style1">  FOR product_rec in product_quantity LOOP </pre>
<pre class="style1">    quantity := product_rec.units;</pre>
<pre class="style1">     IF quantity &gt; up_limit THEN </pre>
<br />
<pre class="style1">        RAISE huge_quantity; </pre>
<pre class="style1">     ELSIF quantity &lt; up_limit THEN </pre>
<pre class="style1">      v_message:= 'The number of unit is below the discount limit.'; </pre>
<pre class="style1">     END IF; </pre>
<pre class="style1">     Dbms_output.put_line (message); </pre>
<br />
<pre class="style1">  END LOOP; </pre>
<pre class="style1"> EXCEPTION </pre>
<pre class="style1">   WHEN huge_quantity THEN </pre>
<pre class="style1">	raise_application_error(-2100, 'The number of unit is above the discount limit.');</pre>
<pre class="style1"> END; </pre>
<pre class="style1">/ </pre>
  <br />
<br />
</body>
</html>]]></description>
			<content:encoded><![CDATA[<html>
<body>
<br />
<h2 class="title">Exception Handling</h2> 	<br />
			<br />
			<p>In this section we will discuss about the following,<br />
			<br>1) What is Exception Handling.<br />
			<br>2) Structure of Exception Handling.<br />
			<br>3) Types of Exception Handling.<br />
			</p>
<br />
			<br />
<br />
<h3 class="subtitle">1) What is Exception Handling?</h3><br />
<br />
<p>PL/SQL provides a feature to handle the Exceptions which occur in a PL/SQL Block known as exception Handling.<br />
Using Exception Handling we can test the code and avoid it from exiting abruptly. When an exception occurs a messages which explains its cause is recieved. <br> PL/SQL Exception message consists of three parts. <br />
  <strong>1) Type of Exception<br />
  2) An Error Code<br />
  3) A message </strong><br />
<br />
  By Handling the exceptions we can ensure a PL/SQL block does not exit abruptly. </p>
  <br />
<br />
<h3 class="subtitle">2) Structure of Exception Handling.</h3><br />
<p>
The General Syntax for coding the exception section<br />
</p>
 <pre class="style1"> DECLARE</pre>
 <pre class="style1">   Declaration section </pre>
<br />
 <pre class="style1"> BEGIN </pre>
 <pre class="style1">   Exception section </pre>
 <pre class="style1"> EXCEPTION </pre>
 <pre class="style1"> WHEN ex_name1 THEN </pre>
 <pre class="style1">    -Error handling statements </pre>
<br />
 <pre class="style1"> WHEN ex_name2 THEN </pre>
 <pre class="style1">    -Error handling statements </pre>
 <pre class="style1"> WHEN Others THEN </pre>
 <pre class="style1">   -Error handling statements </pre>
 <pre class="style1">END; </pre>
<br />
<p>General PL/SQL statments can be used in the Exception Block.</p>
<p>
When an exception is raised, Oracle searches for an appropriate exception handler in the exception section. For example in the above example, if the error raised is 'ex_name1 ', then the error is handled according to the statements under it. Since, it is not possible to determine all the possible runtime errors during testing fo the code, the 'WHEN Others' exception is used to manage the exceptions that are not explicitly handled. Only one exception can be raised in a Block and the control does not return to the Execution Section after the error is handled. </p>
<p>
If there are nested PL/SQL blocks like this.<br />
</p>
<br />
<pre class="style1"> DELCARE</pre>
<pre class="style1">   Declaration section </pre>
<pre class="style1"> BEGIN</pre>
<br />
       <pre class="style4"> <spacer>   DECLARE</pre>
  <pre class="style4">   <spacer>   Declaration section </pre>
  <pre class="style4">  <spacer>  BEGIN </pre>
  <pre class="style4">    <spacer>  Execution section </pre>
  <pre class="style4">  <spacer>  EXCEPTION </pre>
<br />
  <pre class="style4">   <spacer>   Exception section </pre>
  <pre class="style4"> <spacer>   END; </pre>
<pre class="style1"> EXCEPTION</pre>
<pre class="style1">   Exception section </pre>
<pre class="style1"> END; </pre>
<br />
<br />
<p>In the above case, if the exception is raised in the inner block it should be handled in the exception block of the inner PL/SQL block else the control moves to the Exception block of the next upper PL/SQL Block. If none of the blocks handle the exception the program ends abruptly with an error.</p>
<br />
<br />
<br />
<h3 class="subtitle">3) Types of Exception.</h3><br />
<p>
	There are 3 types of Exceptions.<br />
<br />
	<br>a) Named System Exceptions <br />
	<br>b) Unnamed System Exceptions <br />
	<br>c) User-defined Exceptions<br />
</p>
<br />
<h3 class="subtitle">a) Named System Exceptions </h3><br />
<p>
System exceptions are automatically raised by Oracle, when a program violates a RDBMS rule. There are some system exceptions which are raised frequently, so they are pre-defined and given a name in Oracle which are known as Named System Exceptions.<br />
</p>
<br />
<p><strong>For example:</strong> NO_DATA_FOUND and ZERO_DIVIDE are called Named System exceptions. <br />
</p>
<br />
<p>Named system exceptions are: <br />
<br>1) Not Declared explicitly,<br />
<br>2) Raised implicitly when a predefined Oracle error occurs, <br />
<br>3) caught by referencing the standard name within an exception-handling routine.<br />
</p>
<br />
<table border="1" cellpadding="0" cellspacing="0" width="602">
                  <tbody><tr>
                    <td align="center" valign="top"><strong>Exception Name</strong></td>
                    <td align="center" valign="top"><strong>Reason</strong></td>
					<td align="center" valign="top"><strong>Error Number</strong></td>
                  </tr>
                  <tr>
                    <td valign="top"><p>CURSOR_ALREADY_OPEN</p></td>
<br />
                    <td valign="top"><p>When you open a cursor that is already open.</p></td>
					<td valign="top"><p>ORA-06511</p></td>
                  </tr>
                  <tr>
                    <td valign="top"><p>INVALID_CURSOR</p></td>
                    <td valign="top"><p>When you perform an invalid operation on a cursor like closing a cursor, fetch data from a cursor that is not opened.</p></td>
					<td valign="top"><p>ORA-01001</p></td>
<br />
                  </tr>
                  <tr>
                    <td valign="top"><p>NO_DATA_FOUND</p></td>
                    <td valign="top"><p>When a SELECT...INTO clause does not return any row from a table.</p></td>
					<td valign="top"><p>ORA-01403</p></td>
                  </tr>
                  <tr>
<br />
                    <td valign="top"><p>TOO_MANY_ROWS</p></td>
                    <td valign="top"><p>When you SELECT or fetch more than one row into a record or variable.</p></td>
					<td valign="top"><p>ORA-01422</p></td>
                  </tr>
				  <tr>
				    <td valign="top"><p>ZERO_DIVIDE</p></td>
                    <td valign="top"><p>When you attempt to divide a number by zero.</p></td>
<br />
					<td valign="top"><p>ORA-01476</p></td>
                  </tr>
                </tbody></table>
<br />
<br />
<br />
<p>
<strong>For Example: </strong>Suppose a NO_DATA_FOUND exception is raised in a proc, we can write a code to handle the exception as given below.<br />
</p>
<br />
<br />
  <pre class="style1">BEGIN </pre>
  <pre class="style1">  Execution section</pre>
  <pre class="style1">EXCEPTION </pre>
  <pre class="style1">WHEN NO_DATA_FOUND THEN </pre>
  <pre class="style1"> dbms_output.put_line ('A SELECT...INTO did not return any row.'); </pre>
  <pre class="style1"> END; </pre>
<br />
<br />
<br />
<br />
<h3 class="subtitle">b) Unnamed System Exceptions</h3><br />
<br />
<p>Those system exception for which oracle does not provide a name is known as unamed system exception.<br />
These exception do not occur frequently. These Exceptions have a code and an associated message. <br />
</p>
<br />
<p>There are two ways to handle unnamed sysyem exceptions:<br />
<br />
<br>1. By using the WHEN OTHERS exception handler, or <br />
<br>2. By associating the exception code to a name and using it as a named exception. <br />
</p>
<br />
<p>We can assign a name to unnamed system exceptions using a <strong>Pragma</strong> called <strong>EXCEPTION_INIT.</strong><br />
<br />
<br />
<strong>EXCEPTION_INIT</strong>  will associate a predefined Oracle error number to a programmer_defined exception name. <br />
 </p>
 <p>
Steps to be followed to use unnamed system exceptions are <br />
<br />
<br>•  They are raised implicitly.<br />
<br>•  If they are not handled in WHEN Others they must be handled explicity.<br />
<br>•  To handle the exception explicity, they must be declared using Pragma EXCEPTION_INIT as given above and handled referecing the user-defined exception name in the exception section.<br />
</p>
<br />
<p>The general syntax to declare unnamed system exception using EXCEPTION_INIT is:<br />
<br />
</p><pre class="style1">DECLARE </pre>
<pre class="style1">   exception_name EXCEPTION; </pre>
<br />
<pre class="style1">   PRAGMA </pre>
<pre class="style1">   EXCEPTION_INIT (exception_name, Err_code); </pre>
<pre class="style1">BEGIN </pre>
<pre class="style1">Execution section</pre>
<pre class="style1">EXCEPTION</pre>
<pre class="style1">  WHEN exception_name THEN</pre>
<pre class="style1">     handle the exception</pre>
<br />
<pre class="style1">END;</pre>
<br />
<br />
<p>
<strong>For Example:</strong> Lets consider the product table and order_items table from sql joins.<br />
</p>
<br />
<p>Here product_id is a primary key in product table and a foreign key in order_items table.<br />
<br />
If we try to delete a product_id from the product table when it has child records in order_id table an exception will be thrown with oracle code number -2292.<br />
<br />
We can provide a name to this exception and handle it in the exception section as given below.<br />
</p>
<br />
<pre class="style1"> DECLARE </pre>
<br />
<pre class="style1">  Child_rec_exception EXCEPTION; </pre>
<pre class="style1">  PRAGMA </pre>
<pre class="style1">   EXCEPTION_INIT (Child_rec_exception, -2292); </pre>
<pre class="style1">BEGIN </pre>
<pre class="style1">  Delete FROM product where product_id= 104; </pre>
<pre class="style1">EXCEPTION </pre>
<pre class="style1">   WHEN Child_rec_exception </pre>
<br />
<pre class="style1">   THEN Dbms_output.put_line('Child records are present for this product_id.'); </pre>
<pre class="style1">END; </pre>
<pre class="style1">/ </pre>
  <br />
  <br />
<br />
<h3 class="subtitle">c) User-defined Exceptions</h3><br />
<p>
Apart from sytem exceptions we can explicity define exceptions based on business rules. These are known as user-defined exceptions. <br />
</p>
<p>Steps to be followed to use user-defined exceptions:<br />
 <br />
<br>• They should be explicitly declared in the declaration section. <br />
<br>• They should be explicitly raised in the Execution Section.<br />
<br>• They should be handled by referencing the user-defined exception name in the exception section. <br />
</p>
<br />
<p><strong>For Example:</strong> Lets consider the product table and order_items table from sql joins to explain user-defined exception.<br />
<br />
Lets create a business rule that if the total no of units of any particular product sold is more than 20, then it is a huge quantity and a special discount should be provided. <br />
</p>
<br />
<pre class="style1">DECLARE </pre>
<pre class="style1">  huge_quantity EXCEPTION; </pre>
<br />
<pre class="style1">  CURSOR product_quantity is </pre>
<pre class="style1">  SELECT p.product_name as name, sum(o.total_units) as units</pre>
<br />
<pre class="style1">  FROM order_tems o, product p</pre>
<pre class="style1">  WHERE o.product_id = p.product_id; </pre>
<pre class="style1">  quantity order_tems.total_units%type; </pre>
<pre class="style1">  up_limit CONSTANT order_tems.total_units%type := 20; </pre>
<pre class="style1">  message VARCHAR2(50); </pre>
<pre class="style1">BEGIN </pre>
<br />
<pre class="style1">  FOR product_rec in product_quantity LOOP </pre>
<pre class="style1">    quantity := product_rec.units;</pre>
<pre class="style1">     IF quantity &gt; up_limit THEN </pre>
<pre class="style1">      message := 'The number of units of product ' || product_rec.name ||  </pre>
<pre class="style1">                 ' is more than 20. Special discounts should be provided. </pre>
<br />
<pre class="style1">		 Rest of the records are skipped. '</pre>
<pre class="style1">     RAISE huge_quantity; </pre>
<pre class="style1">     ELSIF quantity &lt; up_limit THEN </pre>
<pre class="style1">      v_message:= 'The number of unit is below the discount limit.'; </pre>
<pre class="style1">     END IF; </pre>
<br />
<pre class="style1">     dbms_output.put_line (message); </pre>
<pre class="style1">  END LOOP; </pre>
<pre class="style1"> EXCEPTION </pre>
<pre class="style1">   WHEN huge_quantity THEN </pre>
<pre class="style1">     dbms_output.put_line (message); </pre>
<pre class="style1"> END; </pre>
<br />
<pre class="style1">/ </pre>
 <br />
<br />
<h3 class="subtitle">RAISE_APPLICATION_ERROR ( ) </h3><br />
<p>
<strong>RAISE_APPLICATION_ERROR</strong> is a built-in procedure in oracle which is used to display the user-defined error messages along with the error number whose range is in between -20000 and -20999. <br />
</p>
<p>Whenever a message is displayed using RAISE_APPLICATION_ERROR, all previous transactions which are not committed  within the PL/SQL Block are rolled back automatically (i.e. change due to INSERT, UPDATE, or DELETE statements). <br />
</p>
<p>RAISE_APPLICATION_ERROR raises an exception but does not handle it.<br />
</p>
<p>RAISE_APPLICATION_ERROR is used for the following reasons,<br />
<br />
<br>a) to create a unique id for an user-defined exception.<br />
<br>b) to make the user-defined exception look like an Oracle error. <br />
<br />
</p>
<p>The General Syntax to use this procedure is: <br />
<br />
</p><pre class="style1"><strong>RAISE_APPLICATION_ERROR (error_number, error_message); </strong></pre>
<br />
<br>• The Error number must be between -20000 and -20999 <br />
<br>• The Error_message is the message you want to display when the error occurs. <br />
<br />
<br />
<p>Steps to be folowed to use RAISE_APPLICATION_ERROR procedure:<br />
<br />
<br>1. Declare a user-defined exception in the declaration section.<br />
<br>2. Raise the user-defined exception based on a specific business rule in the execution section.<br />
<br>3. Finally, catch the exception and link the exception to a user-defined error number in RAISE_APPLICATION_ERROR.<br />
</p>
<br />
<p>Using the above example we can display a error message using RAISE_APPLICATION_ERROR.<br />
</p>
<br />
<pre class="style1">DECLARE</pre>
<br />
<pre class="style1">  huge_quantity EXCEPTION; </pre>
<pre class="style1">  CURSOR product_quantity is </pre>
<pre class="style1">  SELECT p.product_name as name, sum(o.total_units) as units</pre>
<pre class="style1">  FROM order_tems o, product p</pre>
<pre class="style1">  WHERE o.product_id = p.product_id; </pre>
<pre class="style1">  quantity order_tems.total_units%type; </pre>
<br />
<pre class="style1">  up_limit CONSTANT order_tems.total_units%type := 20; </pre>
<pre class="style1">  message VARCHAR2(50); </pre>
<pre class="style1">BEGIN </pre>
<pre class="style1">  FOR product_rec in product_quantity LOOP </pre>
<pre class="style1">    quantity := product_rec.units;</pre>
<pre class="style1">     IF quantity &gt; up_limit THEN </pre>
<br />
<pre class="style1">        RAISE huge_quantity; </pre>
<pre class="style1">     ELSIF quantity &lt; up_limit THEN </pre>
<pre class="style1">      v_message:= 'The number of unit is below the discount limit.'; </pre>
<pre class="style1">     END IF; </pre>
<pre class="style1">     Dbms_output.put_line (message); </pre>
<br />
<pre class="style1">  END LOOP; </pre>
<pre class="style1"> EXCEPTION </pre>
<pre class="style1">   WHEN huge_quantity THEN </pre>
<pre class="style1">	raise_application_error(-2100, 'The number of unit is above the discount limit.');</pre>
<pre class="style1"> END; </pre>
<pre class="style1">/ </pre>
  <br />
<br />
</body>
</html>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Parameters in Procedure and Functions]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2725</link>
			<pubDate>Sat, 14 Nov 2009 15:29:52 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2725</guid>
			<description><![CDATA[<span style="font-weight: bold;">How to pass parameters to Procedures and Functions in PL/SQL</span> <br />
<br />
In PL/SQL, we can pass parameters to procedures and functions in three ways.<br />
<br />
1) <span style="font-weight: bold;">IN type parameter</span>: These types of parameters are used to send values to stored procedures.<br />
2) <span style="font-weight: bold;">OUT type parameter</span>: These types of parameters are used to get values from stored procedures. This is similar to a return type in functions.<br />
3) <span style="font-weight: bold;">IN OUT parameter</span>: These types of parameters are used to send values and get values from stored procedures.<br />
<span style="font-weight: bold;"><br />
NOTE</span>: If a parameter is not explicitly defined a parameter type, then by default it is an IN type parameter.<br />
<span style="font-weight: bold;"><br />
1) IN parameter:</span><br />
<br />
This is similar to passing parameters in programming languages. We can pass values to the stored procedure through these parameters or variables. This type of parameter is a read only parameter. We can assign the value of IN type parameter to a variable or use it in a query, but we cannot change its value inside the procedure.<br />
<span style="font-weight: bold;"><br />
The General syntax to pass a IN parameter is</span><br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE [OR REPLACE&#93; PROCEDURE procedure_name ( <br />
<br />
&nbsp;&nbsp;param_name1 IN datatype, param_name12 IN datatype ... )</code></div></div>
<br />
    * param_name1, param_name2... are unique parameter names.<br />
    * datatype - defines the datatype of the variable.<br />
    * IN - is optional, by default it is a IN type parameter.<br />
<br />
<br />
<span style="font-weight: bold;">2) OUT Parameter:</span><br />
<br />
The OUT parameters are used to send the OUTPUT from a procedure or a function. This is a write-only parameter i.e, we cannot pass values to OUT paramters while executing the stored procedure, but we can assign values to OUT parameter inside the stored procedure and the calling program can recieve this output value.<br />
<br />
<span style="font-weight: bold;">The General syntax to create an OUT parameter is</span><br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE [OR REPLACE&#93; PROCEDURE proc2 (param_name OUT datatype)</code></div></div>
<br />
The parameter should be explicity declared as OUT parameter.<br />
<br />
<span style="font-weight: bold;">3) IN OUT Parameter:</span><br />
<br />
The IN OUT parameter allows us to pass values into a procedure and get output values from the procedure. This parameter is used if the value of the IN parameter can be changed in the calling program.<br />
<br />
By using IN OUT parameter we can pass values into a parameter and return a value to the calling program using the same parameter. But this is possible only if the value passed to the procedure and output value have a same datatype. This parameter is used if the value of the parameter will be changed in the procedure.<br />
<br />
<span style="font-weight: bold;">The General syntax to create an IN OUT parameter is</span><br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE [OR REPLACE&#93; PROCEDURE proc3 (param_name IN OUT datatype)</code></div></div>
<br />
<br />
The below examples show how to create stored procedures using the above three types of parameters.<br />
<span style="font-weight: bold;"><br />
Example1:<br />
<br />
Using IN and OUT parameter:</span><br />
<br />
Let’s create a procedure which gets the name of the employee when the employee id is passed.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>1&gt; CREATE OR REPLACE PROCEDURE emp_name (id IN NUMBER, emp_name OUT NUMBER)<br />
<br />
2&gt; IS<br />
<br />
3&gt; BEGIN<br />
<br />
4&gt;&nbsp;&nbsp;&nbsp;&nbsp;SELECT first_name INTO emp_name<br />
<br />
5&gt;&nbsp;&nbsp;&nbsp;&nbsp;FROM emp_tbl WHERE empID = id;<br />
<br />
6&gt; END;<br />
<br />
7&gt; /</code></div></div>
We can call the procedure ‘emp_name’ in this way from a PL/SQL Block.<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>1&gt; DECLARE<br />
<br />
2&gt;&nbsp;&nbsp;empName varchar(20);<br />
<br />
3&gt;&nbsp;&nbsp;CURSOR id_cur SELECT id FROM emp_ids;<br />
<br />
4&gt; BEGIN<br />
<br />
5&gt; FOR emp_rec in id_cur<br />
<br />
6&gt; LOOP<br />
<br />
7&gt;&nbsp;&nbsp; emp_name(emp_rec.id, empName);<br />
<br />
8&gt;&nbsp;&nbsp; dbms_output.putline('The employee ' || empName || ' has id ' || emp-rec.id);<br />
<br />
9&gt; END LOOP;<br />
<br />
10&gt; END;<br />
<br />
11&gt; /</code></div></div>
<br />
In the above PL/SQL Block<br />
In line no 3; we are creating a cursor ‘id_cur’ which contains the employee id.<br />
In line no 7; we are calling the procedure ‘emp_name’, we are passing the ‘id’ as IN parameter and ‘empName’ as OUT parameter.<br />
In line no 8; we are displaying the id and the employee name which we got from the procedure ‘emp_name’.<br />
<br />
<span style="font-weight: bold;">Example 2:</span><br />
<span style="font-weight: bold;"><br />
Using IN OUT parameter in procedures:</span><br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>1&gt; CREATE OR REPLACE PROCEDURE emp_salary_increase<br />
<br />
2&gt; (emp_id IN emptbl.empID%type, salary_inc IN OUT emptbl.salary%type) <br />
<br />
3&gt; IS <br />
<br />
4&gt;&nbsp;&nbsp;&nbsp;&nbsp;tmp_sal number; <br />
<br />
5&gt; BEGIN <br />
<br />
6&gt;&nbsp;&nbsp;&nbsp;&nbsp;SELECT salary <br />
<br />
7&gt;&nbsp;&nbsp;&nbsp;&nbsp;INTO tmp_sal <br />
<br />
8&gt;&nbsp;&nbsp;&nbsp;&nbsp;FROM emp_tbl<br />
<br />
9&gt;&nbsp;&nbsp;&nbsp;&nbsp;WHERE empID = emp_id; <br />
<br />
10&gt;&nbsp;&nbsp; IF tmp_sal between 10000 and 20000 THEN <br />
<br />
11&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;salary_inout := tmp_sal * 1.2; <br />
<br />
12&gt;&nbsp;&nbsp; ELSIF tmp_sal between 20000 and 30000 THEN <br />
<br />
13&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;salary_inout := tmp_sal * 1.3; <br />
<br />
14&gt;&nbsp;&nbsp; ELSIF tmp_sal &gt; 30000 THEN <br />
<br />
15&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;salary_inout := tmp_sal * 1.4; <br />
<br />
16&gt;&nbsp;&nbsp; END IF; <br />
<br />
17&gt; END;<br />
<br />
18&gt; /</code></div></div>
<br />
The below PL/SQL block shows how to execute the above 'emp_salary_increase' procedure.<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>1&gt; DECLARE <br />
<br />
2&gt;&nbsp;&nbsp;&nbsp;&nbsp;CURSOR updated_sal is <br />
<br />
3&gt;&nbsp;&nbsp;&nbsp;&nbsp;SELECT empID,salary <br />
<br />
4&gt;&nbsp;&nbsp;&nbsp;&nbsp;FROM emp_tbl; <br />
<br />
5&gt;&nbsp;&nbsp;&nbsp;&nbsp;pre_sal number; <br />
<br />
6&gt; BEGIN <br />
<br />
7&gt;&nbsp;&nbsp; FOR emp_rec IN updated_sal LOOP <br />
<br />
8&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pre_sal := emp_rec.salary; <br />
<br />
9&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emp_salary_increase(emp_rec.empID, emp_rec.salary); <br />
<br />
10&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbms_output.put_line('The salary of ' || emp_rec.empID || <br />
<br />
11&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' increased from '|| pre_sal || ' to '||emp_rec.salary);<br />
<br />
12&gt;&nbsp;&nbsp; END LOOP; <br />
<br />
13&gt; END;<br />
<br />
14&gt; /</code></div></div>
]]></description>
			<content:encoded><![CDATA[<span style="font-weight: bold;">How to pass parameters to Procedures and Functions in PL/SQL</span> <br />
<br />
In PL/SQL, we can pass parameters to procedures and functions in three ways.<br />
<br />
1) <span style="font-weight: bold;">IN type parameter</span>: These types of parameters are used to send values to stored procedures.<br />
2) <span style="font-weight: bold;">OUT type parameter</span>: These types of parameters are used to get values from stored procedures. This is similar to a return type in functions.<br />
3) <span style="font-weight: bold;">IN OUT parameter</span>: These types of parameters are used to send values and get values from stored procedures.<br />
<span style="font-weight: bold;"><br />
NOTE</span>: If a parameter is not explicitly defined a parameter type, then by default it is an IN type parameter.<br />
<span style="font-weight: bold;"><br />
1) IN parameter:</span><br />
<br />
This is similar to passing parameters in programming languages. We can pass values to the stored procedure through these parameters or variables. This type of parameter is a read only parameter. We can assign the value of IN type parameter to a variable or use it in a query, but we cannot change its value inside the procedure.<br />
<span style="font-weight: bold;"><br />
The General syntax to pass a IN parameter is</span><br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE [OR REPLACE] PROCEDURE procedure_name ( <br />
<br />
&nbsp;&nbsp;param_name1 IN datatype, param_name12 IN datatype ... )</code></div></div>
<br />
    * param_name1, param_name2... are unique parameter names.<br />
    * datatype - defines the datatype of the variable.<br />
    * IN - is optional, by default it is a IN type parameter.<br />
<br />
<br />
<span style="font-weight: bold;">2) OUT Parameter:</span><br />
<br />
The OUT parameters are used to send the OUTPUT from a procedure or a function. This is a write-only parameter i.e, we cannot pass values to OUT paramters while executing the stored procedure, but we can assign values to OUT parameter inside the stored procedure and the calling program can recieve this output value.<br />
<br />
<span style="font-weight: bold;">The General syntax to create an OUT parameter is</span><br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE [OR REPLACE] PROCEDURE proc2 (param_name OUT datatype)</code></div></div>
<br />
The parameter should be explicity declared as OUT parameter.<br />
<br />
<span style="font-weight: bold;">3) IN OUT Parameter:</span><br />
<br />
The IN OUT parameter allows us to pass values into a procedure and get output values from the procedure. This parameter is used if the value of the IN parameter can be changed in the calling program.<br />
<br />
By using IN OUT parameter we can pass values into a parameter and return a value to the calling program using the same parameter. But this is possible only if the value passed to the procedure and output value have a same datatype. This parameter is used if the value of the parameter will be changed in the procedure.<br />
<br />
<span style="font-weight: bold;">The General syntax to create an IN OUT parameter is</span><br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE [OR REPLACE] PROCEDURE proc3 (param_name IN OUT datatype)</code></div></div>
<br />
<br />
The below examples show how to create stored procedures using the above three types of parameters.<br />
<span style="font-weight: bold;"><br />
Example1:<br />
<br />
Using IN and OUT parameter:</span><br />
<br />
Let’s create a procedure which gets the name of the employee when the employee id is passed.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>1&gt; CREATE OR REPLACE PROCEDURE emp_name (id IN NUMBER, emp_name OUT NUMBER)<br />
<br />
2&gt; IS<br />
<br />
3&gt; BEGIN<br />
<br />
4&gt;&nbsp;&nbsp;&nbsp;&nbsp;SELECT first_name INTO emp_name<br />
<br />
5&gt;&nbsp;&nbsp;&nbsp;&nbsp;FROM emp_tbl WHERE empID = id;<br />
<br />
6&gt; END;<br />
<br />
7&gt; /</code></div></div>
We can call the procedure ‘emp_name’ in this way from a PL/SQL Block.<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>1&gt; DECLARE<br />
<br />
2&gt;&nbsp;&nbsp;empName varchar(20);<br />
<br />
3&gt;&nbsp;&nbsp;CURSOR id_cur SELECT id FROM emp_ids;<br />
<br />
4&gt; BEGIN<br />
<br />
5&gt; FOR emp_rec in id_cur<br />
<br />
6&gt; LOOP<br />
<br />
7&gt;&nbsp;&nbsp; emp_name(emp_rec.id, empName);<br />
<br />
8&gt;&nbsp;&nbsp; dbms_output.putline('The employee ' || empName || ' has id ' || emp-rec.id);<br />
<br />
9&gt; END LOOP;<br />
<br />
10&gt; END;<br />
<br />
11&gt; /</code></div></div>
<br />
In the above PL/SQL Block<br />
In line no 3; we are creating a cursor ‘id_cur’ which contains the employee id.<br />
In line no 7; we are calling the procedure ‘emp_name’, we are passing the ‘id’ as IN parameter and ‘empName’ as OUT parameter.<br />
In line no 8; we are displaying the id and the employee name which we got from the procedure ‘emp_name’.<br />
<br />
<span style="font-weight: bold;">Example 2:</span><br />
<span style="font-weight: bold;"><br />
Using IN OUT parameter in procedures:</span><br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>1&gt; CREATE OR REPLACE PROCEDURE emp_salary_increase<br />
<br />
2&gt; (emp_id IN emptbl.empID%type, salary_inc IN OUT emptbl.salary%type) <br />
<br />
3&gt; IS <br />
<br />
4&gt;&nbsp;&nbsp;&nbsp;&nbsp;tmp_sal number; <br />
<br />
5&gt; BEGIN <br />
<br />
6&gt;&nbsp;&nbsp;&nbsp;&nbsp;SELECT salary <br />
<br />
7&gt;&nbsp;&nbsp;&nbsp;&nbsp;INTO tmp_sal <br />
<br />
8&gt;&nbsp;&nbsp;&nbsp;&nbsp;FROM emp_tbl<br />
<br />
9&gt;&nbsp;&nbsp;&nbsp;&nbsp;WHERE empID = emp_id; <br />
<br />
10&gt;&nbsp;&nbsp; IF tmp_sal between 10000 and 20000 THEN <br />
<br />
11&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;salary_inout := tmp_sal * 1.2; <br />
<br />
12&gt;&nbsp;&nbsp; ELSIF tmp_sal between 20000 and 30000 THEN <br />
<br />
13&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;salary_inout := tmp_sal * 1.3; <br />
<br />
14&gt;&nbsp;&nbsp; ELSIF tmp_sal &gt; 30000 THEN <br />
<br />
15&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;salary_inout := tmp_sal * 1.4; <br />
<br />
16&gt;&nbsp;&nbsp; END IF; <br />
<br />
17&gt; END;<br />
<br />
18&gt; /</code></div></div>
<br />
The below PL/SQL block shows how to execute the above 'emp_salary_increase' procedure.<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>1&gt; DECLARE <br />
<br />
2&gt;&nbsp;&nbsp;&nbsp;&nbsp;CURSOR updated_sal is <br />
<br />
3&gt;&nbsp;&nbsp;&nbsp;&nbsp;SELECT empID,salary <br />
<br />
4&gt;&nbsp;&nbsp;&nbsp;&nbsp;FROM emp_tbl; <br />
<br />
5&gt;&nbsp;&nbsp;&nbsp;&nbsp;pre_sal number; <br />
<br />
6&gt; BEGIN <br />
<br />
7&gt;&nbsp;&nbsp; FOR emp_rec IN updated_sal LOOP <br />
<br />
8&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pre_sal := emp_rec.salary; <br />
<br />
9&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emp_salary_increase(emp_rec.empID, emp_rec.salary); <br />
<br />
10&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbms_output.put_line('The salary of ' || emp_rec.empID || <br />
<br />
11&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' increased from '|| pre_sal || ' to '||emp_rec.salary);<br />
<br />
12&gt;&nbsp;&nbsp; END LOOP; <br />
<br />
13&gt; END;<br />
<br />
14&gt; /</code></div></div>
]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[How to execute a PL/SQL Function]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2724</link>
			<pubDate>Sat, 14 Nov 2009 15:26:08 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2724</guid>
			<description><![CDATA[A function can be executed in the following ways.<br />
<br />
1) Since a function returns a value we can assign it to a variable.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>employee_name :=&nbsp;&nbsp;employer_details_func;</code></div></div>
<br />
If ‘employee_name’ is of datatype varchar we can store the name of the employee by assigning the return type of the function to it.<br />
<br />
2) As a part of a SELECT statement<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>SELECT employer_details_func FROM dual;</code></div></div>
<br />
3) In a PL/SQL Statements like,<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>dbms_output.put_line(employer_details_func);</code></div></div>
<br />
This line displays the value returned by the function.]]></description>
			<content:encoded><![CDATA[A function can be executed in the following ways.<br />
<br />
1) Since a function returns a value we can assign it to a variable.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>employee_name :=&nbsp;&nbsp;employer_details_func;</code></div></div>
<br />
If ‘employee_name’ is of datatype varchar we can store the name of the employee by assigning the return type of the function to it.<br />
<br />
2) As a part of a SELECT statement<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>SELECT employer_details_func FROM dual;</code></div></div>
<br />
3) In a PL/SQL Statements like,<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>dbms_output.put_line(employer_details_func);</code></div></div>
<br />
This line displays the value returned by the function.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[PL/SQL Functions]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2723</link>
			<pubDate>Sat, 14 Nov 2009 15:25:28 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2723</guid>
			<description><![CDATA[<span style="font-weight: bold;">What is a Function in PL/SQL</span><br />
<br />
A function is a named PL/SQL Block which is similar to a procedure. The major difference between a procedure and a function is, a function must always return a value, but a procedure may or may not return a value.<br />
<span style="font-weight: bold;"><br />
The General Syntax to create a function is:</span><br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE [OR REPLACE&#93; FUNCTION function_name [parameters&#93; <br />
<br />
RETURN return_datatype;&nbsp;&nbsp;<br />
<br />
IS&nbsp;&nbsp;<br />
<br />
Declaration_section&nbsp;&nbsp;<br />
<br />
BEGIN&nbsp;&nbsp;<br />
<br />
Execution_section <br />
<br />
Return return_variable;&nbsp;&nbsp;<br />
<br />
EXCEPTION&nbsp;&nbsp;<br />
<br />
exception section&nbsp;&nbsp;<br />
<br />
Return return_variable;&nbsp;&nbsp;<br />
<br />
END;</code></div></div>
<br />
1) Return Type: The header section defines the return type of the function. The return datatype can be any of the oracle datatype like varchar, number etc.<br />
2) The execution and exception section both should return a value which is of the datatype defined in the header section.<br />
<br />
For example, let’s create a frunction called ''employer_details_func' similar to the one created in stored proc<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>1&gt; CREATE OR REPLACE FUNCTION employer_details_func<br />
<br />
2&gt;&nbsp;&nbsp;&nbsp;&nbsp;RETURN VARCHAR(20);<br />
<br />
3&gt; IS <br />
<br />
5&gt;&nbsp;&nbsp;&nbsp;&nbsp;emp_name VARCHAR(20); <br />
<br />
6&gt; BEGIN <br />
<br />
7&gt;&nbsp;&nbsp;&nbsp;&nbsp;SELECT first_name INTO emp_name<br />
<br />
8&gt;&nbsp;&nbsp;&nbsp;&nbsp;FROM emp_tbl WHERE empID = '100';<br />
<br />
9&gt;&nbsp;&nbsp;&nbsp;&nbsp;RETURN emp_name;<br />
<br />
10&gt; END;<br />
<br />
11&gt; /</code></div></div>
<br />
In the example we are retrieving the ‘first_name’ of employee with empID 100 to variable ‘emp_name’.<br />
The return type of the function is VARCHAR which is declared in line no 2.<br />
The function returns the 'emp_name' which is of type VARCHAR as the return value in line no 9.]]></description>
			<content:encoded><![CDATA[<span style="font-weight: bold;">What is a Function in PL/SQL</span><br />
<br />
A function is a named PL/SQL Block which is similar to a procedure. The major difference between a procedure and a function is, a function must always return a value, but a procedure may or may not return a value.<br />
<span style="font-weight: bold;"><br />
The General Syntax to create a function is:</span><br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE [OR REPLACE] FUNCTION function_name [parameters] <br />
<br />
RETURN return_datatype;&nbsp;&nbsp;<br />
<br />
IS&nbsp;&nbsp;<br />
<br />
Declaration_section&nbsp;&nbsp;<br />
<br />
BEGIN&nbsp;&nbsp;<br />
<br />
Execution_section <br />
<br />
Return return_variable;&nbsp;&nbsp;<br />
<br />
EXCEPTION&nbsp;&nbsp;<br />
<br />
exception section&nbsp;&nbsp;<br />
<br />
Return return_variable;&nbsp;&nbsp;<br />
<br />
END;</code></div></div>
<br />
1) Return Type: The header section defines the return type of the function. The return datatype can be any of the oracle datatype like varchar, number etc.<br />
2) The execution and exception section both should return a value which is of the datatype defined in the header section.<br />
<br />
For example, let’s create a frunction called ''employer_details_func' similar to the one created in stored proc<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>1&gt; CREATE OR REPLACE FUNCTION employer_details_func<br />
<br />
2&gt;&nbsp;&nbsp;&nbsp;&nbsp;RETURN VARCHAR(20);<br />
<br />
3&gt; IS <br />
<br />
5&gt;&nbsp;&nbsp;&nbsp;&nbsp;emp_name VARCHAR(20); <br />
<br />
6&gt; BEGIN <br />
<br />
7&gt;&nbsp;&nbsp;&nbsp;&nbsp;SELECT first_name INTO emp_name<br />
<br />
8&gt;&nbsp;&nbsp;&nbsp;&nbsp;FROM emp_tbl WHERE empID = '100';<br />
<br />
9&gt;&nbsp;&nbsp;&nbsp;&nbsp;RETURN emp_name;<br />
<br />
10&gt; END;<br />
<br />
11&gt; /</code></div></div>
<br />
In the example we are retrieving the ‘first_name’ of employee with empID 100 to variable ‘emp_name’.<br />
The return type of the function is VARCHAR which is declared in line no 2.<br />
The function returns the 'emp_name' which is of type VARCHAR as the return value in line no 9.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[How to execute a Stored Procedure in PL/SQL]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2722</link>
			<pubDate>Sat, 14 Nov 2009 15:24:14 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2722</guid>
			<description><![CDATA[There are two ways to execute a procedure.<br />
<br />
1) From the SQL prompt.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>EXECUTE [or EXEC&#93; procedure_name;</code></div></div>
<br />
2) Within another procedure – simply use the procedure name.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>procedure_name;</code></div></div>
<br />
NOTE: In the examples given above, we are using backward slash ‘/’ at the end of the program. This indicates the oracle engine that the PL/SQL program has ended and it can begin processing the statements.]]></description>
			<content:encoded><![CDATA[There are two ways to execute a procedure.<br />
<br />
1) From the SQL prompt.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>EXECUTE [or EXEC] procedure_name;</code></div></div>
<br />
2) Within another procedure – simply use the procedure name.<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>procedure_name;</code></div></div>
<br />
NOTE: In the examples given above, we are using backward slash ‘/’ at the end of the program. This indicates the oracle engine that the PL/SQL program has ended and it can begin processing the statements.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Stored Procedures In PL/SQL]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2721</link>
			<pubDate>Sat, 14 Nov 2009 15:23:28 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2721</guid>
			<description><![CDATA[A stored procedure or in simple a proc is a named PL/SQL block which performs one or more specific task. This is similar to a procedure in other programming languages. A procedure has a header and a body. The header consists of the name of the procedure and the parameters or variables passed to the procedure. The body consists or declaration section, execution section and exception section similar to a general PL/SQL Block. A procedure is similar to an anonymous PL/SQL Block but it is named for repeated usage.<br />
<span style="font-weight: bold;"><br />
We can pass parameters to procedures in three ways.</span><br />
1) IN-parameters<br />
2) OUT-parameters<br />
3) IN OUT-parameters<br />
<br />
A procedure may or may not return any value.<br />
<span style="font-weight: bold;"><br />
General Syntax to create a procedure is:</span><br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE [OR REPLACE&#93; PROCEDURE proc_name [list of parameters&#93; <br />
<br />
IS&nbsp;&nbsp;&nbsp;&nbsp;<br />
<br />
&nbsp;&nbsp; Declaration section <br />
<br />
BEGIN&nbsp;&nbsp;&nbsp;&nbsp;<br />
<br />
&nbsp;&nbsp; Execution section <br />
<br />
EXCEPTION&nbsp;&nbsp;&nbsp;&nbsp;<br />
<br />
&nbsp;&nbsp;Exception section <br />
<br />
END;</code></div></div>
<br />
IS - marks the beginning of the body of the procedure and is similar to DECLARE in anonymous PL/SQL Blocks. The code between IS and BEGIN forms the Declaration section.<br />
<br />
The syntax within the brackets [ &#93; indicate they are optional. By using CREATE OR REPLACE together the procedure is created if no other procedure with the same name exists or the existing procedure is replaced with the current code.<br />
<br />
The below example creates a procedure ‘employer_details’ which gives the details of the employee.<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>1&gt; CREATE OR REPLACE PROCEDURE employer_details<br />
<br />
2&gt; IS <br />
<br />
3&gt;&nbsp;&nbsp;CURSOR emp_cur IS <br />
<br />
4&gt;&nbsp;&nbsp;SELECT first_name, last_name, salary FROM emp_tbl;<br />
<br />
5&gt;&nbsp;&nbsp;emp_rec emp_cur%rowtype;<br />
<br />
6&gt; BEGIN <br />
<br />
7&gt;&nbsp;&nbsp;FOR emp_rec in sales_cur <br />
<br />
8&gt;&nbsp;&nbsp;LOOP <br />
<br />
9&gt;&nbsp;&nbsp;dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name<br />
<br />
10&gt;&nbsp;&nbsp;&nbsp;&nbsp;|| ' ' ||emp_cur.salary);<br />
<br />
11&gt; END LOOP;<br />
<br />
12&gt;END;<br />
<br />
13&gt; /</code></div></div>
]]></description>
			<content:encoded><![CDATA[A stored procedure or in simple a proc is a named PL/SQL block which performs one or more specific task. This is similar to a procedure in other programming languages. A procedure has a header and a body. The header consists of the name of the procedure and the parameters or variables passed to the procedure. The body consists or declaration section, execution section and exception section similar to a general PL/SQL Block. A procedure is similar to an anonymous PL/SQL Block but it is named for repeated usage.<br />
<span style="font-weight: bold;"><br />
We can pass parameters to procedures in three ways.</span><br />
1) IN-parameters<br />
2) OUT-parameters<br />
3) IN OUT-parameters<br />
<br />
A procedure may or may not return any value.<br />
<span style="font-weight: bold;"><br />
General Syntax to create a procedure is:</span><br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters] <br />
<br />
IS&nbsp;&nbsp;&nbsp;&nbsp;<br />
<br />
&nbsp;&nbsp; Declaration section <br />
<br />
BEGIN&nbsp;&nbsp;&nbsp;&nbsp;<br />
<br />
&nbsp;&nbsp; Execution section <br />
<br />
EXCEPTION&nbsp;&nbsp;&nbsp;&nbsp;<br />
<br />
&nbsp;&nbsp;Exception section <br />
<br />
END;</code></div></div>
<br />
IS - marks the beginning of the body of the procedure and is similar to DECLARE in anonymous PL/SQL Blocks. The code between IS and BEGIN forms the Declaration section.<br />
<br />
The syntax within the brackets [ ] indicate they are optional. By using CREATE OR REPLACE together the procedure is created if no other procedure with the same name exists or the existing procedure is replaced with the current code.<br />
<br />
The below example creates a procedure ‘employer_details’ which gives the details of the employee.<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>1&gt; CREATE OR REPLACE PROCEDURE employer_details<br />
<br />
2&gt; IS <br />
<br />
3&gt;&nbsp;&nbsp;CURSOR emp_cur IS <br />
<br />
4&gt;&nbsp;&nbsp;SELECT first_name, last_name, salary FROM emp_tbl;<br />
<br />
5&gt;&nbsp;&nbsp;emp_rec emp_cur%rowtype;<br />
<br />
6&gt; BEGIN <br />
<br />
7&gt;&nbsp;&nbsp;FOR emp_rec in sales_cur <br />
<br />
8&gt;&nbsp;&nbsp;LOOP <br />
<br />
9&gt;&nbsp;&nbsp;dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name<br />
<br />
10&gt;&nbsp;&nbsp;&nbsp;&nbsp;|| ' ' ||emp_cur.salary);<br />
<br />
11&gt; END LOOP;<br />
<br />
12&gt;END;<br />
<br />
13&gt; /</code></div></div>
]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[How to use Explicit Cursor in PL/SQL]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2720</link>
			<pubDate>Sat, 14 Nov 2009 15:21:25 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2720</guid>
			<description><![CDATA[<htm><br />
<body>
.</em></li>
              </ul>
              <h3 class="subtitle"><strong>How to use Explicit Cursor?</strong></h3><br />
                <br>There are  four steps in using an Explicit Cursor. <br />
              <ul>
<br />
                <li>DECLARE the cursor in the declaration section.</li>
                <li>OPEN the cursor in the Execution Section.</li>
                <li>FETCH the data from cursor into PL/SQL variables or  records in the Execution Section.</li>
                <li>CLOSE the cursor in the Execution Section before you end  the PL/SQL Block.</li>
              </ul>
          <p>1) Declaring a  Cursor in the Declaration Section:<br />
<br />
            </p><pre class="style1">   DECLARE</pre>
            <pre class="style1">   CURSOR emp_cur IS </pre>
            <pre class="style1">   SELECT * </pre>
            <pre class="style1">   FROM emp_tbl</pre>
             <pre class="style1">   WHERE salary &gt; 5000; </pre>
<br />
              <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In the above example we are creating a cursor  ‘emp_cur’ on a query which returns the records of all the <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;employees with salary greater than 5000.  Here ‘emp_tbl’ in the table which contains records of all the <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;employees.</p>
              <p>2) Accessing the records in the cursor:<br />
                	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Once the cursor is created in the declaration section we can access the cursor in the execution <br>	<br />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;section of the PL/SQL program. </p>
				<h3>How to access an Explicit Cursor?</h3><br />
<br />
              These are the three steps in accessing the cursor.<br />
                1) Open the cursor.<br />
                2) Fetch the records in the cursor one at a time.<br />
                3) Close the cursor.<br />
              <p>General Syntax to open a cursor is:</p>
              <pre class="style1">OPEN cursor_name;</pre>
              <p>General Syntax to fetch records from a cursor is:</p>
<br />
              <pre class="style1">FETCH cursor_name INTO record_name;</pre>
                OR <br />
                <pre class="style1">FETCH cursor_name INTO variable_list;</pre>
              <p>General Syntax to close a cursor is:</p><p>
                </p><pre class="style1">CLOSE cursor_name;</pre>
              <p>When a cursor is opened, the first row becomes the current row. When the data is fetched it is copied to the record or variables and the logical pointer moves to the next row and it becomes the current row. On every fetch statement, the pointer moves to the next row. If you want to fetch after the last row, the program will throw an error. When there is more than one row in a cursor we can use loops along with explicit cursor attributes to fetch all the records.</p>
<br />
              <p>Points to remember while fetching a row:</p>
              <p>·	We can fetch the rows in a cursor to a PL/SQL Record or a list of variables created in the PL/SQL Block. <br />
                ·	If you are fetching a cursor to a PL/SQL Record, the record should have the same structure as the cursor.<br />
                ·	If you are fetching a cursor to a list of variables, the variables should be listed in the same order in         the fetch statement as the columns are present in the cursor.</p>
              <p>General Form of using an explicit cursor is:<br />
          </p><pre class="style1"> DECLARE</pre>
<br />
                <pre class="style1">    variables;</pre>
                <pre class="style1">    records;</pre>
                <pre class="style1">    create a cursor;</pre>
                <pre class="style1"> BEGIN </pre>
                <pre class="style1">   OPEN cursor;</pre>
<br />
                <pre class="style1">   FETCH cursor;</pre>
                <pre class="style1">     process the records;</pre>
                <pre class="style1">   CLOSE cursor;</pre>
                <pre class="style1"> END;</pre>
                <br />
<br />
<br />
<p>Lets Look at the example below<br><br />
<br />
Example 1: </p>
                <pre class="style1">1&gt; DECLARE </pre>
                <pre class="style1">2&gt;    emp_rec emp_tbl%rowtype;</pre>
                <pre class="style1">3&gt;    CURSOR emp_cur IS </pre>
<br />
                <pre class="style1">4&gt;    SELECT *</pre>
                <pre class="style1">5&gt;    FROM </pre>
                <pre class="style1">6&gt;    WHERE salary &gt; 10; </pre>
<br />
                <pre class="style1">7&gt; BEGIN </pre>
                <pre class="style1">8&gt;&nbsp;   OPEN emp_cur; </pre>
                <pre class="style1">9&gt;&nbsp;   FETCH emp_cur INTO emp_rec; </pre>
                <pre class="style1">10&gt;&nbsp;&nbsp;&nbsp;&nbsp;  dbms_output.put_line (emp_rec.first_name || '&nbsp; ' || emp_rec.last_name); </pre>
<br />
                <pre class="style1">11&gt;&nbsp;  CLOSE emp_cur; </pre>
                <pre class="style1">12&gt; END; </pre>
				<br />
				<p>In the above example, first we are creating a record ‘emp_rec’ of the same structure as of table ‘emp_tbl’ in line no 2. We can also create a record with a cursor by replacing the table name with the cursor name. Second, we are declaring a cursor ‘emp_cur’ from a select query in line no 3 - 6. Third, we are opening the cursor in the execution section in line no 8. Fourth, we are fetching the cursor to the record in line no 9. Fifth, we are displaying the first_name and last_name of the employee in the record emp_rec in line no 10. Sixth, we are closing the cursor in line no 11. </p>
				<h3 class="subtitle">What are Explicit Cursor Attributes?</h3><br />
				<p>Oracle provides some attributes known as Explicit Cursor Attributes to control the data processing while using cursors.  We use these attributes to avoid errors while accessing cursors through OPEN, FETCH and CLOSE Statements.</p>
<br />
				 <h3 class="subtitle">When does an error occur while accessing an explicit cursor?</h3><br />
				 <p>a) When we try to open a cursor which is not closed in the previous operation.<br>b) When we try to fetch a cursor after the last operation.</p>
				 <p>These are the attributes available to check the status of an explicit cursor. </p>
				 <table border="1" cellpadding="0" cellspacing="0">
                   <tbody><tr>
                     <td valign="top" width="126"><p align="center"><strong>Attributes</strong></p></td>
<br />
                     <td valign="top" width="258"><p align="center"><strong>Return values </strong></p></td>
                     <td valign="top" width="206"><p align="center"><strong>Example</strong></p></td>
                   </tr>
                   <tr>
                     <td rowspan="2" valign="top" width="126"><p>%FOUND</p></td>
                     <td valign="top" width="258">TRUE,    if fetch statement returns at least one row.</td>
                     <td rowspan="2" valign="top" width="206"><p>Cursor_name%FOUND</p></td>
<br />
                   </tr>
                   <tr>
                     <td valign="top" width="258">FALSE,    if fetch statement doesn’t return a row.</td>
                   </tr>
                   <tr>
                     <td rowspan="2" valign="top" width="126"><p>%NOTFOUND</p></td>
                     <td valign="top" width="258">TRUE,    , if fetch statement doesn’t return a row.</td>
<br />
                     <td rowspan="2" valign="top" width="206">Cursor_name%NOTFOUND</td>
                   </tr>
                   <tr>
                     <td valign="top" width="258">FALSE,    if fetch statement returns at least one row.</td>
                   </tr>
                   <tr>
                     <td rowspan="2" valign="top" width="126"><p>%ROWCOUNT</p></td>
<br />
                     <td valign="top" width="258">The    number of rows fetched by the fetch statement</td>
                     <td rowspan="2" valign="top" width="206"><p>Cursor_name%ROWCOUNT</p></td>
                   </tr>
                   <tr>
                     <td valign="top" width="258">If    no row is returned, the PL/SQL statement returns an error.</td>
                   </tr>
                   <tr>
<br />
                     <td rowspan="2" valign="top" width="126"><p>%ISOPEN</p></td>
                     <td valign="top" width="258">TRUE,    if the cursor is already open in the program</td>
                     <td rowspan="2" valign="top" width="206"><p>Cursor_name%ISNAME</p></td>
                   </tr>
                   <tr>
                     <td valign="top" width="258">FALSE,    if the cursor is not opened in the program.</td>
                   </tr>
<br />
                 </tbody></table>
				 				 <br />
				 <h3 class="subtitle">Using Loops with Explicit Cursors:</h3><br />
<p>Oracle provides three types of cursors namely SIMPLE LOOP, WHILE LOOP and FOR LOOP. These loops can be used to process multiple rows in the cursor. Here I will modify the same example for each loops to explain how to use loops with cursors. </p>
<br />
<h4>Cursor with a Simple Loop:</h4><br />
<br />
<pre class="style1">1&gt; DECLARE </pre>
<br />
<pre class="style1">2&gt;   CURSOR emp_cur IS </pre>
<pre class="style1">3&gt;   SELECT first_name, last_name, salary FROM emp_tbl; </pre>
<pre class="style1">4&gt;   emp_rec emp_cur%rowtype; </pre>
<pre class="style1">5&gt; BEGIN </pre>
<pre class="style1">6&gt;   IF NOT sales_cur%ISOPEN THEN </pre>
<br />
<pre class="style1">7&gt;      OPEN sales_cur; </pre>
<pre class="style1">8&gt;   END IF; </pre>
<pre class="style1">9&gt;   LOOP </pre>
<pre class="style1">10&gt;     FETCH emp_cur INTO emp_rec; </pre>
<pre class="style1">11&gt;     EXIT WHEN emp_cur%NOTFOUND; </pre>
<br />
<pre class="style1">12&gt;     dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name </pre>
<pre class="style1">13&gt;     || ' ' ||emp_cur.salary); </pre>
<pre class="style1">14&gt;  END LOOP; </pre>
<pre class="style1">15&gt;  END; </pre>
<pre class="style1">16&gt;  / </pre>
<br />
<p>In the above example we are using two cursor attributes %ISOPEN and %NOTFOUND. <br />
In line no 6, we are using the cursor attribute %ISOPEN to check if the cursor is open, if the condition is true the program does not open the cursor again, it directly moves to line no 9. <br />
In line no 11, we are using the cursor attribute %NOTFOUND to check whether the fetch returned any row. If there is no rows found the program would exit, a condition which exists when you fetch the cursor after the last row, if there is a row found the program continues.</p>
<p>
We can use %FOUND in place of %NOTFOUND and vice versa. If we do so, we need to reverse the logic of the program. So use these attributes in appropriate instances.<br />
</p>
<br />
<h4>Cursor with a While Loop:</h4><br />
<br />
<p>Lets modify the above program to use while loop.</p>
<br />
<pre class="style1">1&gt; DECLARE </pre>
<pre class="style1">2&gt;  CURSOR emp_cur IS </pre>
<pre class="style1">3&gt;  SELECT first_name, last_name, salary FROM emp_tbl; </pre>
<pre class="style1">4&gt;  emp_rec emp_cur%rowtype; </pre>
<pre class="style1">5&gt; BEGIN </pre>
<br />
<pre class="style1">6&gt;   IF NOT sales_cur%ISOPEN THEN </pre>
<pre class="style1">7&gt;      OPEN sales_cur; </pre>
<pre class="style1">8&gt;   END IF; </pre>
<pre class="style1">9&gt;   FETCH sales_cur INTO sales_rec;  </pre>
<pre class="style1">10&gt;  WHILE sales_cur%FOUND THEN  </pre>
<br />
<pre class="style1">11&gt;  LOOP </pre>
<pre class="style1">12&gt;    dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name </pre>
<pre class="style1">13&gt;    || ' ' ||emp_cur.salary); </pre>
<pre class="style1">15&gt;    FETCH sales_cur INTO sales_rec; </pre>
<pre class="style1">16&gt;  END LOOP; </pre>
<br />
<pre class="style1">17&gt; END; </pre>
<pre class="style1">18&gt; / </pre>
<br />
<p>
In the above example, in line no 10 we are using %FOUND to evaluate if the first fetch statement in line no 9 returned a row, if true the program moves into the while loop. In the loop we use fetch statement again (line no 15) to process the next row. If the fetch statement is not executed once before the while loop the while condition will return false in the first instance and the while loop is skipped. In the loop, before fetching the record again, always process the record retrieved by the first fetch statement, else you will skip the first row.</p>
 <br />
<h4>Cursor with a FOR Loop:</h4><br />
<p>
When using FOR LOOP you need not declare a record or variables to store the cursor values, need not open, fetch and close the cursor. These functions are accomplished by the FOR LOOP automatically.</p>
<br />
<p><strong>General Syntax for using FOR LOOP:</strong></p>
<br />
<pre class="style1">FOR record_name IN cusror_name </pre>
<pre class="style1">LOOP </pre>
<pre class="style1">    process the row...</pre>
<pre class="style1">END LOOP; </pre>
<br />
<p>Let’s use the above example to learn how to use for loops in cursors.</p>
<br />
<pre class="style1">1&gt; DECLARE </pre>
<pre class="style1">2&gt;  CURSOR emp_cur IS </pre>
<pre class="style1">3&gt;  SELECT first_name, last_name, salary FROM emp_tbl; </pre>
<pre class="style1">4&gt;  emp_rec emp_cur%rowtype; </pre>
<br />
<pre class="style1">5&gt; BEGIN </pre>
<pre class="style1">6&gt;  FOR emp_rec in sales_cur </pre>
<pre class="style1">7&gt;  LOOP  </pre>
<pre class="style1">8&gt;  dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name </pre>
<pre class="style1">9&gt;    || ' ' ||emp_cur.salary);  </pre>
<br />
<pre class="style1">10&gt; END LOOP; </pre>
<pre class="style1">11&gt;END;</pre>
<pre class="style1">12&gt; /</pre>
<br />
<p>In the above example, when the FOR loop is processed a record ‘emp_rec’of structure ‘emp_cur’ gets created, the cursor is opened, the rows are fetched to the record ‘emp_rec’ and the cursor is closed after the last row is processed. By using FOR Loop in your program, you can reduce the number of lines in the program.</p>
<br />
<p><strong>NOTE:</strong>  In the examples given above, we are using backward slash ‘/’ at the end of the program. This indicates the oracle engine that the PL/SQL program has ended and it can begin processing the statements.</p>
<br />
</body>
</html>]]></description>
			<content:encoded><![CDATA[<htm><br />
<body>
.</em></li>
              </ul>
              <h3 class="subtitle"><strong>How to use Explicit Cursor?</strong></h3><br />
                <br>There are  four steps in using an Explicit Cursor. <br />
              <ul>
<br />
                <li>DECLARE the cursor in the declaration section.</li>
                <li>OPEN the cursor in the Execution Section.</li>
                <li>FETCH the data from cursor into PL/SQL variables or  records in the Execution Section.</li>
                <li>CLOSE the cursor in the Execution Section before you end  the PL/SQL Block.</li>
              </ul>
          <p>1) Declaring a  Cursor in the Declaration Section:<br />
<br />
            </p><pre class="style1">   DECLARE</pre>
            <pre class="style1">   CURSOR emp_cur IS </pre>
            <pre class="style1">   SELECT * </pre>
            <pre class="style1">   FROM emp_tbl</pre>
             <pre class="style1">   WHERE salary &gt; 5000; </pre>
<br />
              <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In the above example we are creating a cursor  ‘emp_cur’ on a query which returns the records of all the <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;employees with salary greater than 5000.  Here ‘emp_tbl’ in the table which contains records of all the <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;employees.</p>
              <p>2) Accessing the records in the cursor:<br />
                	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Once the cursor is created in the declaration section we can access the cursor in the execution <br>	<br />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;section of the PL/SQL program. </p>
				<h3>How to access an Explicit Cursor?</h3><br />
<br />
              These are the three steps in accessing the cursor.<br />
                1) Open the cursor.<br />
                2) Fetch the records in the cursor one at a time.<br />
                3) Close the cursor.<br />
              <p>General Syntax to open a cursor is:</p>
              <pre class="style1">OPEN cursor_name;</pre>
              <p>General Syntax to fetch records from a cursor is:</p>
<br />
              <pre class="style1">FETCH cursor_name INTO record_name;</pre>
                OR <br />
                <pre class="style1">FETCH cursor_name INTO variable_list;</pre>
              <p>General Syntax to close a cursor is:</p><p>
                </p><pre class="style1">CLOSE cursor_name;</pre>
              <p>When a cursor is opened, the first row becomes the current row. When the data is fetched it is copied to the record or variables and the logical pointer moves to the next row and it becomes the current row. On every fetch statement, the pointer moves to the next row. If you want to fetch after the last row, the program will throw an error. When there is more than one row in a cursor we can use loops along with explicit cursor attributes to fetch all the records.</p>
<br />
              <p>Points to remember while fetching a row:</p>
              <p>·	We can fetch the rows in a cursor to a PL/SQL Record or a list of variables created in the PL/SQL Block. <br />
                ·	If you are fetching a cursor to a PL/SQL Record, the record should have the same structure as the cursor.<br />
                ·	If you are fetching a cursor to a list of variables, the variables should be listed in the same order in         the fetch statement as the columns are present in the cursor.</p>
              <p>General Form of using an explicit cursor is:<br />
          </p><pre class="style1"> DECLARE</pre>
<br />
                <pre class="style1">    variables;</pre>
                <pre class="style1">    records;</pre>
                <pre class="style1">    create a cursor;</pre>
                <pre class="style1"> BEGIN </pre>
                <pre class="style1">   OPEN cursor;</pre>
<br />
                <pre class="style1">   FETCH cursor;</pre>
                <pre class="style1">     process the records;</pre>
                <pre class="style1">   CLOSE cursor;</pre>
                <pre class="style1"> END;</pre>
                <br />
<br />
<br />
<p>Lets Look at the example below<br><br />
<br />
Example 1: </p>
                <pre class="style1">1&gt; DECLARE </pre>
                <pre class="style1">2&gt;    emp_rec emp_tbl%rowtype;</pre>
                <pre class="style1">3&gt;    CURSOR emp_cur IS </pre>
<br />
                <pre class="style1">4&gt;    SELECT *</pre>
                <pre class="style1">5&gt;    FROM </pre>
                <pre class="style1">6&gt;    WHERE salary &gt; 10; </pre>
<br />
                <pre class="style1">7&gt; BEGIN </pre>
                <pre class="style1">8&gt;&nbsp;   OPEN emp_cur; </pre>
                <pre class="style1">9&gt;&nbsp;   FETCH emp_cur INTO emp_rec; </pre>
                <pre class="style1">10&gt;&nbsp;&nbsp;&nbsp;&nbsp;  dbms_output.put_line (emp_rec.first_name || '&nbsp; ' || emp_rec.last_name); </pre>
<br />
                <pre class="style1">11&gt;&nbsp;  CLOSE emp_cur; </pre>
                <pre class="style1">12&gt; END; </pre>
				<br />
				<p>In the above example, first we are creating a record ‘emp_rec’ of the same structure as of table ‘emp_tbl’ in line no 2. We can also create a record with a cursor by replacing the table name with the cursor name. Second, we are declaring a cursor ‘emp_cur’ from a select query in line no 3 - 6. Third, we are opening the cursor in the execution section in line no 8. Fourth, we are fetching the cursor to the record in line no 9. Fifth, we are displaying the first_name and last_name of the employee in the record emp_rec in line no 10. Sixth, we are closing the cursor in line no 11. </p>
				<h3 class="subtitle">What are Explicit Cursor Attributes?</h3><br />
				<p>Oracle provides some attributes known as Explicit Cursor Attributes to control the data processing while using cursors.  We use these attributes to avoid errors while accessing cursors through OPEN, FETCH and CLOSE Statements.</p>
<br />
				 <h3 class="subtitle">When does an error occur while accessing an explicit cursor?</h3><br />
				 <p>a) When we try to open a cursor which is not closed in the previous operation.<br>b) When we try to fetch a cursor after the last operation.</p>
				 <p>These are the attributes available to check the status of an explicit cursor. </p>
				 <table border="1" cellpadding="0" cellspacing="0">
                   <tbody><tr>
                     <td valign="top" width="126"><p align="center"><strong>Attributes</strong></p></td>
<br />
                     <td valign="top" width="258"><p align="center"><strong>Return values </strong></p></td>
                     <td valign="top" width="206"><p align="center"><strong>Example</strong></p></td>
                   </tr>
                   <tr>
                     <td rowspan="2" valign="top" width="126"><p>%FOUND</p></td>
                     <td valign="top" width="258">TRUE,    if fetch statement returns at least one row.</td>
                     <td rowspan="2" valign="top" width="206"><p>Cursor_name%FOUND</p></td>
<br />
                   </tr>
                   <tr>
                     <td valign="top" width="258">FALSE,    if fetch statement doesn’t return a row.</td>
                   </tr>
                   <tr>
                     <td rowspan="2" valign="top" width="126"><p>%NOTFOUND</p></td>
                     <td valign="top" width="258">TRUE,    , if fetch statement doesn’t return a row.</td>
<br />
                     <td rowspan="2" valign="top" width="206">Cursor_name%NOTFOUND</td>
                   </tr>
                   <tr>
                     <td valign="top" width="258">FALSE,    if fetch statement returns at least one row.</td>
                   </tr>
                   <tr>
                     <td rowspan="2" valign="top" width="126"><p>%ROWCOUNT</p></td>
<br />
                     <td valign="top" width="258">The    number of rows fetched by the fetch statement</td>
                     <td rowspan="2" valign="top" width="206"><p>Cursor_name%ROWCOUNT</p></td>
                   </tr>
                   <tr>
                     <td valign="top" width="258">If    no row is returned, the PL/SQL statement returns an error.</td>
                   </tr>
                   <tr>
<br />
                     <td rowspan="2" valign="top" width="126"><p>%ISOPEN</p></td>
                     <td valign="top" width="258">TRUE,    if the cursor is already open in the program</td>
                     <td rowspan="2" valign="top" width="206"><p>Cursor_name%ISNAME</p></td>
                   </tr>
                   <tr>
                     <td valign="top" width="258">FALSE,    if the cursor is not opened in the program.</td>
                   </tr>
<br />
                 </tbody></table>
				 				 <br />
				 <h3 class="subtitle">Using Loops with Explicit Cursors:</h3><br />
<p>Oracle provides three types of cursors namely SIMPLE LOOP, WHILE LOOP and FOR LOOP. These loops can be used to process multiple rows in the cursor. Here I will modify the same example for each loops to explain how to use loops with cursors. </p>
<br />
<h4>Cursor with a Simple Loop:</h4><br />
<br />
<pre class="style1">1&gt; DECLARE </pre>
<br />
<pre class="style1">2&gt;   CURSOR emp_cur IS </pre>
<pre class="style1">3&gt;   SELECT first_name, last_name, salary FROM emp_tbl; </pre>
<pre class="style1">4&gt;   emp_rec emp_cur%rowtype; </pre>
<pre class="style1">5&gt; BEGIN </pre>
<pre class="style1">6&gt;   IF NOT sales_cur%ISOPEN THEN </pre>
<br />
<pre class="style1">7&gt;      OPEN sales_cur; </pre>
<pre class="style1">8&gt;   END IF; </pre>
<pre class="style1">9&gt;   LOOP </pre>
<pre class="style1">10&gt;     FETCH emp_cur INTO emp_rec; </pre>
<pre class="style1">11&gt;     EXIT WHEN emp_cur%NOTFOUND; </pre>
<br />
<pre class="style1">12&gt;     dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name </pre>
<pre class="style1">13&gt;     || ' ' ||emp_cur.salary); </pre>
<pre class="style1">14&gt;  END LOOP; </pre>
<pre class="style1">15&gt;  END; </pre>
<pre class="style1">16&gt;  / </pre>
<br />
<p>In the above example we are using two cursor attributes %ISOPEN and %NOTFOUND. <br />
In line no 6, we are using the cursor attribute %ISOPEN to check if the cursor is open, if the condition is true the program does not open the cursor again, it directly moves to line no 9. <br />
In line no 11, we are using the cursor attribute %NOTFOUND to check whether the fetch returned any row. If there is no rows found the program would exit, a condition which exists when you fetch the cursor after the last row, if there is a row found the program continues.</p>
<p>
We can use %FOUND in place of %NOTFOUND and vice versa. If we do so, we need to reverse the logic of the program. So use these attributes in appropriate instances.<br />
</p>
<br />
<h4>Cursor with a While Loop:</h4><br />
<br />
<p>Lets modify the above program to use while loop.</p>
<br />
<pre class="style1">1&gt; DECLARE </pre>
<pre class="style1">2&gt;  CURSOR emp_cur IS </pre>
<pre class="style1">3&gt;  SELECT first_name, last_name, salary FROM emp_tbl; </pre>
<pre class="style1">4&gt;  emp_rec emp_cur%rowtype; </pre>
<pre class="style1">5&gt; BEGIN </pre>
<br />
<pre class="style1">6&gt;   IF NOT sales_cur%ISOPEN THEN </pre>
<pre class="style1">7&gt;      OPEN sales_cur; </pre>
<pre class="style1">8&gt;   END IF; </pre>
<pre class="style1">9&gt;   FETCH sales_cur INTO sales_rec;  </pre>
<pre class="style1">10&gt;  WHILE sales_cur%FOUND THEN  </pre>
<br />
<pre class="style1">11&gt;  LOOP </pre>
<pre class="style1">12&gt;    dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name </pre>
<pre class="style1">13&gt;    || ' ' ||emp_cur.salary); </pre>
<pre class="style1">15&gt;    FETCH sales_cur INTO sales_rec; </pre>
<pre class="style1">16&gt;  END LOOP; </pre>
<br />
<pre class="style1">17&gt; END; </pre>
<pre class="style1">18&gt; / </pre>
<br />
<p>
In the above example, in line no 10 we are using %FOUND to evaluate if the first fetch statement in line no 9 returned a row, if true the program moves into the while loop. In the loop we use fetch statement again (line no 15) to process the next row. If the fetch statement is not executed once before the while loop the while condition will return false in the first instance and the while loop is skipped. In the loop, before fetching the record again, always process the record retrieved by the first fetch statement, else you will skip the first row.</p>
 <br />
<h4>Cursor with a FOR Loop:</h4><br />
<p>
When using FOR LOOP you need not declare a record or variables to store the cursor values, need not open, fetch and close the cursor. These functions are accomplished by the FOR LOOP automatically.</p>
<br />
<p><strong>General Syntax for using FOR LOOP:</strong></p>
<br />
<pre class="style1">FOR record_name IN cusror_name </pre>
<pre class="style1">LOOP </pre>
<pre class="style1">    process the row...</pre>
<pre class="style1">END LOOP; </pre>
<br />
<p>Let’s use the above example to learn how to use for loops in cursors.</p>
<br />
<pre class="style1">1&gt; DECLARE </pre>
<pre class="style1">2&gt;  CURSOR emp_cur IS </pre>
<pre class="style1">3&gt;  SELECT first_name, last_name, salary FROM emp_tbl; </pre>
<pre class="style1">4&gt;  emp_rec emp_cur%rowtype; </pre>
<br />
<pre class="style1">5&gt; BEGIN </pre>
<pre class="style1">6&gt;  FOR emp_rec in sales_cur </pre>
<pre class="style1">7&gt;  LOOP  </pre>
<pre class="style1">8&gt;  dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name </pre>
<pre class="style1">9&gt;    || ' ' ||emp_cur.salary);  </pre>
<br />
<pre class="style1">10&gt; END LOOP; </pre>
<pre class="style1">11&gt;END;</pre>
<pre class="style1">12&gt; /</pre>
<br />
<p>In the above example, when the FOR loop is processed a record ‘emp_rec’of structure ‘emp_cur’ gets created, the cursor is opened, the rows are fetched to the record ‘emp_rec’ and the cursor is closed after the last row is processed. By using FOR Loop in your program, you can reduce the number of lines in the program.</p>
<br />
<p><strong>NOTE:</strong>  In the examples given above, we are using backward slash ‘/’ at the end of the program. This indicates the oracle engine that the PL/SQL program has ended and it can begin processing the statements.</p>
<br />
</body>
</html>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[What are Cursors in PL/SQL]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2719</link>
			<pubDate>Sat, 14 Nov 2009 15:18:21 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2719</guid>
			<description><![CDATA[<html>
<body>
<h2 class="title">What are Cursors in PL/SQL </h2><br />
			<br />
          <p>              A cursor is a  temporary work area created in the system memory when a SQL statement is  executed. A cursor contains information on a select statement and the rows of  data accessed by it. This temporary work area is used to store the data  retrieved from the database, and manipulate this data. A cursor can hold more  than one row, but can process only one row at a time. The set of rows the cursor  holds is called the <em>active </em>set. </p>
            <p>
              There are two types of  cursors in PL/SQL: </p>
              <h4><em>Implicit  cursors: </em> </h4>  <br />
              <p>These are created  by default when DML statements like, INSERT,  UPDATE, and DELETE  statements are executed. They are also created when a SELECT statement that returns just one row is  executed. <br></p>
<br />
              <h4><em>Explicit  cursors: </em></h4>  <br />
              <p>They must be  created when you are executing a SELECT  statement that returns more than one row. Even though the cursor stores  multiple records, only one record can be processed at a time, which is called  as current row. When you fetch a row the current row position moves to next  row. </p>
              Both implicit and  explicit cursors have the same functionality, but they differ in the way they are accessed. <br><br />
  <br />
			  <br />
			  <h3 class="subtitle">Implicit Cursors:</h3><br />
              <p>
              When you execute DML  statements like DELETE, INSERT, UPDATE and SELECT statements, implicit  statements are created to process these statements.<br></p><p>
              Oracle provides few  attributes called as implicit cursor attributes to check the status of DML operations.  The cursor attributes available are %FOUND,  %NOTFOUND, %ROWCOUNT, and %ISOPEN. <br></p><p>
              For example, When you  execute INSERT, UPDATE, or DELETE  statements the cursor attributes tell us whether any rows are affected and how many have been affected. <br> When a SELECT... INTO statement is executed in a PL/SQL Block, implicit cursor attributes can  be used to find out whether any row has been returned by the SELECT statement. <br />
              PL/SQL returns an error when no  data is selected. </p>
<br />
              <p>The status of the  cursor for each of these attributes are defined in the below table.&nbsp; </p>
              <table border="1" cellpadding="0" cellspacing="0">
                <tbody><tr>
                  <td width="116"><div align="center"><strong><br />
                  Attributes </strong></div></td>
                  <td width="312"><p align="center"><strong>Return Value </strong></p></td>
                  <td width="163"><p align="center"><strong>Example</strong></p></td>
<br />
                </tr>
                <tr>
                  <td rowspan="2" valign="top" width="116"><p align="left">%FOUND</p></td>
                  <td valign="top" width="312"><p>The return value is TRUE, if the DML    statements like INSERT, DELETE and UPDATE affect at least one row and  if    SELECT ….INTO statement return at least one row.</p></td>
                  <td rowspan="2" valign="top" width="163"><p align="center">SQL%FOUND</p></td>
                </tr>
                <tr>
<br />
                  <td valign="top" width="312"><p>The return value is FALSE, if DML    statements like INSERT, DELETE and UPDATE do not affect row and  if    SELECT….INTO statement do not return a row.</p></td>
                </tr>
                <tr>
                  <td rowspan="2" valign="top" width="116"><p>%NOTFOUND</p></td>
                  <td valign="top" width="312"><p>The return value is FALSE, if DML    statements like INSERT, DELETE and UPDATE at least one row and if SELECT    ….INTO statement return at least one row.</p></td>
                  <td rowspan="2" valign="top" width="163"><p align="center">SQL%NOTFOUND</p></td>
                </tr>
<br />
                <tr>
                  <td valign="top" width="312"><p>The return value is TRUE, if a DML    statement like INSERT, DELETE and UPDATE do not affect even one row and     if SELECT ….INTO statement does not return a row.</p></td>
                </tr>
                <tr>
                  <td valign="top" width="116"><p>%ROWCOUNT </p></td>
                  <td valign="top" width="312"><p>Return the number    of rows affected by the DML operations INSERT, DELETE, UPDATE, SELECT </p></td>
                  <td valign="top" width="163"><p>SQL%ROWCOUNT</p></td>
<br />
                </tr>
              </tbody></table>
			  <br />
              <p>For Example: Consider the PL/SQL Block that uses implicit cursor attributes as shown below: </p>
                <pre class="style1 style1">DECLARE  var_rows number(5);</pre>
                <pre class="style1">BEGIN</pre>
                <pre class="style1">  UPDATE employee </pre>
<br />
                <pre class="style1">  SET salary = salary + 1000;</pre>
                <pre class="style1">  IF SQL%NOTFOUND THEN</pre>
                <pre class="style1">    dbms_output.put_line('None of the salaries where updated');</pre>
                <pre class="style1">  ELSIF SQL%FOUND THEN</pre>
                <pre class="style1">    var_rows := SQL%ROWCOUNT;</pre>
<br />
                <pre class="style1">    dbms_output.put_line('Salaries for ' || var_rows || 'employees are updated');</pre>
                <pre class="style1">  END IF; </pre>
                <pre class="style1">END; </pre>
              <p>In the above PL/SQL Block, the salaries of all the employees in the ‘employee’ table are updated.  If none of the employee’s salary are updated we get a message 'None of the salaries where updated'. Else we get a message like for example, 'Salaries for 1000 employees are updated' if there are 1000 rows in ‘employee’ table. <br />
<br />
</body>
</html>]]></description>
			<content:encoded><![CDATA[<html>
<body>
<h2 class="title">What are Cursors in PL/SQL </h2><br />
			<br />
          <p>              A cursor is a  temporary work area created in the system memory when a SQL statement is  executed. A cursor contains information on a select statement and the rows of  data accessed by it. This temporary work area is used to store the data  retrieved from the database, and manipulate this data. A cursor can hold more  than one row, but can process only one row at a time. The set of rows the cursor  holds is called the <em>active </em>set. </p>
            <p>
              There are two types of  cursors in PL/SQL: </p>
              <h4><em>Implicit  cursors: </em> </h4>  <br />
              <p>These are created  by default when DML statements like, INSERT,  UPDATE, and DELETE  statements are executed. They are also created when a SELECT statement that returns just one row is  executed. <br></p>
<br />
              <h4><em>Explicit  cursors: </em></h4>  <br />
              <p>They must be  created when you are executing a SELECT  statement that returns more than one row. Even though the cursor stores  multiple records, only one record can be processed at a time, which is called  as current row. When you fetch a row the current row position moves to next  row. </p>
              Both implicit and  explicit cursors have the same functionality, but they differ in the way they are accessed. <br><br />
  <br />
			  <br />
			  <h3 class="subtitle">Implicit Cursors:</h3><br />
              <p>
              When you execute DML  statements like DELETE, INSERT, UPDATE and SELECT statements, implicit  statements are created to process these statements.<br></p><p>
              Oracle provides few  attributes called as implicit cursor attributes to check the status of DML operations.  The cursor attributes available are %FOUND,  %NOTFOUND, %ROWCOUNT, and %ISOPEN. <br></p><p>
              For example, When you  execute INSERT, UPDATE, or DELETE  statements the cursor attributes tell us whether any rows are affected and how many have been affected. <br> When a SELECT... INTO statement is executed in a PL/SQL Block, implicit cursor attributes can  be used to find out whether any row has been returned by the SELECT statement. <br />
              PL/SQL returns an error when no  data is selected. </p>
<br />
              <p>The status of the  cursor for each of these attributes are defined in the below table.&nbsp; </p>
              <table border="1" cellpadding="0" cellspacing="0">
                <tbody><tr>
                  <td width="116"><div align="center"><strong><br />
                  Attributes </strong></div></td>
                  <td width="312"><p align="center"><strong>Return Value </strong></p></td>
                  <td width="163"><p align="center"><strong>Example</strong></p></td>
<br />
                </tr>
                <tr>
                  <td rowspan="2" valign="top" width="116"><p align="left">%FOUND</p></td>
                  <td valign="top" width="312"><p>The return value is TRUE, if the DML    statements like INSERT, DELETE and UPDATE affect at least one row and  if    SELECT ….INTO statement return at least one row.</p></td>
                  <td rowspan="2" valign="top" width="163"><p align="center">SQL%FOUND</p></td>
                </tr>
                <tr>
<br />
                  <td valign="top" width="312"><p>The return value is FALSE, if DML    statements like INSERT, DELETE and UPDATE do not affect row and  if    SELECT….INTO statement do not return a row.</p></td>
                </tr>
                <tr>
                  <td rowspan="2" valign="top" width="116"><p>%NOTFOUND</p></td>
                  <td valign="top" width="312"><p>The return value is FALSE, if DML    statements like INSERT, DELETE and UPDATE at least one row and if SELECT    ….INTO statement return at least one row.</p></td>
                  <td rowspan="2" valign="top" width="163"><p align="center">SQL%NOTFOUND</p></td>
                </tr>
<br />
                <tr>
                  <td valign="top" width="312"><p>The return value is TRUE, if a DML    statement like INSERT, DELETE and UPDATE do not affect even one row and     if SELECT ….INTO statement does not return a row.</p></td>
                </tr>
                <tr>
                  <td valign="top" width="116"><p>%ROWCOUNT </p></td>
                  <td valign="top" width="312"><p>Return the number    of rows affected by the DML operations INSERT, DELETE, UPDATE, SELECT </p></td>
                  <td valign="top" width="163"><p>SQL%ROWCOUNT</p></td>
<br />
                </tr>
              </tbody></table>
			  <br />
              <p>For Example: Consider the PL/SQL Block that uses implicit cursor attributes as shown below: </p>
                <pre class="style1 style1">DECLARE  var_rows number(5);</pre>
                <pre class="style1">BEGIN</pre>
                <pre class="style1">  UPDATE employee </pre>
<br />
                <pre class="style1">  SET salary = salary + 1000;</pre>
                <pre class="style1">  IF SQL%NOTFOUND THEN</pre>
                <pre class="style1">    dbms_output.put_line('None of the salaries where updated');</pre>
                <pre class="style1">  ELSIF SQL%FOUND THEN</pre>
                <pre class="style1">    var_rows := SQL%ROWCOUNT;</pre>
<br />
                <pre class="style1">    dbms_output.put_line('Salaries for ' || var_rows || 'employees are updated');</pre>
                <pre class="style1">  END IF; </pre>
                <pre class="style1">END; </pre>
              <p>In the above PL/SQL Block, the salaries of all the employees in the ‘employee’ table are updated.  If none of the employee’s salary are updated we get a message 'None of the salaries where updated'. Else we get a message like for example, 'Salaries for 1000 employees are updated' if there are 1000 rows in ‘employee’ table. <br />
<br />
</body>
</html>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Iterative Statements in PL/SQL]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2718</link>
			<pubDate>Sat, 14 Nov 2009 15:16:20 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2718</guid>
			<description><![CDATA[An iterative control Statements are used when we want to repeat the execution of one or more statements for specified number of times. These are similar to those in<br />
<br />
There are three types of loops in PL/SQL:<br />
• Simple Loop<br />
• While Loop<br />
• For Loop<br />
1) Simple Loop<br />
<br />
A Simple Loop is used when a set of statements is to be executed at least once before the loop terminates. An EXIT condition must be specified in the loop, otherwise the loop will get into an infinite number of iterations. When the EXIT condition is satisfied the process exits from the loop.<br />
<span style="font-weight: bold;">The General Syntax to write a Simple Loop is:</span><br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>LOOP <br />
<br />
&nbsp;&nbsp; statements; <br />
<br />
&nbsp;&nbsp; EXIT; <br />
<br />
&nbsp;&nbsp; {or EXIT WHEN condition;}<br />
<br />
END LOOP;</code></div></div>
<br />
These are the important steps to be followed while using Simple Loop.<br />
<br />
1) Initialise a variable before the loop body.<br />
2) Increment the variable in the loop.<br />
3) Use a EXIT WHEN statement to exit from the Loop. If you use a EXIT statement without WHEN condition, the statements in the loop is executed only once.<br />
2) While Loop<br />
<br />
A WHILE LOOP is used when a set of statements has to be executed as long as a condition is true. The condition is evaluated at the beginning of each iteration. The iteration continues until the condition becomes false.<br />
<span style="font-weight: bold;">The General Syntax to write a WHILE LOOP is:</span><br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>WHILE &lt;condition&gt; <br />
<br />
 LOOP statements; <br />
<br />
END LOOP;</code></div></div>
<br />
Important steps to follow when executing a while loop:<br />
<br />
1) Initialise a variable before the loop body.<br />
2) Increment the variable in the loop.<br />
3) EXIT WHEN statement and EXIT statements can be used in while loops but it's not done oftenly.<br />
3) FOR Loop<br />
<br />
A FOR LOOP is used to execute a set of statements for a predetermined number of times. Iteration occurs between the start and end integer values given. The counter is always incremented by 1. The loop exits when the counter reachs the value of the end integer.<br />
<span style="font-weight: bold;"><br />
The General Syntax to write a FOR LOOP is:</span><br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>FOR counter IN val1..val2 <br />
<br />
&nbsp;&nbsp;LOOP statements; <br />
<br />
END LOOP;</code></div></div>
<br />
    * val1 - Start integer value.<br />
    * val2 - End integer value.<br />
<br />
Important steps to follow when executing a while loop:<br />
<br />
1) The counter variable is implicitly declared in the declaration section, so it's not necessary to declare it explicity.<br />
2) The counter variable is incremented by 1 and does not need to be incremented explicitly.<br />
3) EXIT WHEN statement and EXIT statements can be used in FOR loops but it's not done oftenly.<br />
<br />
<span style="font-weight: bold;">NOTE:</span> The above Loops are explained with a example when dealing with Explicit Cursors.]]></description>
			<content:encoded><![CDATA[An iterative control Statements are used when we want to repeat the execution of one or more statements for specified number of times. These are similar to those in<br />
<br />
There are three types of loops in PL/SQL:<br />
• Simple Loop<br />
• While Loop<br />
• For Loop<br />
1) Simple Loop<br />
<br />
A Simple Loop is used when a set of statements is to be executed at least once before the loop terminates. An EXIT condition must be specified in the loop, otherwise the loop will get into an infinite number of iterations. When the EXIT condition is satisfied the process exits from the loop.<br />
<span style="font-weight: bold;">The General Syntax to write a Simple Loop is:</span><br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>LOOP <br />
<br />
&nbsp;&nbsp; statements; <br />
<br />
&nbsp;&nbsp; EXIT; <br />
<br />
&nbsp;&nbsp; {or EXIT WHEN condition;}<br />
<br />
END LOOP;</code></div></div>
<br />
These are the important steps to be followed while using Simple Loop.<br />
<br />
1) Initialise a variable before the loop body.<br />
2) Increment the variable in the loop.<br />
3) Use a EXIT WHEN statement to exit from the Loop. If you use a EXIT statement without WHEN condition, the statements in the loop is executed only once.<br />
2) While Loop<br />
<br />
A WHILE LOOP is used when a set of statements has to be executed as long as a condition is true. The condition is evaluated at the beginning of each iteration. The iteration continues until the condition becomes false.<br />
<span style="font-weight: bold;">The General Syntax to write a WHILE LOOP is:</span><br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>WHILE &lt;condition&gt; <br />
<br />
 LOOP statements; <br />
<br />
END LOOP;</code></div></div>
<br />
Important steps to follow when executing a while loop:<br />
<br />
1) Initialise a variable before the loop body.<br />
2) Increment the variable in the loop.<br />
3) EXIT WHEN statement and EXIT statements can be used in while loops but it's not done oftenly.<br />
3) FOR Loop<br />
<br />
A FOR LOOP is used to execute a set of statements for a predetermined number of times. Iteration occurs between the start and end integer values given. The counter is always incremented by 1. The loop exits when the counter reachs the value of the end integer.<br />
<span style="font-weight: bold;"><br />
The General Syntax to write a FOR LOOP is:</span><br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>FOR counter IN val1..val2 <br />
<br />
&nbsp;&nbsp;LOOP statements; <br />
<br />
END LOOP;</code></div></div>
<br />
    * val1 - Start integer value.<br />
    * val2 - End integer value.<br />
<br />
Important steps to follow when executing a while loop:<br />
<br />
1) The counter variable is implicitly declared in the declaration section, so it's not necessary to declare it explicity.<br />
2) The counter variable is incremented by 1 and does not need to be incremented explicitly.<br />
3) EXIT WHEN statement and EXIT statements can be used in FOR loops but it's not done oftenly.<br />
<br />
<span style="font-weight: bold;">NOTE:</span> The above Loops are explained with a example when dealing with Explicit Cursors.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Conditional Statements in PL/SQL]]></title>
			<link>http://naturebios.com/natureboard/showthread.php?tid=2717</link>
			<pubDate>Sat, 14 Nov 2009 15:14:57 -0800</pubDate>
			<guid isPermaLink="false">http://naturebios.com/natureboard/showthread.php?tid=2717</guid>
			<description><![CDATA[As the name implies, PL/SQL supports programming language features like conditional statements, iterative statements.<br />
<br />
The programming constructs are similar to how you use in programming languages like Java and C++. In this section I will provide you syntax of how to use conditional statements in PL/SQL programming.<br />
<br />
<span style="font-weight: bold;">IF THEN ELSE STATEMENT</span><br />
<br />
1)<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>IF condition <br />
<br />
THEN <br />
<br />
 statement 1; <br />
<br />
ELSE <br />
<br />
 statement 2; <br />
<br />
END IF;</code></div></div>
<br />
 <br />
<br />
2)<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>IF condition 1 <br />
<br />
THEN <br />
<br />
 statement 1; <br />
<br />
 statement 2; <br />
<br />
ELSIF condtion2 THEN <br />
<br />
 statement 3; <br />
<br />
ELSE <br />
<br />
 statement 4; <br />
<br />
END IF</code></div></div>
<br />
 <br />
<br />
3)<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>IF condition 1 <br />
<br />
THEN <br />
<br />
 statement 1; <br />
<br />
 statement 2; <br />
<br />
ELSIF condtion2 THEN <br />
<br />
 statement 3; <br />
<br />
ELSE <br />
<br />
 statement 4; <br />
<br />
END IF;</code></div></div>
<br />
 <br />
<br />
4)<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>IF condition1 THEN <br />
<br />
ELSE <br />
<br />
 IF condition2 THEN <br />
<br />
 statement1; <br />
<br />
 END IF; <br />
<br />
ELSIF condition3 THEN <br />
<br />
&nbsp;&nbsp;statement2; <br />
<br />
END IF;</code></div></div>
]]></description>
			<content:encoded><![CDATA[As the name implies, PL/SQL supports programming language features like conditional statements, iterative statements.<br />
<br />
The programming constructs are similar to how you use in programming languages like Java and C++. In this section I will provide you syntax of how to use conditional statements in PL/SQL programming.<br />
<br />
<span style="font-weight: bold;">IF THEN ELSE STATEMENT</span><br />
<br />
1)<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>IF condition <br />
<br />
THEN <br />
<br />
 statement 1; <br />
<br />
ELSE <br />
<br />
 statement 2; <br />
<br />
END IF;</code></div></div>
<br />
 <br />
<br />
2)<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>IF condition 1 <br />
<br />
THEN <br />
<br />
 statement 1; <br />
<br />
 statement 2; <br />
<br />
ELSIF condtion2 THEN <br />
<br />
 statement 3; <br />
<br />
ELSE <br />
<br />
 statement 4; <br />
<br />
END IF</code></div></div>
<br />
 <br />
<br />
3)<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>IF condition 1 <br />
<br />
THEN <br />
<br />
 statement 1; <br />
<br />
 statement 2; <br />
<br />
ELSIF condtion2 THEN <br />
<br />
 statement 3; <br />
<br />
ELSE <br />
<br />
 statement 4; <br />
<br />
END IF;</code></div></div>
<br />
 <br />
<br />
4)<br />
<br />
<div class="codeblock">
<div class="title">Code:<br />
</div><div class="body" dir="ltr"><code>IF condition1 THEN <br />
<br />
ELSE <br />
<br />
 IF condition2 THEN <br />
<br />
 statement1; <br />
<br />
 END IF; <br />
<br />
ELSIF condition3 THEN <br />
<br />
&nbsp;&nbsp;statement2; <br />
<br />
END IF;</code></div></div>
]]></content:encoded>
		</item>
	</channel>
</rss>