SlideShare uma empresa Scribd logo
1 de 110
Baixar para ler offline
SQL - PL ‫دسٔط‬   2
SQL - PL ‫دسٔط‬   3
SQL - PL ‫دسٔط‬   4
‫‪Declare‬‬
‫ُْا حعشٌف انًخغٍشاث ٔ انًؤششاث‬
‫‪Begin‬‬
‫‪ Body‬حق انبشَايح‬

‫‪Exception‬‬
‫يعاندت األخطاء‬

‫;‪end‬‬

             ‫دسٔط ‪SQL - PL‬‬       ‫5‬
‫يالحظت ْايت :‬

                           ‫قغى ‪ٔexception‬قغى ‪declare‬‬
                  ‫ًْا اخخٍاسٌٍ أي ال ٌشخشط ٔخٕدًْا‬


‫أي إرا كاٌ ال ٌٕخذ نذٌك حعشٌف يخغٍشاث ال حغخخذو ‪Declare‬‬


      ‫ٔإرا كُج ال حخعايم يع األخطاء ال حغخخذو ‪Exception‬‬




           ‫دسٔط ‪SQL - PL‬‬                                  ‫6‬
name :='ahmed khaled';
 v:=15;

v_x number;                        ٍٍ‫ٌدب ٔضع انُقطخ‬
v_y number(10);
                                        = ‫قبم‬
v_z number(8,2) := 55.22;
name varchar2(50);
v_lname varchar2(20) :='Hilal';
v_bdate date;
is_married boolean :=false;
                   SQL - PL ‫دسٔط‬                      7
type% : ً‫خاصيح ٔٛع اٌحم‬




declare
      v_did departments . department_id % type ;




               SQL - PL ‫دسٔط‬                       8
‫: ‪% rowtype‬‬
 ‫يدًٕعت يخغٍشاث، ٔحغخخذو نخأعٍظ يصفٕفت يٍ‬
 ‫انًخغٍشاث يبٍُت عهى األعًذة انًٕخٕدة فً يؤشش‬
                                ‫يا أٔ خذٔل يا‬

‫‪declare‬‬
       ‫‪v_rec departments‬‬     ‫; ‪% rowtype‬‬




             ‫دسٔط ‪SQL - PL‬‬                      ‫9‬
dbms_output.put_line(          );


dbms_output.put_line(v_y);
dbms_output.put_line('###########');
dbms_output.put_line('The name is : '|| name);


  ٌٍ‫انًٕخذة ضًٍ عًهت انطباعت ًْ نهٕصم بٍٍ انخعبٍش‬   || ‫فائذة‬

                SQL - PL ‫دسٔط‬                                  10
declare
          v_did number;
          v_dname varchar2(50);
          v_mid number;
          v_lid number;
begin
          select department_id,department_name,manager_id,location_id
          into v_did,v_dname,v_mid,v_lid
          from departments
          where department_id=10;

          dbms_output.put_line('The recors is : ');
          dbms_output.put_line('ID : '|| v_did);
          dbms_output.put_line('Name : '|| v_dname);
          dbms_output.put_line('Manager: '||v_mid);
          dbms_output.put_line('Location : '|| v_lid);
end ;
                       SQL - PL ‫دسٔط‬                                    11
‫ حغخخذو الخخٍاس األعًذة ٔانبٍاَاث انخً َحخاخٓا‬select

begin
        select department_id , department_name , manager_id , location_id

        Declare ً‫ حغخخذو الخخٍاس األعًذة ٌٚىٓ يٍ خالل ياحى حعشٌفّ ف‬into


         into v_did , v_dname,v_mid , v_lid

                 ‫حغخخذو نخحذٌذ اندذٔل انزي عُغخشخع يُّ انبٍاَاث‬

         from departments
         where department_id=10;

                    SQL - PL ‫دسٔط‬                                              12
declare
            v_did number;
            v_dname varchar2(50);
            v_mid number;
            v_lid number;

 begin
            v_did :=666;
            v_dname :='Imam UN';
            v_mid :=105;
            v_lid :=1700;

           insert into departments
( department_id,department_name, manager_id , location_id)
           values(v_did   , v_dname , v_mid , v_lid);
end;

                          SQL - PL ‫دسٔط‬                      13
declare
          v_did departments.department_id%type;
          v_dname departments.department_name%type;
          v_mid departments.manager_id%type;
begin
          select department_id,department_name,manager_id
          into v_did,v_dname,v_mid
          from departments
          where department_id=10;

          dbms_output.put_line('The recors is : ');
          dbms_output.put_line('ID : '|| v_did);
          dbms_output.put_line('Name : '|| v_dname);
          dbms_output.put_line('Manager: '||v_mid);
end ;



                       SQL - PL ‫دسٔط‬                        14
declare
          v_did departments.department_id %type;

          v_dname departments.department_name %type;
          v_mid departments.manager_id %type;




                     SQL - PL ‫دسٔط‬                     15
begin

         select department_id , department_name , manager_id

         into v_did , v_dname , v_mid
         from departments
         where department_id=10;


        ‫حغخخذو الخخٍاس األعًذة ٔانبٍاَاث انخً َحخاخٓا يٍ خالل‬




                     SQL - PL ‫دسٔط‬                              16
dbms_output.put_line('The recors is : ');
        dbms_output.put_line('ID : '|| v_did);
        dbms_output.put_line('Name : '|| v_dname);
        dbms_output.put_line('Manager: ‘ || v_mid);
end ;




                     SQL - PL ‫دسٔط‬                    17
declare
          v_rec departments % rowtype;
begin
          select *
          into v_rec
          from departments
          where department_id=10;

          dbms_output.put_line('The recors is : ');
          dbms_output.put_line('ID : '|| v_rec.department_id);
          dbms_output.put_line('Name : '|| v_rec.department_name);
          dbms_output.put_line('Manager: '||v_rec.manager_id);
          dbms_output.put_line('Location : '|| v_rec.location_id);
end ;




                       SQL - PL ‫دسٔط‬                                 18
‫‪declare‬‬
           ‫;‪v_rec departments % rowtype‬‬



          ‫عشفُا (‪ ًْ ٔ )v_rec‬يصفٕفت يٍ انًخغٍشاث يبٍُت‬
                       ‫عهى األعًذة انًٕخٕدة فً اندذٔل‬




                        ‫دسٔط ‪SQL - PL‬‬                    ‫91‬
begin
        select *
        into v_rec
        from departments
                                     ‫ُْا اخخشَا خًٍع انحقٕل‬
        where department_id=10;
                                      ‫انًٕخٕدة فً اندذٔل‬




                     SQL - PL ‫دسٔط‬                            20
dbms_output.put_line('The recors is : ');
        dbms_output.put_line('ID : '|| v_rec . department_id);
        dbms_output.put_line('Name : '|| v_rec . department_name);
        dbms_output.put_line('Manager: '||v_rec . manager_id);
        dbms_output.put_line('Location : '|| v_rec . location_id );
end ;




                      SQL - PL ‫دسٔط‬                                   21
SQL - PL ‫دسٔط‬   22
‫1.خًهت انششط‬
IF then
      SQL - PL ‫دسٔط‬   23
SQL - PL ‫دسٔط‬   24
declare
          v_salary employees.salary %type;
begin
          select salary into v_salary
          from employees
          where employee_id=150;
          if v_salary > 10000 then
                    dbms_output.put_line('VIP');
          end if ;
end;


                                           v_salary
                                                      employee_id
                               VIP
                      SQL - PL ‫دسٔط‬                                 25
‫خٛاب اٌششط إرا واْ خطأ‬




‫دسٔط ‪SQL - PL‬‬                            ‫62‬
declare
          v_salary employees.salary %type;
begin
          select salary into v_salary
          from employees
          where employee_id=102;
          if v_salary > 10000 then
                    dbms_output.put_line('VIP');
          else
                    dbms_output.put_line('normal');
          end if ;
end;




          normal
                       SQL - PL ‫دسٔط‬                  27
‫خٛاب اٌششط ارا واْ خطأ‬




‫دسٔط ‪SQL - PL‬‬                            ‫82‬
declare
          v_salary employees.salary%type;
begin
          select salary into v_salary
          from employees
          where employee_id=150;
          if v_salary > 10000 then
                     dbms_output.put_line('VIP');
          elsif (v_salary >=3000 and v_salary <=10000) then
                     dbms_output.put_line('normal');
          else
                     dbms_output.put_line('low');
          end if;
end;




                       SQL - PL ‫دسٔط‬                          29
declare
          v_salary employees.salary%type;
begin
          select salary into v_salary
          from employees
          where employee_id=150;
          if v_salary > 10000 then
                     dbms_output.put_line('VIP');
          elsif (v_salary >=3000 and v_salary <=10000) then
                     dbms_output.put_line('normal');
          else
                     dbms_output.put_line('low');
          end if;
end;




                       SQL - PL ‫دسٔط‬                          30
normal


         Low

SQL - PL ‫دسٔط‬            31
declare
          v_salary employees.salary%type;
begin
          select salary into v_salary
          from employees
          where employee_id=150;
          if v_salary > 10000 then
                     dbms_output.put_line('VIP');
          elsif (v_salary between 3000 and 10000) then
                     dbms_output.put_line('normal');
          else
                     dbms_output.put_line('low');
          end if;
end;




                      SQL - PL ‫دسٔط‬                      32
normal


                    Low



         SQL - PL ‫دسٔط‬    33
‫نكٍ الحع ُْا كخابت ‪elsif‬‬

        ‫ٔال حخطً فٍٓا فًٓ نٍغج ‪elseif‬‬

‫بئيكاَُا اعخخذاو ( ‪ ) And , Between‬داخم‬

                        ‫خًهت انششط ) ‪( IF & elsIf‬‬

        ‫دسٔط ‪SQL - PL‬‬                               ‫43‬
‫2. انخكشاس‬
 ‫ٌٕٔخذ عذة أٔايش نهخكشاس ًْٔ:‬


‫دسٔط ‪SQL - PL‬‬                   ‫53‬
(i) loop-exit-end

    SQL - PL ‫دسٔط‬   36
‫ششط اٌخشٚج‬




‫دسٔط ‪SQL - PL‬‬                ‫73‬
declare
  v_counter number := 1;
begin                                       v_counter

loop
         dbms_output.put_line(v_counter);
         v_counter:=v_counter+1;
         exit when v_counter>10;
end loop;
end ;




                     SQL - PL ‫دسٔط‬                      38
declare
v_counter number :=10;
begin                                       v_counter

loop
         dbms_output.put_line(v_counter);
         v_counter:=v_counter-1;
         exit when v_counter=0;
end loop;
end ;




                     SQL - PL ‫دسٔط‬                      39
(ii) WHILE - LOOP - END


      SQL - PL ‫دسٔط‬       40
‫اٌششط‬




‫دسٔط ‪SQL - PL‬‬           ‫14‬
declare                                         v_counter
v_counter number :=1;
begin
while v_counter <=10 loop
             dbms_output.put_line(v_counter);
             v_counter:=v_counter+1;
end loop;
end ;




                        SQL - PL ‫دسٔط‬                       42
declare                                      v_counter
v_counter number :=10;
begin
while v_counter >=1 loop
          dbms_output.put_line(v_counter);
          v_counter:=v_counter-1;
end loop;


end ;




                      SQL - PL ‫دسٔط‬                      43
declare
          v_sal employees.salary %type;
          v_counter number :=0;
begin
          select salary into v_sal
          from employees
          where employee_id=106;
          while v_sal <10000 loop
                   v_sal :=v_sal*1.2;
                   v_counter :=v_counter+1;
          end loop;
          dbms_output.put_line('The number of monthes is : '||v_counter);
end;




                       SQL - PL ‫دسٔط‬                                        44
v_sal
                        v_counter
                  salary




SQL - PL ‫دسٔط‬                       45
(iii) FOR - IN - LOOP - END


       SQL - PL ‫دسٔط‬          46
SQL - PL ‫دسٔط‬   47
begin
For v_counter in 1 ..10 loop
       dbms_output.put_line(v_counter);
end loop;
end ;

               :: ‫ٔاذح اٌثشٔاِح‬

         v_counter = 1
         v_counter = 2
         v_counter = 3
         v_counter = 4
         10 ُ‫سٛف يطثغ إٌى اٌشل‬


                  SQL - PL ‫دسٔط‬           48
begin
For v_counter in reverse 1..10 loop
        dbms_output.put_line(v_counter);
end loop;
end ;




                 SQL - PL ‫دسٔط‬             49
‫فً خًهت ‪For‬‬

‫يا َحخاج ‪ Declare‬ألٌ َقذس َعشف يخغٍش‬

                             ‫داخم انششط‬

                   ‫يثم:: يثال سقى 21 ٔ 31‬


   ‫دسٔط ‪SQL - PL‬‬                            ‫05‬
CURSORS
   ‫اٌّؤششاخ‬

 SQL - PL ‫دسٔط‬   51
‫‪CURSORS‬‬
     ‫ذسرخذَ اٌّؤششاخ إلداسج ػثاساخ اٌرحذيذ ‪ Select‬في ٌغح ‪ٚ sql‬وّا‬
 ‫الحظٕا األٚاِش اٌساتمح ِثً ‪ٚIF‬اٌرىشاس ٌُ ٔسرخذِٙا ِغ تيأاخ اٌدذاٚي‬
                ‫اٌّخضٔح ٌٚؼًّ رٌه التذ ِٓ اسرخذاَ ٘زٖ اٌّؤششاخ.‬


 ‫ٕٚ٘ان ٔٛػيٓ ِٓ اٌّؤششاخ ٘ي اٌضّٕيح ٚاٌصشيحح ٚسٛف ٔرطشق ٌه‬
                                     ‫ٚاحذ تاٌرفصيً ٚاألِثٍح اٌالصِح.‬


                                              ‫اٌّؤششاخ اٌصشيحح :‬


  ‫يرُ ذؼشيف ٘زا إٌٛع ِٓ اٌّؤششاخ ودضء ِٓ اإلػالْ ‪ٚDeclare‬يدة اْ‬
             ‫ذشرًّ ػثاسج ‪SQL‬اٌّؼشفٗ ػٍى ػثاسج اٌرحذيذ ‪Select‬فمظ‬


                ‫دسٔط ‪SQL - PL‬‬                                          ‫25‬
‫‪CURSORS‬‬
 ‫ٚػٕذ اسرخذاَ اٌّؤششاخ اٌصشيحح دائّا ِا سرىرة أستؼح ِىٛٔاخ وّا يٍي:‬


                                ‫١- يرُ ذؼشيف اٌّؤشش في اٌدضء ‪Declare‬‬
                                  ‫‪Begin‬‬   ‫٢- يرُ فرح اٌّؤشش تؼذ ػثاسج‬


                        ‫اٌصيغح اٌؼاِح ٌرؼشيف اٌّؤشش اٌصشيح وّا يٍي:‬




                ‫دسٔط ‪SQL - PL‬‬                                           ‫35‬
CURSORS




    SQL - PL ‫دسٔط‬   54
CURSORS




    SQL - PL ‫دسٔط‬   55
CURSORS _ Loop




SQL - PL ‫دسٔط‬             56
declare
          v_id employees.employee_id%type;
          v_fname employees.first_name%type;
          v_lname employees.last_name%type;
          v_sal employees.salary%type;
          cursor c is select employee_id,first_name,last_name,salary
          from employees where department_id=50;
begin
          open c;
          loop
                    fetch c into v_id,v_fname,v_lname,v_sal;
                    exit when c % notfound;
          dbms_output.put_line(v_id||' '||v_fname||' '||v_lname||' '||v_sal);
          end loop;
          close c;
end ;
                        SQL - PL ‫دسٔط‬                                           57
declare
          v_id employees.employee_id%type;
          v_fname employees.first_name%type;
          v_lname employees.last_name%type;
          v_sal employees.salary%type;


           cursor c is select employee_id,first_name,last_name,salary
           from employees
            where department_id=50;




                    SQL - PL ‫دسٔط‬                                       58
‫•جلب‬




begin
       open c;
       loop
fetch c into v_id,v_fname,v_lname,v_sal;

exit when c % notfound;
dbms_output.put_line(v_id||' '||v_fname||' '||v_lname||' '||v_sal);
          end loop;
          close c;
end ;




                        SQL - PL ‫دسٔط‬                                 59
‫الحظٕا في اٌّثاي اٌساتك أْ االسرؼالَ في ‪Cursor‬‬
‫سٛف يؼٛد تسدً ٚاحذ ٌىٓ ِارا يحذز ٌٛ أػاد اٌّؤشش أوثش ِٓ سدً‬
                                  ‫ٚأسدٔا اٌّشٚس ػٍى وافح اٌسدالخ ؟‬


                                ‫ٌزا ٔسرخذَ األِش ‪Found OR NotFound‬‬


                           ‫ٔسرخذَ األِش ‪ِ NotFound‬غ ‪Loop‬‬

  ‫ً٘ سدالخ اٌّؤشش أرٙد أَ ال ٚٔؼشف رٌه ِٓ خالي خاصيح ‪Found‬‬




           ‫دسٔط ‪SQL - PL‬‬                                             ‫06‬
CURSORS _ While




                while
                %found loop




SQL - PL ‫دسٔط‬                 61
declare
          v_id employees.employee_id%type;
          v_fname employees.first_name%type;
          v_lname employees.last_name%type;
          v_sal employees.salary%type;
          cursor c is select employee_id,first_name,last_name,salary
          from employees where department_id=50;
begin
          open c;
          fetch c into v_id,v_fname,v_lname,v_sal;
          while c %found loop
          dbms_output.put_line(v_id||' '||v_fname||' '||v_lname||' '||v_sal);
          fetch c into v_id,v_fname,v_lname,v_sal;
          end loop;
          close c;
end ;
                        SQL - PL ‫دسٔط‬                                           62
begin
        open c;
        fetch c into v_id,v_fname,v_lname,v_sal;
        while c %found loop
        dbms_output.put_line(v_id||' '||v_fname||' '||v_lname||' '||v_sal);
        fetch c into v_id,v_fname,v_lname,v_sal;
        end loop;
        close c;
end ;




                      SQL - PL ‫دسٔط‬                                           63
declare
          cursor c is select employee_id,first_name,last_name,salary
          from employees where department_id=50;
          v_rec c %rowtype;
begin
          open c;
          fetch c into v_rec;
          while c %found loop
          dbms_output.put_line(v_rec.employee_id||' '||v_rec.first_name||'
'||v_rec.last_name||' '||v_rec.salary);
                    fetch c into v_rec;
          end loop;
          close c;
end ;


                        SQL - PL ‫دسٔط‬                                        64
%rowtype



v_rec




        SQL - PL ‫دسٔط‬              65
declare
       cursor c is select employee_id,first_name,last_name,salary
       from employees
       where department_id=50;
begin
       for v_rec in c loop
       dbms_output.put_line(v_rec.employee_id||' '||v_rec.first_name||'
       '||v_rec.last_name||' '||v_rec.salary);
       end loop;
end;



                    SQL - PL ‫دسٔط‬                                         66
SQL - PL ‫دسٔط‬   67
declare
            v_job employees.job_id%type;
            v_id employees.employee_id%type;
            cursor c is select employee_id,job_id from employees;
begin
open c;
loop
            fetch c into v_id,v_job;
            exit when c%notfound;
            if v_job='ST_CLERK' then
                        delete from employees where employee_id=v_id;
            elsif v_job='IT_PROG' then
                        update employees set department_id=50 where employee_id=v_id;
            else
                        dbms_output.put_line('No Changes');
            end if;
end loop;
close c;
end ;

                            SQL - PL ‫دسٔط‬                                               68
v_id   v_sal

       v_sal    v_id




SQL - PL ‫دسٔط‬                    69
SQL - PL ‫دسٔط‬   70
declare
          v_sal employees.salary%type;
          v_id employees.employee_id%type;
          cursor c is select employee_id,salary from employees;
begin
          open c;
          loop
                    fetch c into v_id,v_sal;
                    exit when c%notfound;
                    if v_sal < 3000 then
          update employees set salary=salary*1.2 where employee_id=v_id;
                    elsif v_sal between 3000 and 10000 then
          update employees set salary=salary*1.15 where employee_id=v_id;
                    else
          update employees set salary=salary*1.1 where employee_id=v_id;
                    end if;
          end loop;
          close c;
end ;

                          SQL - PL ‫دسٔط‬                                     71
Used                   Conditions
One Fetch & ( % notFound )         Loop
                                     If
 Two Fetch & ( % Found )           While
     Don’t use Fetch                For




                 SQL - PL ‫دسٔط‬             72
‫‪Exception‬‬
 ‫االسـرـثـٕـاء‬

 ‫دسٔط ‪SQL - PL‬‬   ‫37‬
‫‪Exception‬‬
‫: ‪Exception handlers‬‬

        ‫ْٕ خضء اخخٍاسي ، ٔ ْزا اندضء ٌبذأ بكهًت ‪Exception‬‬
       ‫ٔ فً ْزا اندضء ٌخى ححذٌذ اإلخشاء انزي عٍخى احخارِ‬
                                     ‫فً حانت حذٔد خطأ‬




              ‫دسٔط ‪SQL - PL‬‬                                 ‫47‬
Exception
                       :: ‫ذٕمسُ األخطاء إٌى‬

1. Too many rows
2. No data found
3. Zero divide
4. Others

       SQL - PL ‫دسٔط‬                          75
Exception
  1. Too many rows
declare
       v_lname employees.last_name%type;
begin
   dbms_output.put_line('Before Exception');
        select last_name into v_lname
           from employees;
          dbms_output.put_line(v_lname);
exception
        when too_many_rows then
                    dbms_output.put_line('Pleae set Comndition in sql statement');
end;



                          SQL - PL ‫دسٔط‬                                              76
Exception
        2. No data found
declare
        v_lname employees.last_name%type;
begin
        select last_name into v_lname
        from employees
        where employee_id=101;
        dbms_output.put_line(v_lname);
exception
                when no_data_found then
        dbms_output.put_line('No data returned from sql statement');
end;
                     SQL - PL ‫دسٔط‬                                     77
Exception
        3. Zero divide
declare
        v_lname employees.last_name%type;
begin
        select last_name into v_lname
        from employees
        where employee_id=101;
        dbms_output.put_line(50/0);
exception
      when zero_divide then
                dbms_output.put_line('Cant division on zero');
end;
                    SQL - PL ‫دسٔط‬                                78
Exception
create table error
(
        title varchar2(25),

       err_date date
)                                          Title
                                    ‫خاص تؼٕٛاْ اٌخطأ‬


         err_date
     ‫خاص تراسيخ اٌخطأ‬



                  SQL - PL ‫دسٔط‬                    79
Exception
declare
          v_lname employees.last_name%type;
begin
         select last_name into v_lname from employees ;
         dbms_output.put_line(v_lname);
         dbms_output.put_line(50/0);
exception
         when too_many_rows then
                   dbms_output.put_line('Pleae set Comndition in sql statement');
                   insert into error values('Too many rows',sysdate);
         when no_data_found then
                   dbms_output.put_line('No data returned from sql statement');
                   insert into error values('no data found',sysdate);
         when zero_divide then
                   dbms_output.put_line('Cant division on zero');
                   insert into error values('Zero Divide',sysdate);
end;
                       SQL - PL ‫دسٔط‬                                                80
exception
            when too_many_rows then
                     dbms_output.put_line('Pleae set Comndition in sql statement');
                     insert into error values('Too many rows',sysdate);
            when no_data_found then
                     dbms_output.put_line('No data returned from sql statement');
                     insert into error values('no data found', sysdate);
            when zero_divide then
                     dbms_output.put_line('Cant division on zero');
                     insert into error values('Zero Divide',sysdate);
end;




                         SQL - PL ‫دسٔط‬                                                81
Exception
 4. Others
declare
            v_lname employees.last_name%type;
begin
            select last_name into v_lname from employees ;
            dbms_output.put_line(v_lname);
            dbms_output.put_line(50/0);
exception
            when too_many_rows then
                       dbms_output.put_line('Pleae set Comndition in sql statement');
                       insert into error values('Too many rows',sysdate);
            when no_data_found then
                       dbms_output.put_line('No data returned from sql statement');
                       insert into error values('no data found',sysdate);
            when zero_divide then
                       dbms_output.put_line('Cant division on zero');
                       insert into error values('Zero Divide',sysdate);
            when others then
                       dbms_output.put_line('Error');
end;
                               SQL - PL ‫دسٔط‬                                            82
exception
         when too_many_rows then
                 dbms_output.put_line('Pleae set Comndition in sql statement');
                 insert into error values('Too many rows',sysdate);
         when no_data_found then
                 dbms_output.put_line('No data returned from sql statement');
                 insert into error values('no data found',sysdate);
         when zero_divide then
                 dbms_output.put_line('Cant division on zero');
                 insert into error values('Zero Divide',sysdate);
         when others then
                 dbms_output.put_line('Error');




                       SQL - PL ‫دسٔط‬                                              83
‫‪Exception‬‬
                      ‫**حعشٌف انخطأ كًخغٍش‬

‫طشٌقت اعخخذاو األٔايش انخانٍت يع داخم‬
                               ‫( ‪) Exception‬‬
‫‪** Raise‬‬
‫‪** Commit‬‬
‫‪** Rollback‬‬
      ‫دسٔط ‪SQL - PL‬‬                            ‫48‬
Exception
declare
          v_sal employees.salary%type;
          my_exp exception;
begin
          update employees set salary=salary*1.7 where employee_id=200;
          select salary into v_sal from employees where employee_id=200;
          if v_sal >5500 then
                    raise my_exp;
          else
                    dbms_output.put_line('The salary is updated');
                    commit;
          end if;
exception
        when my_exp then
                    dbms_output.put_line('The salary is more than 7500');
                    rollback;
end;
                        SQL - PL ‫دسٔط‬                                       85
:: ‫**حعشٌف انخطأ كًخغٍش‬
          Declare ًٍ‫ًٌكٍ حعشٌف يخغٍش ( خاص باألخطاء ) ض‬


declare
      v_sal employees.salary%type;
      my_exp exception;




              SQL - PL ‫دسٔط‬                               86
‫‪** Raise‬‬

      ‫‪if v_sal >5500 then‬‬
            ‫;‪raise my_exp‬‬


    ‫إرا طهع انششط ( ‪ ) IF‬خطأ ٌعًم إصاحت نّ( اعخشاض )‬
                 ‫إنى يكاٌ انخاص باألخطاء‬


             ‫دسٔط ‪SQL - PL‬‬                             ‫78‬
** Commit
    else
              dbms_output.put_line('The salary is updated');
              commit;
    end if;




                     data base ٍ‫) إرا خشخُا ي‬commit( ‫ٔظٍفت‬
                                 ‫ٌـحـخـفع بدًٍع انخحذٌثاث ٔ انقٍى‬


                SQL - PL ‫دسٔط‬                                       88
** Rollback

 exception
         when my_exp then
               dbms_output.put_line('The salary is more than 7500');
               rollback;
 end;


                     data base ٍ‫) إرا خشخُا ي‬rollback( ‫ٔظٍفت‬
                                ‫ٌخشاخع عٍ خًٍع انخحذٌثاث ٔ انقٍى‬


                SQL - PL ‫دسٔط‬                                          89
Procedure &
 Function
  SQL - PL ‫دسٔط‬   90
Procedure



Begin
Exception
End (                       ;
            SQL - PL ‫دسٔط‬                   91
Procedure




SQL - PL ‫دسٔط‬               92
Procedure




SQL - PL ‫دسٔط‬               93
Function



Begin
Exception
End (                       ;
            SQL - PL ‫دسٔط‬                  94
‫‪Function‬‬
‫تحدثنا في الشريحة السابقة عن اإلجراءات المخزنة و اآلن نتحدث عن الوظائف المخزنة‬
                                       ‫لكن الفرق أن الوظائف المخزنة ترجع قيمة‬

                                                       ‫الصيغة العامة كما يلي :‬




       ‫دسٔط ‪SQL - PL‬‬                                                             ‫59‬
ً‫ ) ف‬Declare ( ‫ال ٌٕخذ قغى‬

) Function ( ٔ )Procedure(

      SQL - PL ‫دسٔط‬          96
Procedure
create or replace procedure print_all_emp( p_did
employees.department_id%type)
as
        cursor c is select first_name,last_name,salary
        from employees
        where department_id=p_did;
begin
        for v_rec in c loop
                 dbms_output.put_line(v_rec.first_name||'
                           '||v_rec.last_name||' '||v_rec.salary);
        end loop;
end print_all_emp;




                      SQL - PL ‫دسٔط‬                                  97
create or replace procedure print_all_emp

( p_did employees.department_id%type )




              SQL - PL ‫دسٔط‬                 98
Procedure
create table circle
( id number,
r number);

create or replace procedure circle_proc (p_id number)
is
         v_r number;
begin
         select r into v_r
         from circle where id=p_id;
         dbms_output.put_line('Muhit : '||(2*3.14*v_r));
         dbms_output.put_line('Area : '||(v_r*v_r*3.14));
exception
         when no_data_found         then
                   dbms_output.put_line('The circle you selected
no exists');
end circle_proc;
                       SQL - PL ‫دسٔط‬                               99
create or replace procedure circle_proc (p_id number)
is
         v_r number;
begin
         select r into v_r
         from circle where id=p_id;
         dbms_output.put_line(‘ Muhit : '||(2*3.14*v_r));
         dbms_output.put_line('Area : '||(v_r*v_r*3.14));
exception
         when no_data_found         then
         dbms_output.put_line('The circle you selected no exists');
end circle_proc;




                       SQL - PL ‫دسٔط‬                                  100
Function
create or replace function getR(p_id number)
return number
is
        v_r number;
begin
        select r into v_r
        from circle
        where id=p_id;
        return v_r;
exception
        when no_data_found        then
                  return 0;
end getR;


                    SQL - PL ‫دسٔط‬                     101
Procedure
create or replace procedure circle_proc( p_id number)
is
        v_r number;
begin
        v_r := getR( p_id );

        dbms_output.put_line('Muhit : '||(2*3.14*v_r));
        dbms_output.put_line('Area : '||(v_r*v_r*3.14));
end ;


          ) Function ( ‫ ) إنى‬v_r ( ‫قًُا بئعُاد قًٍت انًخغٍش اندذٌذ‬
                    ) Example 30 ( ً‫انغابقت انخً قًُا بخعشٌفٓا ف‬


                    SQL - PL ‫دسٔط‬                                102
Function
create or replace function getDid (p_eid number)
return number
is
        v_did number;
begin
        select department_id into v_did
        from employees
        where employee_id=p_eid;
        return v_did;
exception
        when no_data_found then
                 return 0;
end getDid;


                    SQL - PL ‫دسٔط‬                         103
Procedure
create or replace procedure updateEmpSal(p_eid number)
is
        v_did number;
begin
        v_did :=getDid(p_eid);
        if v_did!=0 then
                  if(v_did=50) then
update employees set salary=salary*1.5 where employee_id=p_eid;
                  else
update employees set salary=salary*1.2 where employee_id=p_eid;
                  end if;
        else
                  dbms_output.put_line('Employee not found');
        end if;
end     updateEmpSal;

                    SQL - PL ‫دسٔط‬                                 104
Procedure




SQL - PL ‫دسٔط‬           105
Procedure




SQL - PL ‫دسٔط‬           106
Procedure




SQL - PL ‫دسٔط‬           107
SQL - PL ‫دسٔط‬   108
SQL - PL ‫دسٔط‬   109
‫ذــــُ‬
‫تحّذ هللا‬
  ‫دسٔط ‪SQL - PL‬‬   ‫011‬

Mais conteúdo relacionado

Semelhante a SQL-PL

Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQLlubna19
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Thuan Nguyen
 
PL/SQL Code for Sample Projects
PL/SQL Code for Sample ProjectsPL/SQL Code for Sample Projects
PL/SQL Code for Sample Projectsjwjablonski
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Thuan Nguyen
 
Pl-sql blocks and block types and variablesdeclaring.pptx
Pl-sql blocks and block types and variablesdeclaring.pptxPl-sql blocks and block types and variablesdeclaring.pptx
Pl-sql blocks and block types and variablesdeclaring.pptxabobakralwaylysocial
 
Oracle 11g new features for developers
Oracle 11g new features for developersOracle 11g new features for developers
Oracle 11g new features for developersScott Wesley
 
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeksBeginning direct3d gameprogramming05_thebasics_20160421_jintaeks
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeksJinTaek Seo
 
Design and develop secured oracle applications
Design and develop secured oracle applicationsDesign and develop secured oracle applications
Design and develop secured oracle applicationsdyahalom
 
Open Gurukul Language PL/SQL
Open Gurukul Language PL/SQLOpen Gurukul Language PL/SQL
Open Gurukul Language PL/SQLOpen Gurukul
 
Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)
Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)
Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)Maik Becker
 
Neutralizing SQL Injection in PostgreSQL
Neutralizing SQL Injection in PostgreSQLNeutralizing SQL Injection in PostgreSQL
Neutralizing SQL Injection in PostgreSQLJuliano Atanazio
 

Semelhante a SQL-PL (20)

Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
 
PLSQL.docx
PLSQL.docxPLSQL.docx
PLSQL.docx
 
PL-SQL.pdf
PL-SQL.pdfPL-SQL.pdf
PL-SQL.pdf
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
 
plsql les06
 plsql les06 plsql les06
plsql les06
 
PL/SQL Code for Sample Projects
PL/SQL Code for Sample ProjectsPL/SQL Code for Sample Projects
PL/SQL Code for Sample Projects
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07
 
Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01
 
Pl-sql blocks and block types and variablesdeclaring.pptx
Pl-sql blocks and block types and variablesdeclaring.pptxPl-sql blocks and block types and variablesdeclaring.pptx
Pl-sql blocks and block types and variablesdeclaring.pptx
 
Oracle 11g new features for developers
Oracle 11g new features for developersOracle 11g new features for developers
Oracle 11g new features for developers
 
Oracle
OracleOracle
Oracle
 
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeksBeginning direct3d gameprogramming05_thebasics_20160421_jintaeks
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks
 
Design and develop secured oracle applications
Design and develop secured oracle applicationsDesign and develop secured oracle applications
Design and develop secured oracle applications
 
Open Gurukul Language PL/SQL
Open Gurukul Language PL/SQLOpen Gurukul Language PL/SQL
Open Gurukul Language PL/SQL
 
Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)
Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)
Exception Handling - The Good, the Bad and the Maybe (APEXCONN23)
 
SQL- Introduction to PL/SQL
SQL- Introduction to  PL/SQLSQL- Introduction to  PL/SQL
SQL- Introduction to PL/SQL
 
February0504 pm
February0504 pmFebruary0504 pm
February0504 pm
 
Neutralizing SQL Injection in PostgreSQL
Neutralizing SQL Injection in PostgreSQLNeutralizing SQL Injection in PostgreSQL
Neutralizing SQL Injection in PostgreSQL
 
PLSQL.pptx
PLSQL.pptxPLSQL.pptx
PLSQL.pptx
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 

Último

Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 

Último (20)

Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 

SQL-PL

  • 1.
  • 2. SQL - PL ‫دسٔط‬ 2
  • 3. SQL - PL ‫دسٔط‬ 3
  • 4. SQL - PL ‫دسٔط‬ 4
  • 5. ‫‪Declare‬‬ ‫ُْا حعشٌف انًخغٍشاث ٔ انًؤششاث‬ ‫‪Begin‬‬ ‫‪ Body‬حق انبشَايح‬ ‫‪Exception‬‬ ‫يعاندت األخطاء‬ ‫;‪end‬‬ ‫دسٔط ‪SQL - PL‬‬ ‫5‬
  • 6. ‫يالحظت ْايت :‬ ‫قغى ‪ٔexception‬قغى ‪declare‬‬ ‫ًْا اخخٍاسٌٍ أي ال ٌشخشط ٔخٕدًْا‬ ‫أي إرا كاٌ ال ٌٕخذ نذٌك حعشٌف يخغٍشاث ال حغخخذو ‪Declare‬‬ ‫ٔإرا كُج ال حخعايم يع األخطاء ال حغخخذو ‪Exception‬‬ ‫دسٔط ‪SQL - PL‬‬ ‫6‬
  • 7. name :='ahmed khaled'; v:=15; v_x number; ٍٍ‫ٌدب ٔضع انُقطخ‬ v_y number(10); = ‫قبم‬ v_z number(8,2) := 55.22; name varchar2(50); v_lname varchar2(20) :='Hilal'; v_bdate date; is_married boolean :=false; SQL - PL ‫دسٔط‬ 7
  • 8. type% : ً‫خاصيح ٔٛع اٌحم‬ declare v_did departments . department_id % type ; SQL - PL ‫دسٔط‬ 8
  • 9. ‫: ‪% rowtype‬‬ ‫يدًٕعت يخغٍشاث، ٔحغخخذو نخأعٍظ يصفٕفت يٍ‬ ‫انًخغٍشاث يبٍُت عهى األعًذة انًٕخٕدة فً يؤشش‬ ‫يا أٔ خذٔل يا‬ ‫‪declare‬‬ ‫‪v_rec departments‬‬ ‫; ‪% rowtype‬‬ ‫دسٔط ‪SQL - PL‬‬ ‫9‬
  • 10. dbms_output.put_line( ); dbms_output.put_line(v_y); dbms_output.put_line('###########'); dbms_output.put_line('The name is : '|| name); ٌٍ‫انًٕخذة ضًٍ عًهت انطباعت ًْ نهٕصم بٍٍ انخعبٍش‬ || ‫فائذة‬ SQL - PL ‫دسٔط‬ 10
  • 11. declare v_did number; v_dname varchar2(50); v_mid number; v_lid number; begin select department_id,department_name,manager_id,location_id into v_did,v_dname,v_mid,v_lid from departments where department_id=10; dbms_output.put_line('The recors is : '); dbms_output.put_line('ID : '|| v_did); dbms_output.put_line('Name : '|| v_dname); dbms_output.put_line('Manager: '||v_mid); dbms_output.put_line('Location : '|| v_lid); end ; SQL - PL ‫دسٔط‬ 11
  • 12. ‫ حغخخذو الخخٍاس األعًذة ٔانبٍاَاث انخً َحخاخٓا‬select begin select department_id , department_name , manager_id , location_id Declare ً‫ حغخخذو الخخٍاس األعًذة ٌٚىٓ يٍ خالل ياحى حعشٌفّ ف‬into into v_did , v_dname,v_mid , v_lid ‫حغخخذو نخحذٌذ اندذٔل انزي عُغخشخع يُّ انبٍاَاث‬ from departments where department_id=10; SQL - PL ‫دسٔط‬ 12
  • 13. declare v_did number; v_dname varchar2(50); v_mid number; v_lid number; begin v_did :=666; v_dname :='Imam UN'; v_mid :=105; v_lid :=1700; insert into departments ( department_id,department_name, manager_id , location_id) values(v_did , v_dname , v_mid , v_lid); end; SQL - PL ‫دسٔط‬ 13
  • 14. declare v_did departments.department_id%type; v_dname departments.department_name%type; v_mid departments.manager_id%type; begin select department_id,department_name,manager_id into v_did,v_dname,v_mid from departments where department_id=10; dbms_output.put_line('The recors is : '); dbms_output.put_line('ID : '|| v_did); dbms_output.put_line('Name : '|| v_dname); dbms_output.put_line('Manager: '||v_mid); end ; SQL - PL ‫دسٔط‬ 14
  • 15. declare v_did departments.department_id %type; v_dname departments.department_name %type; v_mid departments.manager_id %type; SQL - PL ‫دسٔط‬ 15
  • 16. begin select department_id , department_name , manager_id into v_did , v_dname , v_mid from departments where department_id=10; ‫حغخخذو الخخٍاس األعًذة ٔانبٍاَاث انخً َحخاخٓا يٍ خالل‬ SQL - PL ‫دسٔط‬ 16
  • 17. dbms_output.put_line('The recors is : '); dbms_output.put_line('ID : '|| v_did); dbms_output.put_line('Name : '|| v_dname); dbms_output.put_line('Manager: ‘ || v_mid); end ; SQL - PL ‫دسٔط‬ 17
  • 18. declare v_rec departments % rowtype; begin select * into v_rec from departments where department_id=10; dbms_output.put_line('The recors is : '); dbms_output.put_line('ID : '|| v_rec.department_id); dbms_output.put_line('Name : '|| v_rec.department_name); dbms_output.put_line('Manager: '||v_rec.manager_id); dbms_output.put_line('Location : '|| v_rec.location_id); end ; SQL - PL ‫دسٔط‬ 18
  • 19. ‫‪declare‬‬ ‫;‪v_rec departments % rowtype‬‬ ‫عشفُا (‪ ًْ ٔ )v_rec‬يصفٕفت يٍ انًخغٍشاث يبٍُت‬ ‫عهى األعًذة انًٕخٕدة فً اندذٔل‬ ‫دسٔط ‪SQL - PL‬‬ ‫91‬
  • 20. begin select * into v_rec from departments ‫ُْا اخخشَا خًٍع انحقٕل‬ where department_id=10; ‫انًٕخٕدة فً اندذٔل‬ SQL - PL ‫دسٔط‬ 20
  • 21. dbms_output.put_line('The recors is : '); dbms_output.put_line('ID : '|| v_rec . department_id); dbms_output.put_line('Name : '|| v_rec . department_name); dbms_output.put_line('Manager: '||v_rec . manager_id); dbms_output.put_line('Location : '|| v_rec . location_id ); end ; SQL - PL ‫دسٔط‬ 21
  • 22. SQL - PL ‫دسٔط‬ 22
  • 23. ‫1.خًهت انششط‬ IF then SQL - PL ‫دسٔط‬ 23
  • 24. SQL - PL ‫دسٔط‬ 24
  • 25. declare v_salary employees.salary %type; begin select salary into v_salary from employees where employee_id=150; if v_salary > 10000 then dbms_output.put_line('VIP'); end if ; end; v_salary employee_id VIP SQL - PL ‫دسٔط‬ 25
  • 26. ‫خٛاب اٌششط إرا واْ خطأ‬ ‫دسٔط ‪SQL - PL‬‬ ‫62‬
  • 27. declare v_salary employees.salary %type; begin select salary into v_salary from employees where employee_id=102; if v_salary > 10000 then dbms_output.put_line('VIP'); else dbms_output.put_line('normal'); end if ; end; normal SQL - PL ‫دسٔط‬ 27
  • 28. ‫خٛاب اٌششط ارا واْ خطأ‬ ‫دسٔط ‪SQL - PL‬‬ ‫82‬
  • 29. declare v_salary employees.salary%type; begin select salary into v_salary from employees where employee_id=150; if v_salary > 10000 then dbms_output.put_line('VIP'); elsif (v_salary >=3000 and v_salary <=10000) then dbms_output.put_line('normal'); else dbms_output.put_line('low'); end if; end; SQL - PL ‫دسٔط‬ 29
  • 30. declare v_salary employees.salary%type; begin select salary into v_salary from employees where employee_id=150; if v_salary > 10000 then dbms_output.put_line('VIP'); elsif (v_salary >=3000 and v_salary <=10000) then dbms_output.put_line('normal'); else dbms_output.put_line('low'); end if; end; SQL - PL ‫دسٔط‬ 30
  • 31. normal Low SQL - PL ‫دسٔط‬ 31
  • 32. declare v_salary employees.salary%type; begin select salary into v_salary from employees where employee_id=150; if v_salary > 10000 then dbms_output.put_line('VIP'); elsif (v_salary between 3000 and 10000) then dbms_output.put_line('normal'); else dbms_output.put_line('low'); end if; end; SQL - PL ‫دسٔط‬ 32
  • 33. normal Low SQL - PL ‫دسٔط‬ 33
  • 34. ‫نكٍ الحع ُْا كخابت ‪elsif‬‬ ‫ٔال حخطً فٍٓا فًٓ نٍغج ‪elseif‬‬ ‫بئيكاَُا اعخخذاو ( ‪ ) And , Between‬داخم‬ ‫خًهت انششط ) ‪( IF & elsIf‬‬ ‫دسٔط ‪SQL - PL‬‬ ‫43‬
  • 35. ‫2. انخكشاس‬ ‫ٌٕٔخذ عذة أٔايش نهخكشاس ًْٔ:‬ ‫دسٔط ‪SQL - PL‬‬ ‫53‬
  • 36. (i) loop-exit-end SQL - PL ‫دسٔط‬ 36
  • 38. declare v_counter number := 1; begin v_counter loop dbms_output.put_line(v_counter); v_counter:=v_counter+1; exit when v_counter>10; end loop; end ; SQL - PL ‫دسٔط‬ 38
  • 39. declare v_counter number :=10; begin v_counter loop dbms_output.put_line(v_counter); v_counter:=v_counter-1; exit when v_counter=0; end loop; end ; SQL - PL ‫دسٔط‬ 39
  • 40. (ii) WHILE - LOOP - END SQL - PL ‫دسٔط‬ 40
  • 42. declare v_counter v_counter number :=1; begin while v_counter <=10 loop dbms_output.put_line(v_counter); v_counter:=v_counter+1; end loop; end ; SQL - PL ‫دسٔط‬ 42
  • 43. declare v_counter v_counter number :=10; begin while v_counter >=1 loop dbms_output.put_line(v_counter); v_counter:=v_counter-1; end loop; end ; SQL - PL ‫دسٔط‬ 43
  • 44. declare v_sal employees.salary %type; v_counter number :=0; begin select salary into v_sal from employees where employee_id=106; while v_sal <10000 loop v_sal :=v_sal*1.2; v_counter :=v_counter+1; end loop; dbms_output.put_line('The number of monthes is : '||v_counter); end; SQL - PL ‫دسٔط‬ 44
  • 45. v_sal v_counter salary SQL - PL ‫دسٔط‬ 45
  • 46. (iii) FOR - IN - LOOP - END SQL - PL ‫دسٔط‬ 46
  • 47. SQL - PL ‫دسٔط‬ 47
  • 48. begin For v_counter in 1 ..10 loop dbms_output.put_line(v_counter); end loop; end ; :: ‫ٔاذح اٌثشٔاِح‬ v_counter = 1 v_counter = 2 v_counter = 3 v_counter = 4 10 ُ‫سٛف يطثغ إٌى اٌشل‬ SQL - PL ‫دسٔط‬ 48
  • 49. begin For v_counter in reverse 1..10 loop dbms_output.put_line(v_counter); end loop; end ; SQL - PL ‫دسٔط‬ 49
  • 50. ‫فً خًهت ‪For‬‬ ‫يا َحخاج ‪ Declare‬ألٌ َقذس َعشف يخغٍش‬ ‫داخم انششط‬ ‫يثم:: يثال سقى 21 ٔ 31‬ ‫دسٔط ‪SQL - PL‬‬ ‫05‬
  • 51. CURSORS ‫اٌّؤششاخ‬ SQL - PL ‫دسٔط‬ 51
  • 52. ‫‪CURSORS‬‬ ‫ذسرخذَ اٌّؤششاخ إلداسج ػثاساخ اٌرحذيذ ‪ Select‬في ٌغح ‪ٚ sql‬وّا‬ ‫الحظٕا األٚاِش اٌساتمح ِثً ‪ٚIF‬اٌرىشاس ٌُ ٔسرخذِٙا ِغ تيأاخ اٌدذاٚي‬ ‫اٌّخضٔح ٌٚؼًّ رٌه التذ ِٓ اسرخذاَ ٘زٖ اٌّؤششاخ.‬ ‫ٕٚ٘ان ٔٛػيٓ ِٓ اٌّؤششاخ ٘ي اٌضّٕيح ٚاٌصشيحح ٚسٛف ٔرطشق ٌه‬ ‫ٚاحذ تاٌرفصيً ٚاألِثٍح اٌالصِح.‬ ‫اٌّؤششاخ اٌصشيحح :‬ ‫يرُ ذؼشيف ٘زا إٌٛع ِٓ اٌّؤششاخ ودضء ِٓ اإلػالْ ‪ٚDeclare‬يدة اْ‬ ‫ذشرًّ ػثاسج ‪SQL‬اٌّؼشفٗ ػٍى ػثاسج اٌرحذيذ ‪Select‬فمظ‬ ‫دسٔط ‪SQL - PL‬‬ ‫25‬
  • 53. ‫‪CURSORS‬‬ ‫ٚػٕذ اسرخذاَ اٌّؤششاخ اٌصشيحح دائّا ِا سرىرة أستؼح ِىٛٔاخ وّا يٍي:‬ ‫١- يرُ ذؼشيف اٌّؤشش في اٌدضء ‪Declare‬‬ ‫‪Begin‬‬ ‫٢- يرُ فرح اٌّؤشش تؼذ ػثاسج‬ ‫اٌصيغح اٌؼاِح ٌرؼشيف اٌّؤشش اٌصشيح وّا يٍي:‬ ‫دسٔط ‪SQL - PL‬‬ ‫35‬
  • 54. CURSORS SQL - PL ‫دسٔط‬ 54
  • 55. CURSORS SQL - PL ‫دسٔط‬ 55
  • 56. CURSORS _ Loop SQL - PL ‫دسٔط‬ 56
  • 57. declare v_id employees.employee_id%type; v_fname employees.first_name%type; v_lname employees.last_name%type; v_sal employees.salary%type; cursor c is select employee_id,first_name,last_name,salary from employees where department_id=50; begin open c; loop fetch c into v_id,v_fname,v_lname,v_sal; exit when c % notfound; dbms_output.put_line(v_id||' '||v_fname||' '||v_lname||' '||v_sal); end loop; close c; end ; SQL - PL ‫دسٔط‬ 57
  • 58. declare v_id employees.employee_id%type; v_fname employees.first_name%type; v_lname employees.last_name%type; v_sal employees.salary%type; cursor c is select employee_id,first_name,last_name,salary from employees where department_id=50; SQL - PL ‫دسٔط‬ 58
  • 59. ‫•جلب‬ begin open c; loop fetch c into v_id,v_fname,v_lname,v_sal; exit when c % notfound; dbms_output.put_line(v_id||' '||v_fname||' '||v_lname||' '||v_sal); end loop; close c; end ; SQL - PL ‫دسٔط‬ 59
  • 60. ‫الحظٕا في اٌّثاي اٌساتك أْ االسرؼالَ في ‪Cursor‬‬ ‫سٛف يؼٛد تسدً ٚاحذ ٌىٓ ِارا يحذز ٌٛ أػاد اٌّؤشش أوثش ِٓ سدً‬ ‫ٚأسدٔا اٌّشٚس ػٍى وافح اٌسدالخ ؟‬ ‫ٌزا ٔسرخذَ األِش ‪Found OR NotFound‬‬ ‫ٔسرخذَ األِش ‪ِ NotFound‬غ ‪Loop‬‬ ‫ً٘ سدالخ اٌّؤشش أرٙد أَ ال ٚٔؼشف رٌه ِٓ خالي خاصيح ‪Found‬‬ ‫دسٔط ‪SQL - PL‬‬ ‫06‬
  • 61. CURSORS _ While while %found loop SQL - PL ‫دسٔط‬ 61
  • 62. declare v_id employees.employee_id%type; v_fname employees.first_name%type; v_lname employees.last_name%type; v_sal employees.salary%type; cursor c is select employee_id,first_name,last_name,salary from employees where department_id=50; begin open c; fetch c into v_id,v_fname,v_lname,v_sal; while c %found loop dbms_output.put_line(v_id||' '||v_fname||' '||v_lname||' '||v_sal); fetch c into v_id,v_fname,v_lname,v_sal; end loop; close c; end ; SQL - PL ‫دسٔط‬ 62
  • 63. begin open c; fetch c into v_id,v_fname,v_lname,v_sal; while c %found loop dbms_output.put_line(v_id||' '||v_fname||' '||v_lname||' '||v_sal); fetch c into v_id,v_fname,v_lname,v_sal; end loop; close c; end ; SQL - PL ‫دسٔط‬ 63
  • 64. declare cursor c is select employee_id,first_name,last_name,salary from employees where department_id=50; v_rec c %rowtype; begin open c; fetch c into v_rec; while c %found loop dbms_output.put_line(v_rec.employee_id||' '||v_rec.first_name||' '||v_rec.last_name||' '||v_rec.salary); fetch c into v_rec; end loop; close c; end ; SQL - PL ‫دسٔط‬ 64
  • 65. %rowtype v_rec SQL - PL ‫دسٔط‬ 65
  • 66. declare cursor c is select employee_id,first_name,last_name,salary from employees where department_id=50; begin for v_rec in c loop dbms_output.put_line(v_rec.employee_id||' '||v_rec.first_name||' '||v_rec.last_name||' '||v_rec.salary); end loop; end; SQL - PL ‫دسٔط‬ 66
  • 67. SQL - PL ‫دسٔط‬ 67
  • 68. declare v_job employees.job_id%type; v_id employees.employee_id%type; cursor c is select employee_id,job_id from employees; begin open c; loop fetch c into v_id,v_job; exit when c%notfound; if v_job='ST_CLERK' then delete from employees where employee_id=v_id; elsif v_job='IT_PROG' then update employees set department_id=50 where employee_id=v_id; else dbms_output.put_line('No Changes'); end if; end loop; close c; end ; SQL - PL ‫دسٔط‬ 68
  • 69. v_id v_sal v_sal v_id SQL - PL ‫دسٔط‬ 69
  • 70. SQL - PL ‫دسٔط‬ 70
  • 71. declare v_sal employees.salary%type; v_id employees.employee_id%type; cursor c is select employee_id,salary from employees; begin open c; loop fetch c into v_id,v_sal; exit when c%notfound; if v_sal < 3000 then update employees set salary=salary*1.2 where employee_id=v_id; elsif v_sal between 3000 and 10000 then update employees set salary=salary*1.15 where employee_id=v_id; else update employees set salary=salary*1.1 where employee_id=v_id; end if; end loop; close c; end ; SQL - PL ‫دسٔط‬ 71
  • 72. Used Conditions One Fetch & ( % notFound ) Loop If Two Fetch & ( % Found ) While Don’t use Fetch For SQL - PL ‫دسٔط‬ 72
  • 74. ‫‪Exception‬‬ ‫: ‪Exception handlers‬‬ ‫ْٕ خضء اخخٍاسي ، ٔ ْزا اندضء ٌبذأ بكهًت ‪Exception‬‬ ‫ٔ فً ْزا اندضء ٌخى ححذٌذ اإلخشاء انزي عٍخى احخارِ‬ ‫فً حانت حذٔد خطأ‬ ‫دسٔط ‪SQL - PL‬‬ ‫47‬
  • 75. Exception :: ‫ذٕمسُ األخطاء إٌى‬ 1. Too many rows 2. No data found 3. Zero divide 4. Others SQL - PL ‫دسٔط‬ 75
  • 76. Exception 1. Too many rows declare v_lname employees.last_name%type; begin dbms_output.put_line('Before Exception'); select last_name into v_lname from employees; dbms_output.put_line(v_lname); exception when too_many_rows then dbms_output.put_line('Pleae set Comndition in sql statement'); end; SQL - PL ‫دسٔط‬ 76
  • 77. Exception 2. No data found declare v_lname employees.last_name%type; begin select last_name into v_lname from employees where employee_id=101; dbms_output.put_line(v_lname); exception when no_data_found then dbms_output.put_line('No data returned from sql statement'); end; SQL - PL ‫دسٔط‬ 77
  • 78. Exception 3. Zero divide declare v_lname employees.last_name%type; begin select last_name into v_lname from employees where employee_id=101; dbms_output.put_line(50/0); exception when zero_divide then dbms_output.put_line('Cant division on zero'); end; SQL - PL ‫دسٔط‬ 78
  • 79. Exception create table error ( title varchar2(25), err_date date ) Title ‫خاص تؼٕٛاْ اٌخطأ‬ err_date ‫خاص تراسيخ اٌخطأ‬ SQL - PL ‫دسٔط‬ 79
  • 80. Exception declare v_lname employees.last_name%type; begin select last_name into v_lname from employees ; dbms_output.put_line(v_lname); dbms_output.put_line(50/0); exception when too_many_rows then dbms_output.put_line('Pleae set Comndition in sql statement'); insert into error values('Too many rows',sysdate); when no_data_found then dbms_output.put_line('No data returned from sql statement'); insert into error values('no data found',sysdate); when zero_divide then dbms_output.put_line('Cant division on zero'); insert into error values('Zero Divide',sysdate); end; SQL - PL ‫دسٔط‬ 80
  • 81. exception when too_many_rows then dbms_output.put_line('Pleae set Comndition in sql statement'); insert into error values('Too many rows',sysdate); when no_data_found then dbms_output.put_line('No data returned from sql statement'); insert into error values('no data found', sysdate); when zero_divide then dbms_output.put_line('Cant division on zero'); insert into error values('Zero Divide',sysdate); end; SQL - PL ‫دسٔط‬ 81
  • 82. Exception 4. Others declare v_lname employees.last_name%type; begin select last_name into v_lname from employees ; dbms_output.put_line(v_lname); dbms_output.put_line(50/0); exception when too_many_rows then dbms_output.put_line('Pleae set Comndition in sql statement'); insert into error values('Too many rows',sysdate); when no_data_found then dbms_output.put_line('No data returned from sql statement'); insert into error values('no data found',sysdate); when zero_divide then dbms_output.put_line('Cant division on zero'); insert into error values('Zero Divide',sysdate); when others then dbms_output.put_line('Error'); end; SQL - PL ‫دسٔط‬ 82
  • 83. exception when too_many_rows then dbms_output.put_line('Pleae set Comndition in sql statement'); insert into error values('Too many rows',sysdate); when no_data_found then dbms_output.put_line('No data returned from sql statement'); insert into error values('no data found',sysdate); when zero_divide then dbms_output.put_line('Cant division on zero'); insert into error values('Zero Divide',sysdate); when others then dbms_output.put_line('Error'); SQL - PL ‫دسٔط‬ 83
  • 84. ‫‪Exception‬‬ ‫**حعشٌف انخطأ كًخغٍش‬ ‫طشٌقت اعخخذاو األٔايش انخانٍت يع داخم‬ ‫( ‪) Exception‬‬ ‫‪** Raise‬‬ ‫‪** Commit‬‬ ‫‪** Rollback‬‬ ‫دسٔط ‪SQL - PL‬‬ ‫48‬
  • 85. Exception declare v_sal employees.salary%type; my_exp exception; begin update employees set salary=salary*1.7 where employee_id=200; select salary into v_sal from employees where employee_id=200; if v_sal >5500 then raise my_exp; else dbms_output.put_line('The salary is updated'); commit; end if; exception when my_exp then dbms_output.put_line('The salary is more than 7500'); rollback; end; SQL - PL ‫دسٔط‬ 85
  • 86. :: ‫**حعشٌف انخطأ كًخغٍش‬ Declare ًٍ‫ًٌكٍ حعشٌف يخغٍش ( خاص باألخطاء ) ض‬ declare v_sal employees.salary%type; my_exp exception; SQL - PL ‫دسٔط‬ 86
  • 87. ‫‪** Raise‬‬ ‫‪if v_sal >5500 then‬‬ ‫;‪raise my_exp‬‬ ‫إرا طهع انششط ( ‪ ) IF‬خطأ ٌعًم إصاحت نّ( اعخشاض )‬ ‫إنى يكاٌ انخاص باألخطاء‬ ‫دسٔط ‪SQL - PL‬‬ ‫78‬
  • 88. ** Commit else dbms_output.put_line('The salary is updated'); commit; end if; data base ٍ‫) إرا خشخُا ي‬commit( ‫ٔظٍفت‬ ‫ٌـحـخـفع بدًٍع انخحذٌثاث ٔ انقٍى‬ SQL - PL ‫دسٔط‬ 88
  • 89. ** Rollback exception when my_exp then dbms_output.put_line('The salary is more than 7500'); rollback; end; data base ٍ‫) إرا خشخُا ي‬rollback( ‫ٔظٍفت‬ ‫ٌخشاخع عٍ خًٍع انخحذٌثاث ٔ انقٍى‬ SQL - PL ‫دسٔط‬ 89
  • 90. Procedure & Function SQL - PL ‫دسٔط‬ 90
  • 91. Procedure Begin Exception End ( ; SQL - PL ‫دسٔط‬ 91
  • 92. Procedure SQL - PL ‫دسٔط‬ 92
  • 93. Procedure SQL - PL ‫دسٔط‬ 93
  • 94. Function Begin Exception End ( ; SQL - PL ‫دسٔط‬ 94
  • 95. ‫‪Function‬‬ ‫تحدثنا في الشريحة السابقة عن اإلجراءات المخزنة و اآلن نتحدث عن الوظائف المخزنة‬ ‫لكن الفرق أن الوظائف المخزنة ترجع قيمة‬ ‫الصيغة العامة كما يلي :‬ ‫دسٔط ‪SQL - PL‬‬ ‫59‬
  • 96. ً‫ ) ف‬Declare ( ‫ال ٌٕخذ قغى‬ ) Function ( ٔ )Procedure( SQL - PL ‫دسٔط‬ 96
  • 97. Procedure create or replace procedure print_all_emp( p_did employees.department_id%type) as cursor c is select first_name,last_name,salary from employees where department_id=p_did; begin for v_rec in c loop dbms_output.put_line(v_rec.first_name||' '||v_rec.last_name||' '||v_rec.salary); end loop; end print_all_emp; SQL - PL ‫دسٔط‬ 97
  • 98. create or replace procedure print_all_emp ( p_did employees.department_id%type ) SQL - PL ‫دسٔط‬ 98
  • 99. Procedure create table circle ( id number, r number); create or replace procedure circle_proc (p_id number) is v_r number; begin select r into v_r from circle where id=p_id; dbms_output.put_line('Muhit : '||(2*3.14*v_r)); dbms_output.put_line('Area : '||(v_r*v_r*3.14)); exception when no_data_found then dbms_output.put_line('The circle you selected no exists'); end circle_proc; SQL - PL ‫دسٔط‬ 99
  • 100. create or replace procedure circle_proc (p_id number) is v_r number; begin select r into v_r from circle where id=p_id; dbms_output.put_line(‘ Muhit : '||(2*3.14*v_r)); dbms_output.put_line('Area : '||(v_r*v_r*3.14)); exception when no_data_found then dbms_output.put_line('The circle you selected no exists'); end circle_proc; SQL - PL ‫دسٔط‬ 100
  • 101. Function create or replace function getR(p_id number) return number is v_r number; begin select r into v_r from circle where id=p_id; return v_r; exception when no_data_found then return 0; end getR; SQL - PL ‫دسٔط‬ 101
  • 102. Procedure create or replace procedure circle_proc( p_id number) is v_r number; begin v_r := getR( p_id ); dbms_output.put_line('Muhit : '||(2*3.14*v_r)); dbms_output.put_line('Area : '||(v_r*v_r*3.14)); end ; ) Function ( ‫ ) إنى‬v_r ( ‫قًُا بئعُاد قًٍت انًخغٍش اندذٌذ‬ ) Example 30 ( ً‫انغابقت انخً قًُا بخعشٌفٓا ف‬ SQL - PL ‫دسٔط‬ 102
  • 103. Function create or replace function getDid (p_eid number) return number is v_did number; begin select department_id into v_did from employees where employee_id=p_eid; return v_did; exception when no_data_found then return 0; end getDid; SQL - PL ‫دسٔط‬ 103
  • 104. Procedure create or replace procedure updateEmpSal(p_eid number) is v_did number; begin v_did :=getDid(p_eid); if v_did!=0 then if(v_did=50) then update employees set salary=salary*1.5 where employee_id=p_eid; else update employees set salary=salary*1.2 where employee_id=p_eid; end if; else dbms_output.put_line('Employee not found'); end if; end updateEmpSal; SQL - PL ‫دسٔط‬ 104
  • 105. Procedure SQL - PL ‫دسٔط‬ 105
  • 106. Procedure SQL - PL ‫دسٔط‬ 106
  • 107. Procedure SQL - PL ‫دسٔط‬ 107
  • 108. SQL - PL ‫دسٔط‬ 108
  • 109. SQL - PL ‫دسٔط‬ 109
  • 110. ‫ذــــُ‬ ‫تحّذ هللا‬ ‫دسٔط ‪SQL - PL‬‬ ‫011‬