Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Experiment: Programs Related Function: Steps For Experiment/practical: (A) : ANSWER

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

Experiment: Programs related function

Student Name: Mahesh Sherma                                                  UID: 21MCI1127

Branch:  MCA (AI&ML)                                                            Section/Group: B

Semester:   1st                                                                     Date of Performance: 07-10-2021

Subject Name PL/SQL                                                           Subject Code: 21CAP-627

1) Task to be done:

WRITE A PROGRAM TO CREATE FUNCTION FOR FOLLOWING OPERATIONS IN PLSQL.

 CREATE A FUNCTION TO CALCULATE THE INCENTIVE ON A TARGET ACHIEVED AND


DISPLAY THE MESSAGE EITHER THE RECORD UPDATED OR NOT.

 CREATE A FUNCTION IN PL/SQL TO UPDATE THE SALARY OF A SPECIFC EMPLOYEE BY


8% IF THE SALARY EXCEEDS THE MID RANGE OF THE SALARY AGAINST THIS JOB AND
UPDATE UP TO MID RANGE IF THE SALARY IS LESS THAN THE MID RANGE OF THE
SALARY, AND DISPLAY A SUITABLE MESSAGE
2) Steps for experiment/practical:

(A): ANSWER :-

CREATE TABLE EMP(

EMP_ID NUMBER(10),

EMP_NAME VARCHAR2(20),

SALARY NUMBER(20),

MIN_SALARY NUMBER(20),

MAX_SALARY NUMBER(20)

);
INSERT INTO EMP VALUES('1', 'VIJAY', '25000', '20000','50000');

INSERT INTO EMP VALUES('2', 'MAHESH', '23000', '20000','50000');

INSERT INTO EMP VALUES('3', 'SURESH', '21000', '20000','50000');

INSERT INTO EMP VALUES('4', 'RAMESH', '20000', '20000','50000');

INSERT INTO EMP VALUES('5', 'RAJ', '25000', '20000','50000');

DECLARE

CREATE OR REPLACE FUNCTION FUNCTION1 (

SAL_ACHIEVE NUMBER,

TARGET_QTY NUMBER,

EMP_ID NUMBER

RETURN NUMBER

IS

INCENTIVE NUMBER := 0;

UPDATED VARCHAR2(3) := 'NO';

BEGIN

IF SAL_ACHIEVE > (TARGET_QTY + 200) THEN

INCENTIVE := (SAL_ACHIEVE - TARGET_QTY)/4;

UPDATE EMP

SET SALARY = SALARY + INCENTIVE

WHERE EMP_ID = EMP_ID;


UPDATED := 'YES';

DBMS_OUTPUT.PUT_LINE (

'TABLE UPDATED? ' || UPDATED || ', ' ||

'INCENTIVE = ' || INCENTIVE || '.'

);

RETURN 1;

END IF;

END ;

DECLARE

RES NUMBER;

BEGIN

RES:= FUNCTION1(23000, 20000, 4);

END;

Output

select * from emp;


After function call :-

(B) : Answer:-
CREATE TABLE EMPLOYEE(
EMP_ID VARCHAR(15),
EMP_NAME VARCHAR(30),
EMP_AGE NUMBER(2),
EMP_CITY VARCHAR(30),
EMP_SALARY NUMBER(10),
EMP_MIN_SALARY NUMBER(10),
EMP_MAX_SALARY NUMBER(10)
);

CREATE OR REPLACE PROCEDURE "INSERTRECORD" (EMP_ID IN VARCHAR,EMP_NAME IN


VARCHAR,EMP_AGE IN NUMBER,EMP_CITY IN VARCHAR,EMP_SALARY IN
NUMBER,EMP_MIN_SALARY IN NUMBER,EMP_MAX_SALARY IN NUMBER)
IS
BEGIN
INSERT INTO EMPLOYEE
VALUES(EMP_ID,EMP_NAME,EMP_AGE,EMP_CITY,EMP_SALARY,EMP_MIN_SALARY,EMP_MAX_
SALARY);
END;

BEGIN
INSERTRECORD('21MCI1068','AKHAND PRATAP SINGH',22,'AGRA',50000,60000,100000);
INSERTRECORD('21MCI1001','ADITYA',20,'MATHURA',85000,60000,100000);
INSERTRECORD('21MCI1002','AMAN',23,'JAMMU',90000,60000,100000);
INSERTRECORD('21MCI1003','NIKHIL',21,'JAIPUR',55000,60000,100000);
INSERTRECORD('21MCI1004','DEVESH',25,'JALANDHAR',70000,60000,100000);
INSERTRECORD('21MCI1005','TAPO',26,'DHAKA BANGLADESH',75000,60000,100000);
INSERTRECORD('21MCI1006','GOPAL',27,'NEW DELHI',90000,60000,100000);
INSERTRECORD('21MCI1007','VISHAL',23,'AHMEDABAD',80000,60000,100000);
INSERTRECORD('21MCI1008','RAJESH',24,'BENGALURU',85000,60000,100000);
INSERTRECORD('21MCI1009','ADARSH',24,'NAGPUR',65000,60000,100000);
END;

SELECT * FROM EMPLOYEE;


EMP_ID EMP_NAME EMP_AGE EMP_CITY EMP_SALARY EMP_MIN_SALARY EMP_MAX_SALARY

AKHAND PRATAP
21MCI1068 22 AGRA 50000 60000 100000
SINGH
21MCI1001 ADITYA 20 MATHURA 85000 60000 100000
21MCI1002 AMAN 23 JAMMU 90000 60000 100000
21MCI1003 NIKHIL 21 JAIPUR 55000 60000 100000
21MCI1004 DEVESH 25 JALANDHAR 70000 60000 100000
21MCI1005 TAPO 26 DHAKA BANGLADESH 75000 60000 100000
21MCI1006 GOPAL 27 NEW DELHI 90000 60000 100000
21MCI1007 VISHAL 23 AHMEDABAD 80000 60000 100000
21MCI1008 RAJESH 24 BENGALURU 85000 60000 100000
21MCI1009 ADARSH 24 NAGPUR 65000 60000 100000

CREATE OR REPLACE FUNCTION FUNCTION2(TMP_EMP_ID VARCHAR)


RETURN NUMBER
IS
MIN_SALARY NUMBER(6,0);
MAX_SALARY NUMBER(6,0);
MID_SALARY NUMBER(8,2);
TMP_SALARY EMPLOYEE.EMP_SALARY%TYPE;
TMP_EMP_NAME EMPLOYEE.EMP_NAME%TYPE;
BEGIN
SELECT EMP_MIN_SALARY,EMP_MAX_SALARY,EMP_SALARY,EMP_NAME INTO
MIN_SALARY,MAX_SALARY,TMP_SALARY,TMP_EMP_NAME FROM EMPLOYEE WHERE EMP_ID =
TMP_EMP_ID;
MID_SALARY := (MIN_SALARY+MAX_SALARY)/2;
IF TMP_SALARY < MID_SALARY THEN
UPDATE EMPLOYEE SET EMP_SALARY = MID_SALARY WHERE EMP_ID = TMP_EMP_ID;
ELSE
UPDATE EMPLOYEE SET EMP_SALARY = EMP_SALARY + EMP_SALARY*8/100 WHERE EMP_ID =
TMP_EMP_ID;
END IF;

IF TMP_SALARY > MID_SALARY THEN


DBMS_OUTPUT.PUT_LINE('The employee '||TMP_EMP_NAME||' ID ' || TO_CHAR(TMP_EMP_ID) ||
' works in salary ' || TO_CHAR(TMP_SALARY) ||
' which is higher than mid-range of salary ' || TO_CHAR(MID_SALARY));
ELSIF TMP_SALARY < MID_SALARY THEN
DBMS_OUTPUT.PUT_LINE('The employee '||TMP_EMP_NAME||' ID ' || TO_CHAR(TMP_EMP_ID) ||
' works in salary ' || TO_CHAR(TMP_SALARY) ||
' which is lower than mid-range of salary ' || TO_CHAR(MID_SALARY));
ELSE
DBMS_OUTPUT.PUT_LINE('The employee '||TMP_EMP_NAME||' ID ' || TO_CHAR(TMP_EMP_ID) ||
' works in salary ' || TO_CHAR(TMP_SALARY) ||
' which is equal to the mid-range of salary ' || TO_CHAR(MID_SALARY));
END IF;
RETURN 1;
END;
/

DECLARE
RES NUMBER;
TMP_EMP_ID VARCHAR(15) := '21MCI1068';
BEGIN
RES := FUNCTION2(TMP_EMP_ID);
END;

Statement processed.
The employee ADITYA ID 21MCI1001 works in salary 85000 which is higher than mid-range of salary 80000

Now, If you check salary will be updated, As salary is higher than mid range, salary will be increased by 8%.

1) Output

4) Learning outcomes (What I have learnt):


1. I have learn create a function with parameter

2. Calling of function

3. Perform some operation on table

Evaluation Grid:

Sr. No. Parameters Marks Obtained Maximum Marks


1. Demonstration and Performance 5
(Pre Lab Quiz)
2. Worksheet 10
3. Post Lab Quiz 5

You might also like