0% found this document useful (0 votes)
23 views

Lab 12 Dbms

The document describes 4 tasks to learn about cursors in PL/SQL. Task 1 displays employee, manager names and salaries from a table using a scroll cursor. Task 2 displays employee names, jobs and salaries over 4000 not as clerks using a scroll cursor. Task 3 displays employee, manager, department names not reporting to Jones using a scroll cursor and fetch status check. Task 4 updates employee salaries with increments based on jobs using a scroll cursor and while loop.

Uploaded by

ARISHA 124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lab 12 Dbms

The document describes 4 tasks to learn about cursors in PL/SQL. Task 1 displays employee, manager names and salaries from a table using a scroll cursor. Task 2 displays employee names, jobs and salaries over 4000 not as clerks using a scroll cursor. Task 3 displays employee, manager, department names not reporting to Jones using a scroll cursor and fetch status check. Task 4 updates employee salaries with increments based on jobs using a scroll cursor and while loop.

Uploaded by

ARISHA 124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Arisha Fateh Section: C Roll Number: 2020F-BCE-117

LAB # 12
OBJECTIVE: To learn the cursors.
LAB TASKS:
NewEmployee Table:

TASK # 1: Write a PL/SQL program using cursor which displays all employees’ names,
employees’ salaries, their manager’s name and manager’s salaries from emp table.
SOLUTION:
DECLARE myCursor CURSOR SCROLL FOR
SELECT EName, Sal, MName, MSal FROM NewEmployee
OPEN myCursor
DECLARE @EName VARCHAR(50), @Sal int, @MName VARCHAR(50), @MSal int
FETCH LAST FROM myCursor into @EName, @Sal, @MName, @MSal
PRINT 'Employee name is: ' + @Ename + ' and their salary is: ' + CAST(@Sal AS
VARCHAR(50))
+ '. Manager name is: ' + @MName + ' and Manager salary is: ' + CAST(@MSal AS
VARCHAR(50))
CLOSE myCursor

DECLARE @EName VARCHAR(50), @Sal int, @MName VARCHAR(50), @MSal int


FETCH PRIOR FROM myCursor into @EName, @Sal, @MName, @MSal
PRINT 'Employee name is: ' + @Ename + ' and their salary is: ' + CAST(@Sal AS
VARCHAR(50))
+ '. Manager name is: ' + @MName + ' and Manager salary is: ' + CAST(@MSal AS
VARCHAR(50))

DECLARE @EName VARCHAR(50), @Sal int, @MName VARCHAR(50), @MSal int


FETCH ABSOLUTE 5 FROM myCursor into @EName, @Sal, @MName, @MSal
PRINT 'Employee name is: ' + @Ename + ' and their salary is: ' + CAST(@Sal AS
VARCHAR(50))
Page 1 of 4
Database Management Systems (CE-301L)
Name: Arisha Fateh Section: C Roll Number: 2020F-BCE-117

+ '. Manager name is: ' + @MName + ' and Manager salary is: ' + CAST(@MSal AS
VARCHAR(50))

TASK # 2: Write a PL/SQL program using cursor which displays all employees’ names, their
salaries and their jobs from emp table having salary greater than 4000 and are not CLERKS.
SOLUTION:
DECLARE newCursor CURSOR SCROLL FOR
SELECT EName, Job, Sal FROM NewEmployee WHERE Sal > 4000 AND Job != 'Clerk';
OPEN newCursor;
DECLARE @EName VARCHAR(50), @Job VARCHAR(50), @Sal int
FETCH FIRST FROM newCursor INTO @Ename, @Job, @Sal
PRINT 'Name is: ' + @EName + ' ,job is: ' + @Job + ' and salary is: ' + CAST(@Sal
AS VARCHAR(50))
CLOSE newCursor

DECLARE @EName VARCHAR(50), @Job VARCHAR(50), @Sal int


FETCH NEXT FROM newCursor INTO @Ename, @Job, @Sal
PRINT 'Name is: ' + @EName + ' ,job is: ' + @Job + ' and salary is: ' + CAST(@Sal
AS VARCHAR(50))

DECLARE @EName VARCHAR(50), @Job VARCHAR(50), @Sal int


FETCH LAST FROM newCursor INTO @Ename, @Job, @Sal
PRINT 'Name is: ' + @EName + ' ,job is: ' + @Job + ' and salary is: ' + CAST(@Sal
AS VARCHAR(50))

TASK # 3: Write a PL/SQL program using cursor which displays all employees’ names, their
departments’ names and their managers’names who don’t report to JONES. Cursor must use
%FOUND attribute to check the cursor.
SOLUTION: There is no ‘%FOUND’ in SQL Server So, you can use the @@FETCH_STATUS
function in a loop to check if there are more rows to fetch from the cursor
DECLARE thirdCursor CURSOR SCROLL FOR
SELECT EName, MName, DNo
FROM NewEmployee
WHERE MName != 'Jones';
OPEN thirdCursor
DECLARE @EName nvarchar(100), @MName nvarchar(100), @DNo int
WHILE(@@FETCH_STATUS = 0)
BEGIN
FETCH NEXT FROM thirdCursor INTO @EName, @MName, @DNo
PRINT @EName + ' works in Department number ' + CAST(@DNo AS
VARCHAR(50)) + ' and reports to ' + @MName
END
CLOSE thirdCursor

Page 2 of 4
Database Management Systems (CE-301L)
Name: Arisha Fateh Section: C Roll Number: 2020F-BCE-117

TASK # 4: Write a PL/SQL program using cursor WHILE LOOP which gives increments in
salaries to all the employees of emp table with respect to their jobs. If job = ‘CLERK’ then
increment must be 5% of the salary, If job = ‘SALESMAN’ then increment must be 10% of the
salary, If job = ‘ANALYST’ then increment must be 15% of the salary else increment must be
20% of the salary
SOLUTION:
DECLARE fourthCursor CURSOR SCROLL FOR
SELECT ENo, EName, Job, Sal
FROM NewEmployee

OPEN fourthCursor
DECLARE @ENo INT, @EName NVARCHAR(100), @Job NVARCHAR(50), @Sal INT
FETCH NEXT FROM fourthCursor INTO @ENo, @EName, @Job, @Sal

WHILE @@FETCH_STATUS = 0
BEGIN
IF @Job = 'Clerk'
BEGIN
SET @Sal = @Sal + @Sal * 0.05
END
ELSE IF @Job = 'Salesman'
BEGIN
SET @Sal = @Sal + @Sal * 0.1
END
ELSE IF @Job = 'Analyst'
BEGIN
SET @Sal = @Sal + @Sal * 0.15
END
ELSE
BEGIN
SET @Sal = @Sal + @Sal * 0.2
END
UPDATE NewEmployee
SET Sal = @Sal
WHERE ENo = @ENo

Page 3 of 4
Database Management Systems (CE-301L)
Name: Arisha Fateh Section: C Roll Number: 2020F-BCE-117

PRINT @EName + ' Who works as a ' + @Job + ' ,your incremented salary is: ' +
CAST(@Sal AS VARCHAR(10))
FETCH NEXT FROM fourthCursor INTO @ENo, @EName, @Job, @Sal
END
CLOSE fourthCursor

Page 4 of 4
Database Management Systems (CE-301L)

You might also like