EX1::
posted on 02 Mar 2009 12:01 by igift4uemployee_id,first_name,department_name,salary
โดยมีตารางที่เกี่ยวข้องคือ employees,departments
ให้เพิ่มข้อมูลลงในตารางemp_deptโดยมีเงื่อนไขดังนี้
1.ใช้cursorในการนำข้อมูลมาเก็บ
2.ให้นำเฉพาะรายการที่หารสิบลงตัวเท่านั้น
3.ให้เขียนcursorทั้งสองรูปแบบ
4.การดึงค่าfirst_nameจะต้องเขียนเป็น function
5.โปรแกรมที่สร้างนี้จะต้องทำเป็นpackage
+(๐_๐)+++++++++++++++++++++++++++จบโจทย์
=เริ่มทำ=
----------------------------------------------------------------
1.create table emp_dept
sql>create table emp_dept
as (select e.employee_id,e.employee_name,d.department_name,e.employee_salary
from employees e,department d);
----------------------------------------------------------------
2.create package specification
sql>create or replace package gift is
function f_name return bolean;
procedure p_rec;
end gift;
----------------------------------------------------------------
3.create package body
sql>create or replace package body gift
is
function f_name return boolean
is
fn_name employees.first_name%type;
cursor c2 is select first_name from emp_dept;
begin
open c2;
loop
fetch c2 into fn_name;
Dbms_output.put_line('first name:' ||fn_name);
exit when c2%notfound;
end Loop;
return true;
exception when no_data_found then Dbms_output.put_line('no data');
return false;
close c2;
end f_name;
procedure p_rec
is
e_id employees.employee_id%type;
e_name employees.first_name%type;
d_name departments.department_name%type;
e_sal employees.salary%type;
cos number:=1;
cursor c1 is select e.employee_id,e.first_name,d.department_name,e.salary
from employees e , departments d
where e.department_id =d.department_id;
begin
open c1;
loop
fetch c1 into e_id, e_name, d_name,e_sal;
if mod(cos,10)=0 then
insert into emp_dept(employee_id,first_name,department_name,salary)
values (e_id,e_name,d_name,e_sal);
exit when c1%notfound;
dbms_output.put_line
('e_id : ' ||e_id|| ' ' ||'e_name:'||e_name||' ' ||'d_name:'||d_name||' '||'e_sal:'||' '||e_sal);
end if;
cos:=cos+1;
end loop;
close c1;
end p_rac;
end gift;
--------------------------end-----------------------------------