Plsql-Hanfds On Assignment-Incomplete
Plsql-Hanfds On Assignment-Incomplete
Plsql-Hanfds On Assignment-Incomplete
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)
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;
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;
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.
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.
BEGIN
INTO Emp_ret
FROM Emp_tab
END
Create a function called Add_Num() that accept two paraemter of number type and return the
addition result in number type.
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.
exception
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.
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
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.
declare emp_cur cursor for select empid,salary from employee where dept = 'IT' for update;
open emp_cur;
emp_loop: LOOP
UPDATE employee
SET
salary = salary + (salary * 0.20)
WHERE
empid = v_employee_id;
Use the Deptree view to generate all the dependencies for DEPT table.
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;
: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 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 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.
Wrap the function GET_JOB and then test using the USER_SOURCE