Assignment 3 - PLSQL
Assignment 3 - PLSQL
%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.
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;
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