[bbk4981]第08集 - Chapter 05- Writing Control Structures(00)
2013-04-23 12:40
459 查看
Objectives
After completing this lesson,you should be able to do the following:Identify the uses and types of control structures
Construct an IF statement
Use CASE statements and CASE expressions
Construct and identify loop statements
Use guidelines when using conditional control structures
PL/SQL中,要区分statement和expressions.
Agenda
Using IF statementsUsing CASE statements and CASE expressions
Constructing and identifying loop statements
Controlling Flow of Execution
IF Statements
Syntax:IF condition THEN statements; [ELSIF condition THEN statements;] [ELSE statements;] END IF;
The condition is a Boolean variable,constant,or expression that evaluates to TRUE,FALSE,or NULL.
If condition evaluates to TRUE,the executable statements found after the TEHN keyword and before the matching END IF statement are executed.
If condition evaluates to FALSE or NULL,those statements are not executed.
Simple IF Statement
DECLARE v_myage NUMBER :=31; BEGIN IF v_myage < 11 THEN DBMS_OUTPUT.PUT_LINE('I am a child!'); ELSIF v_myage > 18 THEN DBMS_OUTPUT.PUT_LINE('I am a man!'); END IF; END; /
DECLARE v_myage NUMBER := 31; BEGIN IF v_myage < 11 THEN DBMS_OUTPUT.PUT_LINE('I am a child!'); ELSIF v_myage < 20 THEN DBMS_OUTPUT.PUT_LINE('I am yung!'); ELSIF v_myage < 30 THEN DBMS_OUTPUT.PUT_LINE('I am in my twenties!'); ELSIF v_myage < 40 THEN DBMS_OUTPUT.PUT_LINE('I am in my thirties!'); ELSE DBMS_OUTPUT.PUT_LINE('I am alwarys yung!'); END IF; END; /
DECLARE v_myage NUMBER; BEGIN IF v_myage < 11 THEN DBMS_OUTPUT.PUT_LINE('I am a child!'); ELSIF v_myage < 40 THEN DBMS_OUTPUT.PUT_LINE('I am in my thirty!'); ELSIF v_myage IS NULL THEN DBMS_OUTPUT.PUT_LINE('I am a NULL value!'); END IF; END; /
Simple CASE Statement
DECLARE v_myage NUMBER := 31; BEGIN CASE WHEN v_myage< 11 THEN DBMS_OUTPUT.PUT_LINE('00至10岁之间'); WHEN v_myage< 21 THEN DBMS_OUTPUT.PUT_LINE('11至20岁之间'); WHEN v_myage< 31 THEN DBMS_OUTPUT.PUT_LINE('20至30岁之间'); WHEN v_myage< 41 THEN DBMS_OUTPUT.PUT_LINE('30至40岁之间'); ELSE DBMS_OUTPUT.PUT_LINE('超过40岁了,属于中年人!'); END CASE; END; /
Avoiding IF Syntax Gotchas
keep in mind these points about IF statement syntax:Always match up an IF with an END IF
You must have a space between the keywords END and IF
The ELSIF keyword should not have an embedded "E"
Place a semiconlon(;) only after the END IF keywords
CASE Expressions
A CASE expression selects a result and returns it.To select the result,the CASE expression uses expressions.The value returned by these expressions is used to select one of several alternatives.
CASE selector WHEN expression1 THEN result1 WHEN expression2 THEN result2 ... WHEN expressionN THEN resultN [ELSIF resultN+1] END;
CASE Expressions:Example
Select CASE expression
SET VERIFY OFF DECLARE v_grade CHAR(1) := UPPER('&grade'); v_appraisal VARCHAR2(20); BEGIN v_appraisal := CASE v_grade WHEN 'A' THEN 'Excellent' WHEN 'B' THEN 'Very Good' WHEN 'C' THEN 'Good' WHEN 'D' THEN 'No such grade' END; DBMS_OUTPUT.PUT_LINE('Grade:' || v_grade || ',Appraisal ' || v_appraisal); END; /
Searched Case Expression
SET VERIFY OFF DECLARE v_grade CHAR(1) := UPPER('&grade'); v_appraisal VARCHAR2(20); BEGIN v_appraisal := CASE WHEN v_grade = 'A' THEN 'Excellent' WHEN v_grade IN ('B','C') THEN 'Good' ELSE 'No such grade' END; DBMS_OUTPUT.PUT_LINE('Grade:' || v_grade || ',Appraisal ' || v_appraisal); END; /
CASE STATEMENT
所谓Statement就是一个独立的语句,不能再进行赋值了;CASE STATEMENT:CASE ... END CASE;
CASE EXPRESSION:CASE ... END;
DECLARE opt NUMBER := &optor; BEGIN IF opt = 1 THEN DBMS_OUTPUT.PUT_LINE('true'); END IF; CASE WHEN opt = 1 THEN DBMS_OUTPUT.PUT_LINE('Today is MON !'); WHEN opt = 2 THEN DBMS_OUTPUT.PUT_LINE('Today is TUS !'); WHEN opt = 3 THEN DBMS_OUTPUT.PUT_LINE('Today is WEN !'); WHEN opt = 4 THEN DBMS_OUTPUT.PUT_LINE('Today is THU !'); WHEN opt = 5 THEN DBMS_OUTPUT.PUT_LINE('Today is FRI !'); WHEN opt = 6 THEN DBMS_OUTPUT.PUT_LINE('Today is SAT !'); WHEN opt = 7 THEN DBMS_OUTPUT.PUT_LINE('Today is SUN !'); ELSE DBMS_OUTPUT.PUT_LINE('No Day!'); END CASE; CASE opt WHEN 1 THEN DBMS_OUTPUT.PUT_LINE('A'); WHEN 2 THEN DBMS_OUTPUT.PUT_LINE('B'); WHEN 3 THEN DBMS_OUTPUT.PUT_LINE('C'); WHEN 4 THEN DBMS_OUTPUT.PUT_LINE('D'); WHEN 5 THEN DBMS_OUTPUT.PUT_LINE('E'); WHEN 6 THEN DBMS_OUTPUT.PUT_LINE('F'); WHEN 7 THEN DBMS_OUTPUT.PUT_LINE('G'); ELSE DBMS_OUTPUT.PUT_LINE('O'); END CASE; END; /
Handing Nulls
When you are working with nulls,you can avoid some common mistakes by keeping in mind the following rules:Simple comparisions involving nulls always yield NULL.
Applying the logical operator NOT to a null yields NULL.
If the condition yields NULL in conditional control statements,its associated sequence of statements is not executed.
相关文章推荐
- [bbk4982]第09集 - Chapter 05- Writing Control Structures(01)
- [bbk4984]第10集 - Chapter 05- Writing Control Structures(02)
- 自学PL/SQL 第四讲Writing Control Structures
- [bbk3016] 第59集 - Chapter 14-Using Oracle Dasta Storage Structures Efficiently(05)
- 《代码大全2》阅读笔记10--Chapter 17 Unusual Control Structures
- [bbk2863] 第54集 - Chapter 14-Using Oracle Dasta Storage Structures Efficiently(00)
- CHAPTER 24 VIRTUAL-MACHINE CONTROL STRUCTURES
- Data Structures and Other Objects Using C++ (Chapter 1) 学习笔记二
- Chapter 6 - Structures(三)
- Chapter 6 - Structures(六)
- init: timeout opening/writing control channel /dev/initctl 解决方案
- Writing an ActiveX control with .Net
- [bbk2195] 第36集 - Chapter 09-Optimizing Sore Perations(05)
- [bbk2900] 第56集 - Chapter 14-Using Oracle Dasta Storage Structures Efficiently(02)
- Chapter 6 - Structures(四).1
- Chapter 6 - Structures(七)
- [导入]Writing an ActiveX control with .Net
- Chapter 3.GDI/DirectDraw Internal Data Structures
- Chapter 05-Reporting Aggregated data Using the Group Functions - 01
- CHAPTER 05:EX 04