Download DB_Lab 12 - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Entity–attribute–value model wikipedia , lookup

Microsoft Access wikipedia , lookup

Tandem Computers wikipedia , lookup

Database wikipedia , lookup

Relational algebra wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Relational model wikipedia , lookup

Oracle Database wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
FEST
Database System
Lab#12
OBJECTIVE:
TO BECOME FAMILIAR
EXCEPTION HANDLING
CURSORS IN PL/SQL AND
TOOL:
COMPUTER SYSTEM, INSTALLED ORACLE 9I
CURSORS:
Every SQL statement executed by the Oracle Server has an individual cursor associated
with it.
There are two types of Cursor
1) Implicit Cursors Declared for all DML and PL/SQL Select statements.
2) Explicit Cursors Declared and named by the programmer.
IMPLICIT CURSORS:
The Oracle Server implicitly opens a cursor to process each SQL statement
not associated with an explicitly declared cursor. We cannot use OPEN, FETCH, and CLOSE
statements to control the SQL cursor.
EXPLICIT CURSORS:
We can process each row returned by a multiple row Select statement
through Explicit Cursors. We have four basic commands to use Explicit Cursors.
1) Declare the cursor by naming it and defining the structure of the query to be performed
within it.
2) Open the cursor. The open statement executes the query and binds any variables that are
referenced.
3) Fetch Data from Cursor. The fetch statement loads the current row from the cursor into
variables. Each fetch causes the cursor to move its pointer to the next row in the active
set. If rows are found, the fetch loads the current row into variables; otherwise, it closes
the cursor.
4) Close the Cursor. The close statement releases the active set of rows. It is now possible to
reopen the cursor to establish a fresh active set.
Ali Orangzeb
FEST
Database System
DECLARING CURSOR:
SYNTAX:
CURSOR cursor_name IS
Select _ statement;
EXAMPLE:
DECLARE
CURSOR emp_cursor IS
SELECT empno, ename
from emp;
begin……
OPENING CURSOR:
SYNTAX:
OPEN cursor_name;
FETCHING DATA FROM CURSOR:
SYNTAX:
FETCH cursor_name INTO [variable1, variable2,----];
EXAMPLE:
FETCH emp_cursor INTO v_empno, v_ename;
CLOSING CURSOR:
SYNTAX:
CLOSE cursor_name;
HANDLING EXCEPTIONS:
Run-time errors arise from design faults, coding mistakes, hardware
failures, and many other sources. In PL/SQL, a warning or error condition is called an exception.
Exceptions can be internally defined (by the run-time system) or user defined. Internally defined
exceptions include division by zero and out of memory. Some common internal exceptions have
predefined names, such as ZERO_DIVIDE and STORAGE_ERROR. The others can be given
names.
An exception is an identifier in PL/SQL, raised during the execution of a block that
terminates its main body of actions. A block always terminates when PL/SQL raises an
exception but you specify an exception handler to perform final actions.
There are two methods for raising exceptions
Ali Orangzeb
FEST
Database System
1) An Oracle error occurs and the associated exception is raised automatically.
2) You raise an exception explicitly by issuing the RAISE statement within the block.
EXCEPTION TYPES
1) Predefined Oracle Server
2) Non-predefined Oracle Server
3) User_defined
(implicitly raised)
(implicitly raised)
(explicitly raised)
PREDEFINED EXCEPTIONS:
Trap a predefined Oracle Server error by referencing its
standard name within the corresponding exception-handling routine.
SYNTAX:
BEGIN SELECT …….COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
Statement1;
Statement2;
WHEN TOO_MANY_ROWS THEN
Statement1;
WHEN OTHERS THEN
Statement1;
End;
NON-PREDEFINED EXCEPTIONS:
You can trap a non-predefined Oracle Server error
by declaring it first, or by using the OTHER handler. The declared exception is raised implicitly.
The progma EXCEPTION_INIT tells the compiler to associate an exception name with an
Oracle Error number.
EXAMPLE:
DECLARE
e_emps_remaining EXCEPTION;
PROGMA EXCEPTION_INIT(e_emps_remaining, -2292);
v_deptno
dept.deptno%type := &p_deptno;
BEGIN
DELETE FROM dept
WHERE
deptno = v_deptno;
COMMIT;
EXCEPTION
WHEN e_emps_remaining THEN
DBMS_OUTPUT.PUT_LINE (‘cannot remove dept’ || TO_CHAR(v_deptno) ||
‘.Employees exist.’);
END;
Ali Orangzeb
FEST
Database System
USER-DEFINED EXCEPTIONS:
PL/SQL lets u define your own exceptions. User defined
exceptions must be.
 Declared in the declare section of a PL/SQL block.
 Raised explicitly with RAISE statements.
EXAMPLE:
DECLARE
e_invalid_product
EXCEPTION;
BEGIN
UPDATE product
SET
descrip=’&product_description’
WHERE prodid= & product_number;
IF SQL%NOTFOUND THEN
RAISE e_invalid_product;
END IF;
COMMIT;
EXCEPTION
WHEN e_invalid_product
THEN
DBMS_OUTPUT.PUT_LINE (‘Invalid product number.’);
END;
RESULT:
By the end of this practical we may able to use Cursors in PL/SQL and Exception
Handling
Ali Orangzeb