You are on page 1of 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