EX1::

posted on 02 Mar 2009 12:01 by igift4u
ให้สร้างตารางชื่อemp_dept โดยให้มีcolumnดังนี้
employee_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-----------------------------------

มีคนบอกให้ลองมาเช่าบ้านหลังนี้ดู เพื่อสร้างอะไรให้มีประโยชน์ต่อโลกใบนี้บ้าง อืม...น่าคิด
เพราะปกติจะไม่ค่อยเขียนบทความที่มีเนื้อหาสาระอะไรเท่าไหร่
ต้องมาลองดูกันแล้วล่ะว่าจะไปได้ดีแค่ไหน ^_____^

เรามาเริ่มEntry ของiGiftกับคำสั่งที่ใช้บ่อยๆใน sql*plus กัน

       c = เปลี่ยนข้อความในบรรทัด
     cle = เคลียร์บัฟเฟอร์ sql*plus
       a = เพิ่มข้อความที่เราพิมพ์ไปยังบรรทัดปัจจุบัน
    del = ลบข้อความในบรรทัดปัจจุบัน
        i = สร้างบรรทัดใหม่ต่อจากบรรทัดปัจจุบัน
        l = แสดงข้อความทั้งหมดในบัฟเฟอร์
       / = รันสคริปต์หรือข้อความทั้งหมดในบัฟเฟอร์
   get = อ่านไฟล์แล้วเก็บไว้ในบัฟเฟอร์
 desc = ดูรายละเอียดโครงสร้างตาราง
   sav = บันทึกข้อความทั้งหมดในบัฟเฟอร์ลงในไฟล์
    ed = แก้ไขข้อความในnotepad

ที่เหลือสามารถดูได้ด้วย help topic ก็จะแสดงคำสั่งทั้งออกมา

edit @ 25 Feb 2009 17:38:13 by iGift4u