cursor

Cursor  

Oracle engine uses a work area for its internal processing in order to execute an sql statement. This work area is private to sql operation and is called ³Cursor´. Data stored in side cursor is called ³Active Data Set´

Types of Cursor   Implicit Cursor Explicit Cursor I .

Implicit Cursor   If oracle engine opens a cursor for its internal processing then it known as implicit cursor. %ISOPEN %FOUND %NOT FOUND % ROW COUNT . Attributes of Implicit cursor.

:.returns number of row fetched from cursor.  %FOUND  %NOT FOUND  % ROW COUNT . :. :-Return true if record was fetched successfully other wise false.Return true if record was not fetched successfully other wise false.Implicit cursor attributes  %IS OPEN :-it returns true if cursor is open otherwise false.

 --EXAMPLE OF SQL%FOUND (IMPLICIT CURSORS) Begin update employee set salary=salary *0.put_line('employee no does not exist'). end if. End.put_line('employee record modified successfully'). Else dbms_output. .15 where emp_code = &emp_code. if sql%found then dbms_output.

end.15 where emp_code = &emp_code.put_line('employee record modified successfully'). . --EXAMPLE FOR SQL%NOTFOUND (IMPLICIT CURSORS) Begin update employee set salary = salary*0. else dbms_output. end if. if sql % not found then dbms_output.put_line('employee no does not exist').

elsedbms_output. end. EXAMPLE FOR SQL%ROWCOUNT (IMPLICIT CURSORS) Declare rows_affected char(4).put_line('There are no employees working as salesman'). Begin update employee set salary = salary*0. end if.put_line(rows_affected || 'employee records modified successfully').15 where job=µSALESMAN'. if sql%rowcount > 0 then dbms_output. . rows_affected := to_char(sql%rowcount).

Attributes of Explicit cursor.Explicit Cursor   If an user opens a cursor for processing of data then it is called ³Explicit Cursor´. %ISOPEN %FOUND %NOT FOUND % ROW COUNT .

 . % ROW COUNT:-returns number of row fetched from cursor.Attributes of Explicit cursor.  %FOUND  %NOT FOUND :-Return true if record was not fetched successfully other wise false. :-Return true if record was fetched successfully other wise false.  %IS OPEN :-it returns true if cursor is open otherwise false.

. 6. 3. 2.Explicit Cursor Management 1. Close the cursor. Open cursor. Process the data held in memory variable as required using loop. Fetch data from the cursor one row at a time into memory variables. 5. Declare a cursor mapped to a sql select statement that retrieves data for processing. Exit from the loop after processing is complete. 4.

Command to Control Cursor.        Fetch :-this command moves data from Active Data Set to memory variables. . Open :.it opens cursor.it closes the cursor. Close :. Syntax for cursor declaration cursor <name of cursor> is select statement.sal from emp where deptno=20. Example Cursor cur_emp is select empno.ename.

EXIT WHEN C1 % NOTFOUND. END.PUT_LINE('NAME : ' || ' ' || E_REC.PUT_LINE('SALARY: ' || ' ' | | E_REC. END LOOP. DBMS_OUTPUT. DBMS_OUTPUT.Example of explicit cursor DISPLAYING VALUE OF A TABLE DECLARE CURSOR C1 IS SELECT * FROM EMP.PUT_LINE('NUMBER: ' || ' ' || E_REC. E_REC EMP%ROWTYPE.EMPNO). DBMS_OUTPUT. CLOSE C1.SAL). LOOP FETCH C1 INTO E_REC.ENAME). . BEGIN OPEN C1.

..WRITE PL/SQL CODE FOR FOLLOWING PROBLEM  For finding sum of two number trough user input For Finding Sum of 100 numbers For finding Sum of odd numbers using user input. while loop    ..For loop For finding Sum of 100 odd number .

. c number.. c:=a+b. end. b:=&b.put_line('Sum of ' || a || ' and ' || b || ' is ' || c). Here & is used to take user input at runtime.. . dbms_output. begin a:=&a. b number..PL/SQL PROGRAMS             --ADDITION declare a number.

put_line('Sum between 1 to 100 is '||s1). a:=a+1. end loop. loop s1:=s1+a. exit when (a=100). s1 number default 0.--SUM OF 100 NUMBERS Declare a number. dbms_output. Begin a:=1. . End.

.2)=1 then sum1:=sum1+n.--SUM OF odd NUMBERS USING USER INPUT. endvalue number.. for n in 1.. endvalue loop if mod(n. n:=1. dbms_output. .for loop declare n number.put_line('sum = ' || sum1). begin endvalue:=&endvalue. end if end loop. sum1 number default 0. end.

sum1 number default 0. end loop. n:=1. begin endvalue:=&endvalue. . end.SUM OF 100 ODD NUMBER .put_line('Sum of odd numbers between 1 and ' || endvalue || ' is ' || sum1). while (n < endvalue) loop sum1:=sum1+n. dbms_output. endvalue number. WHILE LOOP declare n number.. n:=n+2.

views. The syntax for creating a synonym is: create [or replace] [public] synonym [schema .Synonym A synonym is an alternative name for objects such as tables. and other database objects.] object_name. stored procedures. .] synonym_name for [schema . sequences.

The object_name phrase is the name of the object for which you are creating the synonym. The public phrase means that the synonym is a public synonym and is accessible to all users.. Remember that the user must first have the appropriate privileges to the object to use the synonym. .    Replace phrase allows you to recreate the synonym (if it already exists) without having to issue a DROP synonym command.Synonym Contd.

Object can be one of the following: Table View Sequence stored procedure function .Synonym Contd..

Where app is the name of Schema where suppliers table is present. suppliers. .. create public synonym suppliers for app.Synonym Contd. suppliers. create or replace public synonym suppliers for app. Examples of Synonym 1. 3. 2.

DROPPING A SYNONYM    The syntax for dropping a synonym is: drop [public] synonym [schema . The force phrase will force Oracle to drop the synonym even if it has dependencies. . means it is in use.] synonym_name [force].

This drop statement would drop the synonym called suppliers that we defined earlier. .Example of Dropping Synonym Drop public synonym suppliers.

A sequence is an object in Oracle that is used to generate a number sequence. This can be useful when you need to create a unique number to act as a primary key. .Sequence  In Oracle. you can create an auto number field by using sequences.

The syntax for a sequence is:  CREATE SEQUENCE sequence_name MINVALUE value MAXVALUE value START WITH value INCREMENT BY value CACHE value. .

.For example:    CREATE SEQUENCE rollno MINVALUE 1 MAXVALUE 1000 START WITH 1 INCREMENT BY 1 CACHE 20.

NEXTVAL FROM dual. NEXTVAL ---------1 SELECT supplier_seq. NEXTVAL ---------2 SELECT supplier_seq.NEXTVAL FROM dual.NEXTVAL FROM dual.Example SELECT supplier_seq. NEXTVAL ---------4 DROP sequence supplier_seq. NEXTVAL ---------3 SELECT supplier_seq.NEXTVAL FROM dual. Sequence dropped. .