You are on page 1of 16

1) Write a program that displays the Netsal(Sal+comm) of an employee

entered at run time from EMP table.(Use NVL Function).


Declare
I emp%rowtype;
vnetsal number(10);
Begin
I.empno:=&eno;
select ename,sal,comm,deptno into I.ename,I.sal,I.comm,I.deptno from
emp
where empno=I.empno;
Vnetsal:=I.sal+nvl(I.comm,0);
display('The emp details are: Emp name is '||I.ename||',sal is '||
I.sal||',commission is '
||I.comm||',netsal is '||vnetsal||',deptno is '||I.deptno);
End;

1) Write a program that will prompt for a EMPNO,ENAME,SAL.


If EMPNO does not exists in EMP table then INSERT the
EMPNO,ENAME,SAL into the EMP table else UPDATE ENAME,SAL
for that EMPNO.

Create table<empdata>
(
EMPNO INT
EMPNAME VARCHAR2(10)
SAL NUMBER(10)
)
INSERT INTO empdata( EMPNO,EMPNAME,SAL)
VALUES(105,’SAI’,100000)
SELECT *FROM empdata
UPDATE empdata set sal=100000,ENAME=’sai’ where empno=eno;
END;
2) declare
n number:=&n;
 
begin
if mod(n,2)=0
then
dbms_output.put_line('number is even');
else
dbms_output.put_line('number is odd');
end if;
end;

Write program that prompts for EMPNO and update his salary
based on the below specification:
If the employee belongs to DEPTNO 10 then update his
salary by 10%.
If the employee belongs to DEPTNO 20 then update his
salary by 15%.
If the employee belongs to other DEPTNO then update his
salary with SALARY+COMM.

Declare
deptid number(2);
sal number(10,2);
begin
select salary,comm,Department_id into sal,deptid from Employees where
Employee_Id =105;
dbms_output_line(sal || ‘: ‘|| deptid);
if deptid =30 then
sal:= sal+sal*10/100;
elseif deptid=20 then
sal=sal+sal*15/100;
else
sal=sal+comm;
end if;
update Employees set salary =sal where Employee_id=105;
dbms_output_line(sal ||’:’ || deptid);
end;
4)

In sql Prompt create a table MYTABLE1 with RESULT as a


column of number datatype.
Write a program that will insert record in the mytable
from 1,2,3… to 10 values.
Do not insert record 6 and 8

Create table< mytable1>


(
Number int,
);
Desc mytable1;
INSERT INTO mytable1( number)
Values(1)
Select *from mytable1
Value(2)
Select *from mytable1
Value(3)
Select*from mytable1
Value(4)
Select*from mytable1
Value(5)
Select*from mytable1
Value(7)
Select*from mytable1
Value(9)
Select*from mytable1
Value(10)
Select*from mytable1
1)
DECLARE
z_employee employees%ROWTYPE;
BEGIN
SELECT *
INTO z_employee -- INTO clause always notifies only
single row can be fetch
FROM employees
WHERE employee_id = 149;

dbms_output.Put_line('Employee Details : ID:'


||z_employee.employee_id
||' Name: '
||z_employee.first_name
||' '
||z_employee.last_name
||' Salary: '
||z_employee.salary);
END;

Write a implicit cursor that will prompt for a JOB and


delete the employees working in that Job.
Store the number of records deleted in a session variable.

BEGIN
Write a program that displays the Top N salaries using
simple Explicit Cursors.

DECLARE
num NUMBER(3) := &p_num;
sal employees.salary%TYPE;
CURSOR emp_cursor IS
SELECT distinct salary
FROM employees
ORDER BY salary DESC;
BEGIN
OPEN emp_cursor;
FETCH emp_cursor INTO sal;
WHILE emp_cursor%ROWCOUNT <= num AND emp_cursor%FOUND
LOOP
INSERT INTO top_salaries (salary)
VALUES (sal);
FETCH emp_cursor INTO sal;
END LOOP;
CLOSE emp_cursor;
END;/
SELECT * FROM top_salaries;

Create a Simple Explicit Cursor that displays all the


employees who are earning more than the average salary of
the Emp Table.
DECLARE
CURSOR c_detail IS
  SELECT dept_name,d.dept_id,first_name,salary
              FROM department d JOIN emp_detail e
              ON e.dept_id = d.dept_id
              WHERE  salary > 48000;               
BEGIN
  FOR item IN c_detail
    LOOP
DBMS_OUTPUT.PUT_LINE(item.first_name||' '||
item.dept_name||' '||item.dept_id||' '||' '||item.salary);
    END LOOP;
END;

Create a Simple Explicit cursor that displays the


employees who earn more than 2000 and have joined the
company after 15-Jun-1981.
Print the output : <> earns <> and joined the
organization on <>.
.

DECLARE
CURSOR c_detail IS
  SELECT dept_name,d.dept_id,first_name,salary,joined
              FROM department d JOIN emp_detail e
              ON e.dept_id = d.dept_id
              WHERE  salary > 2000 and select
to_char(sysdate,15-‘jun’-1981’) from dual ;               
BEGIN
  FOR item IN c_detail
    LOOP
DBMS_OUTPUT.PUT_LINE(item.first_name||' '||
item.dept_name||' '||item.dept_id||' '||' '||item.salary);
    END LOOP;
END;
Create a Simple Explicit cursor that displays the hiredate
in the format DD-MM-RRRR and Day. Sort the records on DAY
starting from Saturday

DECLARE
CURSOR c_detail IS
  SELECT hire_date
              FROM department d JOIN emp_detail e
              ON e.dept_id = d.dept_id
               select to_char(sysdate,DD-MM-RRRR) FROMDUAL
DBMS_OUTPUT.PUT_LINE(hire_date)
END LOOP;
END;

Create a simple explicit cursor that displays the


employees names and commission amounts. If an employee
does not earn commission, show “No Commission" . Label the
column COMM.

SELECT last_name,
NVL(TO_CHAR(commission_pct), ’No Commission’)
COMM FROM employees;
Create a parameter cursor that takes DEPTNO as a parameter
and update the star column with ‘*’.
Every ‘*’ represent a 1000.
For example : If the employees salary is 5000 then update
star column with ‘*****’.
Use Parameter Cursor ,CURSOR WITH FOR LOOP and WHERE
CURRENT OF OPTION.

declare
cursor c_promote(v_job varchar2) is
select empno from emp
where job=v_job and sal>1000
for update of job,sal nowait;
begin
for emp_record in c_promote('CLERK') loop
Update emp set job='SR CLERK' , sal=sal+sal*0.1
where sal>1000 and job = v_job;
where empno=emp_record.empno;
update emp set sal=sal+sal*0.1
where empno=emp_record.empno;
end loop;
end;
Display entire record from the Dept table by passing a
DEPTNO.
Increase the Salary of employees working in deptno 10 by
15%, Deptno 20 by 15% and others by 5% Also display the
corresponding the employees working in that Dept.
Use a parameter Cursor , For Loop Cursor and Cursor with
Update clause.

declare
cursor sal_increase(v_dno number) is select empno,sal,ename,deptno from emp
where deptno=v_dno ;
emp_record sal_increase%rowtype;
begin
OPEN sal_increase(10);
LOOP
FETCH sal_increase INTO emp_record;
EXIT WHEN sal_increase%NOTFOUND;
update emp set sal=sal+sal*0.15 where empno=emp_record.empno;
DBMS_OUTPUT.PUT_LINE(emp_record.ename||' '||emp_record.deptno);
END LOOP;
CLOSE sal_increase;
OPEN sal_increase(20);
LOOP
FETCH sal_increase INTO emp_record;
EXIT WHEN sal_increase%NOTFOUND;
update emp set sal=sal+sal*0.15 where empno=emp_record.empno;
DBMS_OUTPUT.PUT_LINE(emp_record.ename||' '||emp_record.deptno);
END LOOP;
CLOSE sal_increase;
end;
Create a table MESSAGES table with a column Result of type
varchar2(1000)
Write a program that prompt for SAL and get the
corresponding ENAME from the EMP table. Do not use
Explicit Cursors.
If the value entered return one row then Insert into
MESSAGES table with the ENAME||’ ‘||SAL.
If the value returned is more than one row then Insert
into MESSAGES table as ‘More than one employee with a
salary <>.
If no value is returned then insert into MESSAGES table as
‘No Employee with that salary <>.
Any other error insert ‘Other Error’.
Use all NAMED EXCEPTION HANDLERS.

DECLARE
V_Ename emp.Ename%TYPE
V_SAL emp.SAL%TYPE
BEGIN
SELECT Ename,SAL
INTO V_ENAME,V_SAL
FROM EMP
WHERE Emp_Id=101;
Dbms_output_put_line(ENAME||’ ‘||SAL);
EXCEPTION
WHEN NO_DATA_FOUND THEN
Dbms_output.put_line(‘NO EMPLOYEE WITH THAT
SALARY’);
WHEN TOO_MANY_ROWS THEN
dbms_output.put_line(‘MORE THAN ONE EMPLOYEE
WITH THAT SALARY);
WHEN OTHERS THEN
Dbms_output.put_line(‘some error found’)
END;

Write a program that will prompt for Empno,ename,sal and insert them into the Emp table.

Use Unnamed system defined exception handler

CREATE OR REPLACE PROCEDURE add_Empdata


      (EmpnoIN NUMBER, e _name_in IN VARCHAR2,sal IN NUMBER)
IS
BEGIN
       INSERT INTO Empdata(Empno, E _name,SAL )
       VALUES (Empno in, E_Name in,SAL in );
EXCEPTION
       WHEN DUP_VAL_ON_INDEX THEN
 raise_application_error (-20001,'Empno');
       WHEN OTHERS THEN
raise_application_error (-20002,'An error occurred.');
END;

Create a stock table with columns PNO,PNAME,RATE AND TR_QTY.

Insert some records in this table.

Create a PL/SQL block that takes PNO,TR_TYPE and TR_QTY from the Stock Table.

If PNO is found and TR_TYPE is ‘R’ then update the TR_QTY with Old TR_QTY plus NEW TR_QTY.

If PNO exists and TR_TYPE is ‘I’ then update the TR_QTY with Old TR_QTY minus NEW TR_QTY.

Handle the user defined exceptions using USER DEFINED EXCEPTION HANDLERS.

Create a procedure to accept an empno as a input parameter and display the detail like
ename,sal and deptno.

PROCEDURE Get_emp_rec (Emp_number IN Emp_tab.Empno%TYPE,

Emp_ret OUT Emp_tab%ROWTYPE) IS

BEGIN

SELECT Empno, Ename, Job, Mgr, Hiredate, Sal, Comm, Deptno

INTO Emp_ret
FROM Emp_tab

WHERE Empno = Emp_number;

END

Create a function called Add_Num() that accept two paraemter of number type and return the
addition result in number type.

create or replace procedure add(n1 in int,n2 in int,result out int)


as
begin
result :=n1+n2;
end;

Create a procedure ADD_DEPT.

This procedure will take 3 IN arguments DEPTNO,DNAME,LOC. Insert these values into the
DEPT table.

Handle the exception if the user tries to insert a duplicate row using User Defined
Exception Handler.

create or replace procedure ADD_DEPT(DEPTNO in dept.deptno%type,


DNAME in dept.dname%type,LOC in dept.loc%type)
is
begin

insert into dept values(DEPTNO,DNAME,LOC);

exception

when dup_row _on_index


then
raise_application_error(-20001,'Value duplicated on row' );

end;
Create a procedure UPDATE_DEPT and takes 3 arguments DEPTNO,DNAME,LOC. Update the DNAME,LOC
for the corresponding DEPTNO.

Handle exception when the user enters DEPTNO that does not exits.

create or replace procedure ADD_DEPT(DEPTNO in dept.deptno%type,


DNAME in dept.dname%type,LOC in dept.loc%type)
is
begin

insert into dept values(DEPTNO,DNAME,LOC);

exception

when dup_deptno_on_index
then
raise_application_error(-20001,'Value duplicated on deptno' );

end;
Create a procedure DELETE_DEPT which takes 1 argument DEPTNO.
Delete the record for the corresponding DEPTNO.
Handle the required exception when the user enters a not existence
DEPTNo

WRAP the ADD_DEPT procedure and test your procedure.

create or replace procedure ADD_DEPT(DEPTNO in dept.deptno%type,


DNAME in dept.dname%type,LOC in dept.loc%type)
is
begin

insert into dept values(DEPTNO,DNAME,LOC);


END;

Create a Procedure that will insert into the EMP_TEST table the employees who working in a
given deptno and also update the employees salary in emp table by 20% in EMP table.

Use Parameter Cursor, For Loop Cursor, Update clause Cursor.

create procedure up()


begin

DECLARE v_employee_id INT;


DECLARE v_salary NUMERIC(8,2);
DECLARE v_last_emp INT DEFAULT 0;

declare emp_cur cursor for select empid,salary from employee where dept = 'IT' for update;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_last_emp=1;


START TRANSACTION;

open emp_cur;
emp_loop: LOOP

fetch emp_cur into v_employee_id, v_salary;


if v_last_emp then
leave emp_loop;
end if;

UPDATE employee
SET
salary = salary + (salary * 0.20)
WHERE
empid = v_employee_id;

end loop emp_loop;


close emp_cur;
set v_last_emp =0;
end;
Create a procedure GET_EMP that will take EMPNO(IN),ENAME(OUT),SAL(OUT). Handle the
required the Exceptions. Call the procedure from the SQLPROMPT by defining 2 session
variables and the output from these variables by passing 7788 EMPNO.

PROCEDURE Get_emp_names (Dept_num IN NUMBER) IS


Emp_name VARCHAR2(10);
CURSOR c1 (Depno NUMBER) IS
SELECT Ename FROM Emp_tab
WHERE deptno = Depno;
BEGIN
OPEN c1(Dept_num);
LOOP
FETCH c1 INTO Emp_name;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(Emp_name);
END LOOP;
CLOSE c1;
END;
Write a procedure that take an IN OUT argument for Mobile Number. Pass the Mobile Number as
9999999999 and the procedure should return (999)999-9999.

CREATE OR REPLACE PROCEDURE DISP_NUMBER(


BEGIN
select regexp_replace('9999999999','((…))(...)(-.....)(.)','\1-\2-\3-\4') from dual;
END;

Use the Deptree view to generate all the dependencies for DEPT table.

ELECT name, type, referenced_name, referenced_type


FROM sys.dba_dependencies
WHERE owner = 'JWARD';
Create a Function GET_JOB, that will return JOB of an Employee. Define a session variable
TITLE allowing a length of 35 characters. Invoke the function to return the value in the
session variable and print.

CREATE OR REPLACE
FUNCTION GET_JOB(
p_jobid IN jobs.job_id%TYPE)
RETURN VARCHAR2
AS
v_jobtitle jobs.job_title%TYPE;
BEGIN
SELECT job_title INTO v_jobtitle FROM jobs WHERE job_id=p_jobid;
RETURN v_jobtitle;
END GET_JOB;

VARIABLE b_title VARCHAR2(35)


set serveroutput on
DECLARE
P_JOBID VARCHAR2(10);
v_jobtitle VARCHAR2(200);
BEGIN
v_jobtitle := GET_JOB('SA_REP');
P_JOBID => P_JOBID;

DBMS_OUTPUT.PUT_LINE('The salary of job id'||' '||p_jobid||' '|| 'is'||' '||v_jobtitle);

:b_title := v_jobtitle;
END;

Create a function GET_ANNUAL_SAL that will calculate the annual salary of an Employee. Call
the function from a SELECT statement against the EMP table for employees in DEPTNO 30.

CREATE FUNCTION ANNUAL_COMP(EMPSalary employees.salary%type,)


RETURN NUMBER IS AnnualSalary employees.salary%type;
BEGIN
SELECT *FROM Emp
Where emp_DEPTNO=30
AnnualSalary:=EMPSALARY*12;
RETURN AnnualSalary;
END;

Assuming that there is no relationship in DEPT and EMP tables.

Create a function VALID_DEPTNO from the DEPT table for an specific DEPTNO. Return a BOOLEAN
value TRUE if DEPTNO exists.

Create a procedure ADD_EMP that will insert EMPNO, ENAME,SAL,DEPTNO in the EMP table. The
new row should be added in EMP table only if the function VALID_DEPTNO return TRUE else
alert the user with a message.

Create a PLSQL function CAL_REVERSE. This function will take a String and return reverse of
this string.(Do not used Reverse inbuilt function).

declare
str1 varchar2(50):='&str';
str2 varchar2(50);
len number;
i number;
 
begin
len:=length(str1);
for i in reverse 1..len
loop
str2:=str2 || substr(str1,i,1);
end loop;
dbms_output.put_line('Reverse of String is:'||str2);
end;
/

Create a function CAL_PCT that calculates an employee percentage of salary. Pass EMPNO and
percentage of salary to the function .
Call the function using EMP table . Use 16% as default parameter for percentage.

create or replace function calculate_increment


2 (incperc in number,
3 salary in number
4 )
5 return number
6 as
7 begin
8 return salary * incperc/100;
9 end;
10 /

Create a function that returns TRUE if the SAL is less than Average salary else return
FALSE.

Create a procedure REC_INSUPD that takes 3 arguments. EMPNO,ENAME,SAL. The user should
insert row and update the rows only if the new salary is less than avgsal.

Create a function CAL_LDATE that returns the DAY, MONTH DD,YYYY that the Employee joined
the company. Return in GERMAN.

CREATE OR REPLACE FUNCTION CAL_LDATE( Sysdate,


Format_Of_Date_and_Time AS Alias_Name
FROM Dual

Create a function CAL_WORDS that returns a numeric value in Words.

FUNCTION f_numbertoword(pn_number NUMBER) RETURN VARCHAR2 IS


f_numbertoword VARCHAR2(34);
BEGIN
f_numbertoword := REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(pn_number,
'0','zero '),
'1','one '),
'2','two '),
'3','three '),
'4','four '),
'5','five '),
'6','six '),
'7','seven '),
'8','eight '),
'9','nine ');
RETURN f_numbertoword;
END;

Wrap the function GET_JOB and then test using the USER_SOURCE

WRAPPING THE CODE:


CREATE OR REPLACE FUNCTION WRAP_SUM(P

You might also like