Вы находитесь на странице: 1из 10

CURSORS

Oracle uses work areas to execute Pl/SQL statements and store processing information. Cursor lets you name a work area and access its stored information. Implicit Cursors: Oracle implisitly opens a cursor to process each SQL statement not associated with an explicitly declared cursor. The recent implicit cursor is referred to as SQL cursor.It has got four attributes: SQL%ISOPEN,SQL%FOUND,SQL%NOTFOUND, SQL%ROWCOUNT

SQL%ISOPEN: Checks whether the implicit cursor is open or not.Returns BOOLEAN Value. SQL%FOUND: Returns True when DML statements affects some rows. SQL%NOTFOUND: Returns False when a DML statement affects some rows. SQL%ROWCOUNT: returns the number of rows affected by the DML statements. Explicit Cursor: The set of rows returned by a query can consist of zero, one, or multiple rows, depending on how many rows meet your search criteria. When a query returns multiple rows, you can explicitly define a cursor to process the rows.

Syntax for Cursor Declaration: DECLARE CURSOR <cursor name> IS SELECT <list of columns> FROM <table name> [WHERE <condition>]

The three commands used to control cursor are: OPEN: OPENing the cursor executes the query and identifies the active set, which consists of all rows that meet the query search criteria FETCH: The FETCH statement retrieves the rows in the active set one at a time. Each time FETCH is executed, the cursor advances to the next row in the active set. CLOSE: Release the cursor. Explicit cursor attributes: %NOTFOUND: Evaluates to TRUE if the last FETCH failed because no more rows available. %FOUND: After an explicit cursor is open but before the first fetch, %FOUND evaluates to NULL. Thereafter, it evaluates to TRUE if the last fetch returned a row or to FALSE if no row was returned. %ROWCOUNT: When you open its cursor, %ROWCOUNT is zeroed. Before the first fetch, %ROWCOUNT returns a zero. Thereafter, it returns the number of rows fetched so far. The number is incremented if the latest fetch returned a row. %ISOPEN: evaluates to TRUE if its cursor is open; otherwise, %ISOPEN evaluates to FALSE.

DECLARE <declarations.> CURSOR <cursorname> IS <query> BEGIN OPEN<cursorname> LOOP FETCH <CURSORNAME> INTO <Locavariable>; <statements>; EXIT WHEN<condition> END LOOP CLOSE <cursorname> END;

SQL>DECLARE Empno emp.ssn%type; eJob emp.job%type; Esal emp.sal%type; Cursor c1 is select ssn, job,sal from emp where dno=10; BEGIN open c1; LOOP Fetch c1 into empno,ejob,esal; Exit when c1%NOTFOUND If job=clerkand esal < 3000 then update sal=sal+0.1*esal; Elsif job=analyst update sal=sal+0.2*sal Else update sal=sal+0.3*sal; End if; End loop; Close c1; End;

PROCEDURE
CREATE OR REPLACE PROCEDURE <procname>(<var> IN<datatype>) IS (<local var declarations>) BEGIN <executable statements>; END; Create or replace procedure fib(n number) is F1 number; F2 number; I number; C number; Begin F1:=0; F2:=1; dbms_output.put_line(f1); dbms_output.put_line(f2); For I in 1..n Loop c:=f1+f2; dbms_output.put_line(c); F1:=f2; F2:=c; End loop End; SQL>exec fib(&n);

SQL>EXEC<procname>(<paramvalues>)

Functions:
CREATE OR REPLACE FUNCTION <fnname>(<parameters>) RETURN TYPE IS <localvar declarations> BEGIN <executable statements>; Return <expn>; END; TO execute function SQL>Declare local var declarations begin <var>:=<fnname>(values); . . End; 2.Select fnname(values) from dual;

Ex:Create or replace function fact(num number) Return number is I number; F number; Begin F:=1; For I in 1..num Loop F=f*I; End loop; Return f; End; / Executing function SQL> declare a number Begin a:=fact(&n); Dbms_output.putline(the factorial of n is=||a); End;

Trigger:It is a stored procedure which is fixed when any manipulation occurs on a specified table. Syntax:Create or replace trigger <name> [before/after][insert/update/delete] On <tablename>[for each statement/for each row] when <condition>; Declare <lovavar> Begin <statements> End; Ex:create or replace trigger del_trig Before delete on emp for each row Begin Delete from dept where dno=:old.dn end

Exceptions: In PL/SQL a warning or error condition is called an exception. Exceptions can be internally defined (by the runtime system) or user-defined. Examples of 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 other internal exceptions can be given names. You can define exceptions of your own in the declarative part of any PL/SQL block,

declare <excepname> EXCEPTION; <var1>datatype1; Begin <executable statements> If<cond> then Raise execp1; -- raise exception Else <executablestatements> End if EXCEPTION WHEN excep1 THEN executable statements to process error END;

Example:1 declare invalid_dept exception; begin delete from emp where deptno=&deptno; if sql%notfound then raise invalid_dept; end if; exception when invalid_dept then dbms_output.put_line('the deptnumber is not valid'); end; SQL> / Enter value for deptno: 10 old 4: delete from emp where deptno=&deptno; new 4: delete from emp where deptno=10; the deptnumber is not valid PL/SQL procedure successfully completed.

Вам также может понравиться