Sie sind auf Seite 1von 8

--program to calculate annual salary of given employee-declare

v_fname employees.first_name%type;
v_lname varchar2(10);
v_salary number(9,2);
v_job_id varchar2(10);
v_annsal number(9,2);
begin
select first_name,last_name,salary,job_id
into v_fname,v_lname,v_salary,v_job_id
from employees where employee_id=100;
v_annsal:=v_salary*12;
dbms_output.put_line('the details are as follows:');
dbms_output.put_line('Employee name:'||v_fname||v_lname);
dbms_output.put_line('salary:'||v_salary);
dbms_output.put_line('job_id:'||v_job_id);
dbms_output.put_line('Annual salary:'||v_annsal);
end;
/
****************************************************
declare
empname varchar2(20):='PATRICK';
BEGIN
declare
ename varchar2(20):='Mike';
BEGIN
dbms_output.put_line('Name:'||ename);
dbms_output.put_line('Name:'||empname);
END;
END;
/
********************************************
<<outer>>
declare
ename varchar2(20):='PATRICK';
BEGIN
declare
ename varchar2(20):='Mike';
BEGIN
dbms_output.put_line('Name:'||ename);
dbms_output.put_line('Name:'||outer.ename);
END;
END;
/
*********************************************
<<outer>>
declare
ename varchar2(20):='PATRICK';
BEGIN
<<inner>>
declare
ename varchar2(20):='Mike';
BEGIN
dbms_output.put_line('Name:'||ename);
dbms_output.put_line('Name:'||outer.ename);
END;
dbms_output.put_line('Name:'||inner.ename);

END;
/
********************************************
declare
v_empid number(3):=102;
BEGIN
select salary*12 into :annualsal from employees where employee_id=v_empid;
dbms_output.put_line('done!');
END;
/
*********************************************
declare
v_empid number(3):=102;
BEGIN
select first_name||last_name into :fullname from employees where employee_id=v_
empid;
dbms_output.put_line('done!');
END;
/
************************************************
declare
v_empid number(3):=100;
v_annsal number(9,2);
BEGIN
select salary*12 into v_annsal from employees where employee_id=v_empid;
if(v_annsal>20000) then
dbms_output.put_line('tax applicable!');
else
dbms_output.put_line('no tax!');
end if;
dbms_output.put_line('done!');
END;
/
***********************************************
--obtain the salary for a given employee-declare
v_empid number(3):=100;
v_sal number(9,2);
comment varchar2(30);
BEGIN
select salary into v_sal from employees where employee_id=v_empid;
if(v_sal>1 and v_sal<10000) then
comment:='THE GARDE IS C';
elsif(v_sal>10000 and v_sal<20000) then
comment:='THE GRADE IS B';
else
comment:='THE GRADE IS A';
end if;
dbms_output.put_line(comment);
dbms_output.put_line('done!');
END;
/
***************************************************
--obtain the salary for a given employee--

declare
grade char(1):='A';
appraisal varchar2(20);
BEGIN
appraisal:=
CASE grade
when 'A' then 'EXCELLENT'
when 'B' then 'VERY GOOD'
when 'C' then 'GOOD'
else 'NO SUCH GRADE!'
END;
dbms_output.put_line('grade:'||grade||'appraisal:'||appraisal);
dbms_output.put_line('done!');
END;
/
****************************************************
--for loop-DECLARE
counter number(2);
BEGIN
for counter in 1..5 loop
dbms_output.put_line(counter);
END LOOP;
END;
/
****************************************************
--alternate for loop method-DECLARE
counter number(2):=1;
BEGIN
for i in counter..5 loop
dbms_output.put_line(i);
END LOOP;
END;
/
*****************************************************
--for loop-DECLARE
counter number(2):=1;
BEGIN
for i in REVERSE counter..5 loop
dbms_output.put_line(i);
END LOOP;
END;
/
*****************************************************
--basic loop-DECLARE
i number(2):=1;
BEGIN
loop
dbms_output.put_line(i);
i:=i+1;

exit when i=5;


end loop;
END;
/
********************************************************
--while loop-DECLARE
c number(2):=0;
BEGIN
while c<= 10 LOOP
c:=c+1;
dbms_output.put_line('THE VALUE OF C IS:'||c);
END LOOP;
END;
/
*********************************************************
DECLARE
v_last_name employees.last_name%type;
BEGIN
select last_name into v_last_name from employees where first_name='Neena';
dbms_output.put_line('Neena''s last name is:'||v_last_name);
END;
/
************************************************************
DECLARE
v_last_name employees.last_name%type;
BEGIN
select last_name into v_last_name from employees where first_name='John';
dbms_output.put_line('Neena''s last name is:'||v_last_name);
EXCEPTION when TOO_MANY_ROWS then
dbms_output.put_line('More than 1 record is fetched.Pls check condition!');
END;
/
************************************************************
--DEFAULT CASE-DECLARE
v_last_name employees.last_name%type;
BEGIN
select last_name into v_last_name from employees where first_name='Kavya';
dbms_output.put_line('John''s last name is:'||v_last_name);
EXCEPTION when TOO_MANY_ROWS then
dbms_output.put_line('More than 1 record is fetched.Pls check condition!');
when NO_DATA_FOUND then
dbms_output.put_line('No data is available for query. Pls check condition');
when OTHERS then
NULL;
END;
/
***************************************************************
--procedure-create or replace procedure prc_my_first
as

v_my_name varchar2(2):='LN';
BEGIN
dbms_output.put_line('Hello'||v_my_name);
EXCEPTION when Others then
NULL;
END prc_my_first;
/
*****************************************************************
--IN PARAMETER-create or replace procedure prc_calculate_total_fees
(p_course_id IN varchar2)
is
v_total_fees number(15);
begin
--calculate total fees-select base_fees+special_fees into v_total_fees
from course_fees where
course_code=p_course_id;
--print total fees-dbms_output.put_line('total fees for course id'||'is'||v_total_fees);
exception
when no_data_found then
dbms_output.put_line('course id:'||p_course_id||'is invalid');
end prc_calculate_total_fees;
/
**********************************************************************
--OUT PARAMETER-create or replace procedure prc_calculate_total_fees
(p_course_id IN varchar2,p_fees OUT number)
is
begin
--raised fees-select base_fees+(base_fees*0.25) into p_fees
from course_fees where
course_code=p_course_id;
exception
when no_data_found then
dbms_output.put_line('course id:'||p_course_id||'is invalid');
end prc_calculate_total_fees;
/
***********************************************************************
--INOUT PARAMETER-create or replace procedure prc_course_msg
(p_course_id in out varchar2)
is
name_course varchar2(45);
begin
select course_name into name_course
from course_info where
course_code=p_course_id;
p_course_id:='The course code of'||name_course||'is:'||p_course_id;
exception
when no_data_found then
dbms_output.put_line('course id does not exist');
end prc_course_msg;
/
************************************************************************
create or replace function fn_calculate_discount
(course_id in varchar2)

return number
is
v_discount number(10);
v_total_fees number(10);
begin
select base_fees,discount into v_total_fees,v_discount
from course_fees
where course_code=course_id;
return v_total_fees*(v_discount/100);
end fn_calculate_discount;
/
*******************************************************************
begin
dbms_output.put_line(fn_calculate_discount(2));
end;
/
********************************************************************
--package specification-create or replace package course_package as
PROCEDURE prc_calculate_tota_fees(p_course_id IN varchar2,p_fees OUedT number);
function fn_calculate_discount(course_id in varchar2) return number;
end course_package;
/
********************************************************************
--package body-create or replace package body course_package as
PROCEDURE prc_calculate_total_fees(p_course_id IN varchar2,p_fees OUT number)
is
begin
--raised fees
select base_fees+(base_fees*0.25)into p_fees from course_fees where course_code=
p_course_id;
exception
WHEN NO_DATA_FOUND then
dbms_output.put_line('course_id:'||p_course_id||'is invalid');
end prc_calculate_total_fees;
--fucntion-function fn_calculate_discount(course_id in varchar2)
return number
is
v_discount number(10);
v_total_fees number(10);
begin
select base_fees,discount into v_total_fees,v_discount
from course_fees
where course_code=course_id;
return v_total_fees*(v_discount/100);
end fn_calculate_discount;
end course_package;
/
****************************************************************************
DECLARE
emp_rec employees%ROWTYPE;
BEGIN
select * into emp_rec from employees where employee_id=101;
dbms_output.put_line(emp_rec.employee_id);

dbms_output.put_line(emp_rec.last_name);
dbms_output.put_line(emp_rec.job_id);
dbms_output.put_line(emp_rec.salary);
dbms_output.put_line(emp_rec.department_id);
exception
when no_data_found then
dbms_output.put_line('The employee does not exist');
dbms_output.put_line('try again with diff number');
when others then
NULL;
END;
/
******************************************************************************
--alternate method-DECLARE
emp_rec employees%ROWTYPE;
BEGIN
select * into emp_rec from employees where employee_id=&employee_id;
dbms_output.put_line(emp_rec.employee_id);
dbms_output.put_line(emp_rec.last_name);
dbms_output.put_line(emp_rec.job_id);
dbms_output.put_line(emp_rec.salary);
dbms_output.put_line(emp_rec.department_id);
exception
when no_data_found then
dbms_output.put_line('The employee does not exist');
dbms_output.put_line('try again with diff number');
when others then
NULL;
END;
/
*******************************************************************************
--inserting data into table using record-DECLARE
emp_rec employees%ROWTYPE;
BEGIN
select * into emp_rec from employees where employee_id=&employee_id;
insert into sales_info values emp_rec;
exception
when no_data_found then
dbms_output.put_line('The employee does not exist');
dbms_output.put_line('try again with diff number');
when others then
NULL;
* END;
********************************************************************************
**
--Cursor-DECLARE
cursor empcursor is
select employee_id,job_id,salary from employees;
empid employees.employee_id%type;
job employees.job_id%type;
salary employees.salary%type;
BEGIN
open empcursor;
LOOP
FETCH empcursor into empid,job,salary;
exit when empcursor%notfound;

dbms_ouTput.put_line('REC:'||' '||empid||' '||job||' '||salary);


END LOOP;
close empcursor;
END;
/
********************************************************************************
*

Das könnte Ihnen auch gefallen