Vous êtes sur la page 1sur 4

Assignment-3

%type attribute:
The %type attribute is used in the declaration of a variable when the variable’s attributes must be
picked up from a column in a particular table.

Syntax: variable tablename.columnname%type;


Example: salary emp.sal%type.
The variable salary takes the type of the column sal in the emp table i.e., salary is now a number
type
variable with same attributes as of column sal in the emp table.
Cursor:
Oracle DBA uses a work area for its internal processing. This work area is private to SQL’s
operations and is called a Cursor. The data stored in the cursor is called the Active Data Set.
The size of the cursor in memory is the size required to hold the number of rows in the Active
Data Set. Oracle has a predefined area in memory set aside, within which it opens cursors.
Example: The salary of all the employees in dept no 20 has to be incremented and those details
should be inserted in another table which already exists. This is done with the help of cursors.

Declaring a Cursor:
Every cursor should be associated with a cursor name and an SQL statement.
Syntax : CURSOR cursorname IS
SQL statement.
Example: DECLARE
CURSOR c_emp IS
select empno,sal from emp
where deptno=20;

Opening a Cursor:
Opening a cursor executes the query and identifies the active set that contains all the rows, which
meet the query search criteria.
Syntax: OPEN cursorname;
Example :
DECLARE
CURSOR c_emp IS
select empno,sal from emp
where deptno=20;
BEGIN
OPEN c_emp;

Fetching a record from the cursor:


The fetch statement retrieves the rows from the active set to the variables one at a time. Each
time the fetch
is executed, the focus of the DBA cursor advances to the next row in the Active Set. Fetch
statement should
be used along with any loop statement (Loop End loop, while or for).
Syntax: FETCH cursorname INTO variable1,variable2,……
Example :
DECLARE
CURSOR c_emp IS
select empno,sal from emp
where deptno=20;
emp_sal emp.sal%type;
emp_no emp.empno%type;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp into emp_no,emp_sal;
UPDATE emp SET sal=emp_sal+(emp_sal*0.05)
WHERE empno=emp_no;
INSERT new_emp VALUES(emp_no,emp_sal);
END LOOP;
COMMIT;
END;
Closing a cursor:
The close statement disables the cursor and the active set becomes undefined. This will release the
memory occupied by the cursor and its Data Set.
Syntax: CLOSE cursorname
Example : In the previous example add the CLOSE statement after the COMMIT statement.
The above example would result in an infinite loop if the following cursor attributes are not used.
Cursor Attributes:
%NOTFOUND : evaluates to true, if the last fetch has failed because no more rows were available.
Evaluates to false, if the last fetch returned a row.
%FOUND :is the logical opposite of %NOTFOUND
%ISOPEN : evaluates to true, if an explicit cursor is open; or to false if it is closed.
%ROWCOUNT : returns the number of rows fetched from the active data set.
How to use the Cursor Attributes?
Implicit Cursor Attributes: If the above mentioned attributes of a cursor are used with latest used SQL
statement. It should be used like
EXIT WHEN SQL%NOTFOUND ;
Explicit Cursor Attributes: This helps us to keep track of the ‘Current’ status of the cursor. It should be
used like
IF cursorname%ISOPEN THEN
……….
END IF.
Example:
DECLARE
CURSOR c_emp IS
select empno,sal from emp
where deptno=20;
emp_sal emp.sal%type;
emp_no emp.empno%type;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp into emp_no,emp_sal;
EXIT WHEN c_emp%NOTFOUND; (explicit )
/*EXIT WHEN SQL%NOTFOUND; (implicit) This statement can also be used */
EXIT WHEN SQL%NOTFOUND; (implicit)
WHERE empno=emp_no;
INSERT new_emp VALUES(emp_no,emp_sal);
END LOOP;
COMMIT;
ClOSE c_emp;
End;
Cursor For Loops:
A cursor for loop implicitly declares its loop index as a %rowtype record, opens a cursor, repeatedly
fetches rows of values from the active set into terms in the record, and closes the cursor when all rows
have been processed.

Syntax :
FOR cursor_loop_name IN cursor_name
LOOP
……
END LOOP;
The sequence of statement inside the loop is executed once for every row that satisfies the query associated with the
cursor. Note the dot notation used in the example.
Example:
DECLARE
CURSOR c_emp IS
select empno,sal from emp
where deptno=20;
BEGIN
OPEN c_emp;
FOR loop_cursor in c_emp
LOOP
UPDATE emp SET sal=loop_cursor.sal+( loop_cursor.*0.05)
WHERE empno= loop_cursor.empno;
END LOOP;
COMMIT;
CLOSE c_emp;
END;

Questions
1. Select and print the salary of Mr. SMITH using a PL/SQL code.
2. Get empno as input and check whether the salary is less than 3000, if this condition is
true, then hike the salary by 10% and update in the table. It must also display a message
like "Mr. MILLER’s Salary is hiked by 10%" appropriately.
3. Get deptno as input and print the number of employees working in that particular
department using cursors.
4. Get department no as input and give a hike of 10% salary to all the employees belonging
to that department using cursors.
5. Print the highest salary in each department using cursors.

The table EMP is used to store information about employees. For the attributes, the following
data types are defined:
EMPNO :number(4), ENAME :varchar2(30 ), JOB: char(10 ), MGR: number(4),
HIREDATE :date, SAL :number(7 ,2), DEPTNO :number(2)
EMPNO ENAM JOB MGR H IR E DA T E SAL DE P T N O
E
7369 S M IT H CLERK 7902 1 7 -DE C -8 0 800 20
7499 S M IT H SALESMAN 7698 20 -F E B -8 1 1600 30
7521 WARD SALESMAN 7698 22-F E B -81 1250 30
7698 BLAKE MANAGER NULL 0 1 -M A Y -8 3850 30
1
7902 FORD ANALYST 7599 0 3-DE C -8 1 3000 10
The table DEPT stores information about departments (number, name, and location)
DEPTNO DNAME LOC
10 STORE CHICAGO
20 RESEARCH DALLAS
30 SALES NEWYORK
40 MARKETTING BOSTON

Vous aimerez peut-être aussi