جمل التحكم المحاضرة التاسعة T.Eman Alsqour.

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Introduction to PL/SQL – Lecture 6.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
PL/SQL (Embedded SQL) Introduction Benefits Basic Constructs
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.
PL/SQL Agenda: Basic PL/SQL block structure
9 Copyright © 2009, Oracle. All rights reserved. Introducing Stored Procedures and Functions.
1 PL/SQL programming Procedures and Cursors Lecture 1 Akhtar Ali.
PL/SQL Introduction Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
1 JumpStart PL/SQL Wang Hao. 2 Outline PL/SQL Programming SQL*Plus and SQL commands.
PL/SQL Loops. Building Logical Conditions All logical conditions must yield a boolean condition. You can build a simple Boolean condition by combining.
About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and query statements of SQL are included.
Program with PL/SQL. Interacting with the Oracle Server.
Procedure Function Trigger. create table employee ( id number, name varchar2(100), deptno number, salary float, primary key(id) ) create table deptincrease.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
Program with PL/SQL Lesson 4. Writing Control Structure.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
Script Writing. Objectives: Definition of script writing Types of script Creating/Modification and running script Execution of script Fundamental of Script.
1 CursorsCursors. 2 SQL Cursor A cursor is a private SQL work area. A cursor is a private SQL work area. There are two types of cursors: There are two.
PL/SQL Procedural Language / Structured Query Language.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
PL/SQL. Introduction to PL/SQL block Declare declarations Begin executable statement Exception exception handlers End;
Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ Lecture by.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Bordoloi and Bock Control Structures: Iterative Control.
Copyright  Oracle Corporation, All rights reserved. 18 Interacting with the Oracle Server.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 Writing Control Structures Part D. 2 PL/SQL Decision Control Structures Sequential processing Sequential processing Processes statements one after another.
Overview Go over parts of quiz? Another iteration structure for loop.
Chapter 18: Exception Handling1 Chapter Eighteen Exception Handling Objective: – Define exceptions – List types of exception handlers – Trap errors – Exception.
Working with Loops, Conditional Statements, and Arrays.
CSI 3125, Preliminaries, page 1 Control Statements.
1 PL/SQL Part C Scope and Interacting with the Oracle Server.
Introduction to PL/SQL Francis Thottungal. The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables,
9 Copyright © 2007, Oracle. All rights reserved. Creating Stored Procedures and Functions.
PLSQL Cont…. Most Common Oracle Data Types VARCHAR2 –Stores variable-length character data. –Takes a required parameter that specifies a maximum length.
Fundamentals of PL/SQL part 2 (Basics)
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Program with PL/SQL Lesson 4.
Interacting with the Oracle Server
Writing Control Structures
Writing Executable Statements
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Oracle9i Developer: PL/SQL Programming Chapter 3 PL/SQL Processing.
Writing Control Structures
SQL PL/SQL Presented by: Dr. Samir Tartir
JavaScript: Control Statements.
Chapter 19 JavaScript.
Database Management Systems 2
Interacting with the Oracle Server
كتابة الجمل التنفيذية في PL/SQL
استخدام لغة معالجة البياناتDML ضمن الوحدات البرمجية
جمل التحكم.
حلقات التكرار.
Interacting with the Oracle Database Server
Writing Control Structures
Chapter 03 DBMS.
PL/SQL week10.
Program Flow.
Decisions, decisions, decisions
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

جمل التحكم المحاضرة التاسعة T.Eman Alsqour

جمل التحكم مثل :جمل الشرط و جمل الدوران مقدمة عادة تنفيذ الجمل داخل وحدة (Block) يكون بنفس الترتيب التي كتبت فيها هذه الجمل, لكن في بعض الأحيان نحتاج تغيير سير هذا التنفيذ أو تكرار جمل معينة , لحل هذه المشكلة نستخدم: جمل التحكم مثل :جمل الشرط و جمل الدوران T.Eman Alsqour

عند كتابة جملة الشرط لابد من وضع فاصلة منقوطة بعد انتهائها جمل الشرط هناك 3 أنواع من جمل الشرط: جملة الشرط البسيطة IF Statment : تتكون من جملة الشرط تليها مجموعة الجمل الواجب تنفيذها في حال تحقق الشرط. مثال: DECLARE v_sal NUMBER ; BEGIN …. IF v_ename = ‘Ali’ THEN v_sal := 3500; END IF; END; عند كتابة جملة الشرط لابد من وضع فاصلة منقوطة بعد انتهائها T.Eman Alsqour

جمل الشرط 2. جملة الشرط IF THEN ELSE : تتكون من مجموعتين من الجمل المجموعة الأولى وهي الواجب تنفيذها في حال تحقق الشرط والمجموعة الثانية وهي المجموعة الواجب تنفيذها في حال عدم تحقق الشرط. مثال: IF v_deptno = 10 THEN UPDATE emp SET sal = sal * 1.10 WHERE deptno = v_deptno; ELSE SET sal = sal * 1.08 END IF; T.Eman Alsqour

جمل الشرط T.Eman Alsqour

جمل الشرط 3. جملة الشرط IF THEN ELSIF : تتكون من مجموعتين من الجمل الأولى وهي المجموعة الواجب تنفيذها في حال تحقق الشرط والمجموعة الأخرى هي مجموعة ل (IF) الخاصة ب(ELSE) وأيضا ممكن أن تكون مقسمة إلى جزأين مجموعة متحققة عند حصول الشرط والأخرى أيضا بدورها ممكن أن تكون مقسمة إلى جزأين بناء على الشرط الجديد الثاني (IF) التابع ل(ELS) الثانية وهكذا إلى أن نصل إلى مرحلة تكون هناك (ELSE) ليس لها (IF). T.Eman Alsqour

T.Eman Alsqour

جمل الشرط مثال: IF v_grade >100 OR v_grade < 0 THEN DBMS_OUTPUT.PUT_LINE(‘Invalid Grade ’); ELSIF v_grade >= 90 THEN DBMS_OUTPUT.PUT_LINE(‘A’); ELSIF v_grade >= 80 THEN DBMS_OUTPUT.PUT_LINE(‘B’); ELSIF v_grade >= 70 THEN DBMS_OUTPUT.PUT_LINE(‘C’); ELSIF v_grade >= 60 THEN DBMS_OUTPUT.PUT_LINE(‘D’); ELSE DBMS_OUTPUT.PUT_LINE(‘F’); END IF; T.Eman Alsqour

جمل الشرط - أي تعبير حسابي يحتوي على NULL فإن النتيجة تكون NULL . عند استخدام جمل الشرط يمكن ربط أكثر من شرط باستخدام: AND ,OR , NOT مثال: IF v_sal > 3000 AND v_job = ‘SALESMAN’ OR v_deptno = 10 THEN ….. END IF ; T.Eman Alsqour

جداول المنطق نستخدم جداول المنطق لمعرفة نتيجة شرط مركب من أكثر من جزء. NULL FALSE TRUE AND NULL FALSE TRUE OR TRUR NOT FALSE TRUE NULL T.Eman Alsqour

جداول المنطق مثال لتطبيق جداول المنطق في جمل الشرط: DECLARE v_flag BOOLEAN  DEFAULT  TRUE; v_orderflag BOOLEAN ; BEGIN IF v_flag OR v_orderflag THEN v_flag := FALSE; v_orderflag := TRUE; END IF; IF v_flag AND v_orderflag THEN v_flag := TRUE; ELSIF v_flag = v_orderflag THEN v_flag := NULL; ELSE v_orderflag := FALSE; END IF ; END; إذا أنشئ متغير من نوع (BOOLEAN ) ولم تسند له أي قيمة فإن القيمة الابتدائية له ستكون NULL T.Eman Alsqour

حلقات الدوران حلقات الدوران في تعني إمكانية تكرار جملة أومجموعة من الجمل أثناء تنفيذ البرنامج. أشكال حلقات الدوران: حلقات الدوران البسيطة Basic Loop. حلقة الدوران FOR Loop. حلقة الدوران WHILE Loop. حلقات الدوران المتداخلة Nested LOOPS. T.Eman Alsqour

حلقة الدوران البسيطة Basic Loop LOOP  بداية الدوران Statement  جسم الدوران ... EXIT [WHENcondition];  جملة الخروج من جسم الدوران عند تحقق الشرط ويمكن أن تكون الجملة في أي مكان في جسم الدوران END LOOP;  جملة نهاية الدوران T.Eman Alsqour

حلقة الدوران البسيطة Basic Loop مثال: DECLARE v_ordid item.ordid%TYPE := 601; v_counter NUMBER(2) := 1; BEGIN LOOP INSERT INTO item(ordid, itemid) VALUES(v_ordid, v_counter); v_counter := v_counter + 1; EXIT WHEN v_counter > 10; END LOOP; END; T.Eman Alsqour

حلقة الدوران FOR Loop <sequence of statements>  جسم الدوران FOR <counter> IN [REVERSE]<lower bound>..<upper bound> LOOP  بداية الدوران <sequence of statements>  جسم الدوران END LOOP;  نهاية الدوران نستخدم حلقة الدوران FOR Loop في الحالات التي يكون فيها عدد مرات التكرار معروف قبل التنفيذ مثل 10 مرات ,5مرات ...إلخ T.Eman Alsqour

حلقة الدوران FOR Loop مثال: BEGIN FOR i IN 1..5 LOOP DBMS_OUTPUT.PUT_LINE('i= '||i); END LOOP; END; - أو يمكن كتابة نفس وحدة (Block) بالأعلى بالشكل التالي: DECLARE v_lower number:=1; v_upper number:=5; FOR i IN v_lower.. v_upper LOOP الناتج الذي سيظهر على الشاشة: i= 1 i= 2 i= 3 i= 4 i= 5 T.Eman Alsqour

حلقة الدوران FOR Loop لو أردنا استخدام حلقة الدوران (FOR Loop) مع ” REVERSE“ - مثال: BEGIN FOR i IN REVERSE 1..5 LOOP DBMS_OUTPUT.PUT_LINE('i= '||i); END LOOP; END; الناتج الذي سيظهر على الشاشة: i= 5 i= 4 i= 3 i= 2 i= 1 T.Eman Alsqour

حلقة الدوران WHILE Loop نستخدم في الحالات التي يكون فيها عدد مرات التكرار غير معروف قبل التنفيذ , فهي تشبه حلقة الدوران البسيطة ولكن بعكس الشرط. WHILE (condition)  بداية الدوران statement;  جسم الدوران . . . END LOOP;  نهاية الدوران T.Eman Alsqour

حلقة الدوران WHILE Loop مثال:  DECLARE       v_Counter BINARY_INTEGER := 1; BEGIN       WHILE (v_Counter <= 50) LOOP          INSERT INTO employee (id)  VALUES (v_Counter);          v_Counter := v_Counter + 1;        END LOOP;  END;     T.Eman Alsqour

حلقات الدوران المتداخلة Nested LOOPS هي عبارة عن حلقات دوران داخل بعضها البعض كل منها يمثل مستوى ويمكن أن تكون كل حلقة تمثل نوع مختلف من حلقات الدوران. لتمييز هذه المستويات عن بعضها البعض إذ لابد من استخدام عنوان للحلقة (label). عند الخروج من حلقة الدوران لا بد من ذكر عنوان الحلقة (label) لأن جملة الخروج تؤدي إلى الخروج من الدوران الداخلي والانتقال إلى المستوى الأكبر منه لبدء دورة جديدة. T.Eman Alsqour

مثال على حلقات الدوران المتداخلة Nested LOOPS DECLARE v_s NUMBER := 0; v_i NUMBER := 0; v_j NUMBER ; BEGIN <<outer_loop>> LOOP v_i := v_i + 1; v_j := 0; <<inner_loop>> v_j := v_j + 1; v_s := v_s + v_i * v_j; -- sum a bunch of products EXIT inner_loop WHEN (v_j > 5); EXIT outer_loop WHEN ((v_i * v_j) > 15); END LOOP inner_loop; END LOOP outer_loop; DBMS_OUTPUT.PUT_LINE('The sum of products equals: ' || TO_CHAR(s)); END; T.Eman Alsqour

الخلاصة جمل التحكم: جمل الشرط. - جملة الشرط البسيطة IF Statment - جملة الشرط IF THEN ELSE - جملة الشرط IF THEN ELSIF 2. حلقات الدوران. حلقات الدوران البسيطة Basic Loop. حلقة الدوران FOR Loop. حلقة الدوران WHILE Loop. حلقات الدوران المتداخلة Nested LOOPS. جداول المنطق. T.Eman Alsqour