Sql pl

1,415 views

Published on

Database :: SQL-PL 2012

Published in: Technology
  • Be the first to comment

Sql pl

  1. 1. SQL - PL ‫دسٔط‬ 2
  2. 2. SQL - PL ‫دسٔط‬ 3
  3. 3. SQL - PL ‫دسٔط‬ 4
  4. 4. ‫‪Declare‬‬‫ُْا حعشٌف انًخغٍشاث ٔ انًؤششاث‬‫‪Begin‬‬‫‪ Body‬حق انبشَايح‬‫‪Exception‬‬‫يعاندت األخطاء‬‫;‪end‬‬ ‫دسٔط ‪SQL - PL‬‬ ‫5‬
  5. 5. ‫يالحظت ْايت :‬ ‫قغى ‪ٔexception‬قغى ‪declare‬‬ ‫ًْا اخخٍاسٌٍ أي ال ٌشخشط ٔخٕدًْا‬‫أي إرا كاٌ ال ٌٕخذ نذٌك حعشٌف يخغٍشاث ال حغخخذو ‪Declare‬‬ ‫ٔإرا كُج ال حخعايم يع األخطاء ال حغخخذو ‪Exception‬‬ ‫دسٔط ‪SQL - PL‬‬ ‫6‬
  6. 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
  7. 7. type% : ً‫خاصيح ٔٛع اٌحم‬declare v_did departments . department_id % type ; SQL - PL ‫دسٔط‬ 8
  8. 8. ‫: ‪% rowtype‬‬ ‫يدًٕعت يخغٍشاث، ٔحغخخذو نخأعٍظ يصفٕفت يٍ‬ ‫انًخغٍشاث يبٍُت عهى األعًذة انًٕخٕدة فً يؤشش‬ ‫يا أٔ خذٔل يا‬‫‪declare‬‬ ‫‪v_rec departments‬‬ ‫; ‪% rowtype‬‬ ‫دسٔط ‪SQL - PL‬‬ ‫9‬
  9. 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
  10. 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
  11. 11. ‫ حغخخذو الخخٍاس األعًذة ٔانبٍاَاث انخً َحخاخٓا‬selectbegin 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
  12. 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
  13. 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
  14. 14. declare v_did departments.department_id %type; v_dname departments.department_name %type; v_mid departments.manager_id %type; SQL - PL ‫دسٔط‬ 15
  15. 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
  16. 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
  17. 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
  18. 18. ‫‪declare‬‬ ‫;‪v_rec departments % rowtype‬‬ ‫عشفُا (‪ ًْ ٔ )v_rec‬يصفٕفت يٍ انًخغٍشاث يبٍُت‬ ‫عهى األعًذة انًٕخٕدة فً اندذٔل‬ ‫دسٔط ‪SQL - PL‬‬ ‫91‬
  19. 19. begin select * into v_rec from departments ‫ُْا اخخشَا خًٍع انحقٕل‬ where department_id=10; ‫انًٕخٕدة فً اندذٔل‬ SQL - PL ‫دسٔط‬ 20
  20. 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
  21. 21. SQL - PL ‫دسٔط‬ 22
  22. 22. ‫1.خًهت انششط‬IF then SQL - PL ‫دسٔط‬ 23
  23. 23. SQL - PL ‫دسٔط‬ 24
  24. 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
  25. 25. ‫خٛاب اٌششط إرا واْ خطأ‬‫دسٔط ‪SQL - PL‬‬ ‫62‬
  26. 26. 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
  27. 27. ‫خٛاب اٌششط ارا واْ خطأ‬‫دسٔط ‪SQL - PL‬‬ ‫82‬
  28. 28. 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
  29. 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
  30. 30. normal LowSQL - PL ‫دسٔط‬ 31
  31. 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
  32. 32. normal Low SQL - PL ‫دسٔط‬ 33
  33. 33. ‫نكٍ الحع ُْا كخابت ‪elsif‬‬ ‫ٔال حخطً فٍٓا فًٓ نٍغج ‪elseif‬‬‫بئيكاَُا اعخخذاو ( ‪ ) And , Between‬داخم‬ ‫خًهت انششط ) ‪( IF & elsIf‬‬ ‫دسٔط ‪SQL - PL‬‬ ‫43‬
  34. 34. ‫2. انخكشاس‬ ‫ٌٕٔخذ عذة أٔايش نهخكشاس ًْٔ:‬‫دسٔط ‪SQL - PL‬‬ ‫53‬
  35. 35. (i) loop-exit-end SQL - PL ‫دسٔط‬ 36
  36. 36. ‫ششط اٌخشٚج‬‫دسٔط ‪SQL - PL‬‬ ‫73‬
  37. 37. declare v_counter number := 1;begin v_counterloop dbms_output.put_line(v_counter); v_counter:=v_counter+1; exit when v_counter>10;end loop;end ; SQL - PL ‫دسٔط‬ 38
  38. 38. declarev_counter number :=10;begin v_counterloop dbms_output.put_line(v_counter); v_counter:=v_counter-1; exit when v_counter=0;end loop;end ; SQL - PL ‫دسٔط‬ 39
  39. 39. (ii) WHILE - LOOP - END SQL - PL ‫دسٔط‬ 40
  40. 40. ‫اٌششط‬‫دسٔط ‪SQL - PL‬‬ ‫14‬
  41. 41. declare v_counterv_counter number :=1;beginwhile v_counter <=10 loop dbms_output.put_line(v_counter); v_counter:=v_counter+1;end loop;end ; SQL - PL ‫دسٔط‬ 42
  42. 42. declare v_counterv_counter number :=10;beginwhile v_counter >=1 loop dbms_output.put_line(v_counter); v_counter:=v_counter-1;end loop;end ; SQL - PL ‫دسٔط‬ 43
  43. 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
  44. 44. v_sal v_counter salarySQL - PL ‫دسٔط‬ 45
  45. 45. (iii) FOR - IN - LOOP - END SQL - PL ‫دسٔط‬ 46
  46. 46. SQL - PL ‫دسٔط‬ 47
  47. 47. beginFor 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
  48. 48. beginFor v_counter in reverse 1..10 loop dbms_output.put_line(v_counter);end loop;end ; SQL - PL ‫دسٔط‬ 49
  49. 49. ‫فً خًهت ‪For‬‬‫يا َحخاج ‪ Declare‬ألٌ َقذس َعشف يخغٍش‬ ‫داخم انششط‬ ‫يثم:: يثال سقى 21 ٔ 31‬ ‫دسٔط ‪SQL - PL‬‬ ‫05‬
  50. 50. CURSORS ‫اٌّؤششاخ‬ SQL - PL ‫دسٔط‬ 51
  51. 51. ‫‪CURSORS‬‬ ‫ذسرخذَ اٌّؤششاخ إلداسج ػثاساخ اٌرحذيذ ‪ Select‬في ٌغح ‪ٚ sql‬وّا‬ ‫الحظٕا األٚاِش اٌساتمح ِثً ‪ٚIF‬اٌرىشاس ٌُ ٔسرخذِٙا ِغ تيأاخ اٌدذاٚي‬ ‫اٌّخضٔح ٌٚؼًّ رٌه التذ ِٓ اسرخذاَ ٘زٖ اٌّؤششاخ.‬ ‫ٕٚ٘ان ٔٛػيٓ ِٓ اٌّؤششاخ ٘ي اٌضّٕيح ٚاٌصشيحح ٚسٛف ٔرطشق ٌه‬ ‫ٚاحذ تاٌرفصيً ٚاألِثٍح اٌالصِح.‬ ‫اٌّؤششاخ اٌصشيحح :‬ ‫يرُ ذؼشيف ٘زا إٌٛع ِٓ اٌّؤششاخ ودضء ِٓ اإلػالْ ‪ٚDeclare‬يدة اْ‬ ‫ذشرًّ ػثاسج ‪SQL‬اٌّؼشفٗ ػٍى ػثاسج اٌرحذيذ ‪Select‬فمظ‬ ‫دسٔط ‪SQL - PL‬‬ ‫25‬
  52. 52. ‫‪CURSORS‬‬ ‫ٚػٕذ اسرخذاَ اٌّؤششاخ اٌصشيحح دائّا ِا سرىرة أستؼح ِىٛٔاخ وّا يٍي:‬ ‫١- يرُ ذؼشيف اٌّؤشش في اٌدضء ‪Declare‬‬ ‫‪Begin‬‬ ‫٢- يرُ فرح اٌّؤشش تؼذ ػثاسج‬ ‫اٌصيغح اٌؼاِح ٌرؼشيف اٌّؤشش اٌصشيح وّا يٍي:‬ ‫دسٔط ‪SQL - PL‬‬ ‫35‬
  53. 53. CURSORS SQL - PL ‫دسٔط‬ 54
  54. 54. CURSORS SQL - PL ‫دسٔط‬ 55
  55. 55. CURSORS _ LoopSQL - PL ‫دسٔط‬ 56
  56. 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
  57. 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
  58. 58. ‫•جلب‬begin open c; loopfetch 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
  59. 59. ‫الحظٕا في اٌّثاي اٌساتك أْ االسرؼالَ في ‪Cursor‬‬‫سٛف يؼٛد تسدً ٚاحذ ٌىٓ ِارا يحذز ٌٛ أػاد اٌّؤشش أوثش ِٓ سدً‬ ‫ٚأسدٔا اٌّشٚس ػٍى وافح اٌسدالخ ؟‬ ‫ٌزا ٔسرخذَ األِش ‪Found OR NotFound‬‬ ‫ٔسرخذَ األِش ‪ِ NotFound‬غ ‪Loop‬‬ ‫ً٘ سدالخ اٌّؤشش أرٙد أَ ال ٚٔؼشف رٌه ِٓ خالي خاصيح ‪Found‬‬ ‫دسٔط ‪SQL - PL‬‬ ‫06‬
  60. 60. CURSORS _ While while %found loopSQL - PL ‫دسٔط‬ 61
  61. 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
  62. 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
  63. 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
  64. 64. %rowtypev_rec SQL - PL ‫دسٔط‬ 65
  65. 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
  66. 66. SQL - PL ‫دسٔط‬ 67
  67. 67. declare v_job employees.job_id%type; v_id employees.employee_id%type; cursor c is select employee_id,job_id from employees;beginopen 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
  68. 68. v_id v_sal v_sal v_idSQL - PL ‫دسٔط‬ 69
  69. 69. SQL - PL ‫دسٔط‬ 70
  70. 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
  71. 71. Used ConditionsOne Fetch & ( % notFound ) Loop If Two Fetch & ( % Found ) While Don’t use Fetch For SQL - PL ‫دسٔط‬ 72
  72. 72. ‫‪Exception‬‬ ‫االسـرـثـٕـاء‬ ‫دسٔط ‪SQL - PL‬‬ ‫37‬
  73. 73. ‫‪Exception‬‬‫: ‪Exception handlers‬‬ ‫ْٕ خضء اخخٍاسي ، ٔ ْزا اندضء ٌبذأ بكهًت ‪Exception‬‬ ‫ٔ فً ْزا اندضء ٌخى ححذٌذ اإلخشاء انزي عٍخى احخارِ‬ ‫فً حانت حذٔد خطأ‬ ‫دسٔط ‪SQL - PL‬‬ ‫47‬
  74. 74. Exception :: ‫ذٕمسُ األخطاء إٌى‬1. Too many rows2. No data found3. Zero divide4. Others SQL - PL ‫دسٔط‬ 75
  75. 75. Exception 1. Too many rowsdeclare 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
  76. 76. Exception 2. No data founddeclare 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
  77. 77. Exception 3. Zero dividedeclare 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
  78. 78. Exceptioncreate table error( title varchar2(25), err_date date) Title ‫خاص تؼٕٛاْ اٌخطأ‬ err_date ‫خاص تراسيخ اٌخطأ‬ SQL - PL ‫دسٔط‬ 79
  79. 79. Exceptiondeclare 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
  80. 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
  81. 81. Exception 4. Othersdeclare 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
  82. 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
  83. 83. ‫‪Exception‬‬ ‫**حعشٌف انخطأ كًخغٍش‬‫طشٌقت اعخخذاو األٔايش انخانٍت يع داخم‬ ‫( ‪) Exception‬‬‫‪** Raise‬‬‫‪** Commit‬‬‫‪** Rollback‬‬ ‫دسٔط ‪SQL - PL‬‬ ‫48‬
  84. 84. Exceptiondeclare 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
  85. 85. :: ‫**حعشٌف انخطأ كًخغٍش‬ Declare ًٍ‫ًٌكٍ حعشٌف يخغٍش ( خاص باألخطاء ) ض‬declare v_sal employees.salary%type; my_exp exception; SQL - PL ‫دسٔط‬ 86
  86. 86. ‫‪** Raise‬‬ ‫‪if v_sal >5500 then‬‬ ‫;‪raise my_exp‬‬ ‫إرا طهع انششط ( ‪ ) IF‬خطأ ٌعًم إصاحت نّ( اعخشاض )‬ ‫إنى يكاٌ انخاص باألخطاء‬ ‫دسٔط ‪SQL - PL‬‬ ‫78‬
  87. 87. ** Commit else dbms_output.put_line(The salary is updated); commit; end if; data base ٍ‫) إرا خشخُا ي‬commit( ‫ٔظٍفت‬ ‫ٌـحـخـفع بدًٍع انخحذٌثاث ٔ انقٍى‬ SQL - PL ‫دسٔط‬ 88
  88. 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
  89. 89. Procedure & Function SQL - PL ‫دسٔط‬ 90
  90. 90. ProcedureBeginExceptionEnd ( ; SQL - PL ‫دسٔط‬ 91
  91. 91. ProcedureSQL - PL ‫دسٔط‬ 92
  92. 92. ProcedureSQL - PL ‫دسٔط‬ 93
  93. 93. FunctionBeginExceptionEnd ( ; SQL - PL ‫دسٔط‬ 94
  94. 94. ‫‪Function‬‬‫تحدثنا في الشريحة السابقة عن اإلجراءات المخزنة و اآلن نتحدث عن الوظائف المخزنة‬ ‫لكن الفرق أن الوظائف المخزنة ترجع قيمة‬ ‫الصيغة العامة كما يلي :‬ ‫دسٔط ‪SQL - PL‬‬ ‫59‬
  95. 95. ً‫ ) ف‬Declare ( ‫ال ٌٕخذ قغى‬) Function ( ٔ )Procedure( SQL - PL ‫دسٔط‬ 96
  96. 96. Procedurecreate or replace procedure print_all_emp( p_didemployees.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
  97. 97. create or replace procedure print_all_emp( p_did employees.department_id%type ) SQL - PL ‫دسٔط‬ 98
  98. 98. Procedurecreate 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 selectedno exists);end circle_proc; SQL - PL ‫دسٔط‬ 99
  99. 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
  100. 100. Functioncreate or replace function getR(p_id number)return numberis 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
  101. 101. Procedurecreate 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
  102. 102. Functioncreate or replace function getDid (p_eid number)return numberis 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
  103. 103. Procedurecreate 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) thenupdate employees set salary=salary*1.5 where employee_id=p_eid; elseupdate 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
  104. 104. ProcedureSQL - PL ‫دسٔط‬ 105
  105. 105. ProcedureSQL - PL ‫دسٔط‬ 106
  106. 106. ProcedureSQL - PL ‫دسٔط‬ 107
  107. 107. SQL - PL ‫دسٔط‬ 108
  108. 108. SQL - PL ‫دسٔط‬ 109
  109. 109. ‫ذــــُ‬‫تحّذ هللا‬ ‫دسٔط ‪SQL - PL‬‬ ‫011‬

×