PL/SQL programing 第六版学习笔记-2

CHAPTER 4

Conditional and Sequential Control

IF Statements

Using operators such as IS NULL and IS NOT NULL or functions
such as COALESCE and NVL2 are good ways to detect and deal with
potentially NULL values. For every variable that you reference in
every Boolean expression that you write, be sure to think carefully
about the consequences if that variable is NULL.

Nested IF Statements
IF condition1
THEN
  IF condition2
  THEN
    statements2
  ELSE
    IF condition3
    THEN
      statements3
    ELSIF condition4
    THEN
      statements4
    END IF;
  END IF;
END IF;
CASE Statements and Expressions
CASE employee_type
WHEN 'S' THEN
award_salary_bonus(employee_id);
WHEN 'H' THEN
award_hourly_bonus(employee_id);
WHEN 'C' THEN
award_commissioned_bonus(employee_id);
ELSE
RAISE invalid_employee_type;
END CASE;

In other words, if you don’t specify an ELSE clause, and none of the results in the WHEN
clauses match the result of the CASE expression, PL/SQL raises a CASE_NOT_FOUND
error. This behavior is different from what I’m used to with IF statements. When an IF
statement lacks an ELSE clause, nothing happens when the condition is not met. With
CASE, the analogous situation leads to an error.

Searched CASE Statements
CASE
WHEN expression1 THEN
statements1
WHEN expression2 THEN
statements2
...
ELSE
statements_else
END CASE;

Loop Basics

/* File on web: loop_examples.sql */
PROCEDURE display_multiple_years (
  start_year_in IN PLS_INTEGER
  ,end_year_in IN PLS_INTEGER
)
IS
  l_current_year PLS_INTEGER := start_year_in;
BEGIN
  LOOP
    EXIT WHEN l_current_year > end_year_in;
      display_total_sales (l_current_year);
      l_current_year := l_current_year + 1;
   END LOOP;
END display_multiple_years;

Oracle offers a numeric and a cursor FOR loop.

/* File on web: loop_examples.sql */
PROCEDURE display_multiple_years (
  start_year_in IN PLS_INTEGER
  ,end_year_in IN PLS_INTEGER
)
IS
BEGIN
  FOR l_current_year IN start_year_in .. end_year_in
    LOOP
    display_total_sales (l_current_year);
  END LOOP;
END display_multiple_years;
/* File on web: loop_examples.sql */
PROCEDURE display_multiple_years (
  start_year_in IN PLS_INTEGER
  ,end_year_in IN PLS_INTEGER
)
IS
BEGIN
  FOR sales_rec IN (
    SELECT *
    FROM sales_data
    WHERE year BETWEEN start_year_in AND end_year_in)
  LOOP
    display_total_sales (sales_rec.year);
  END LOOP;
END display_multiple_years;

The WHILE loop
simple loop

LOOP
  executable statement(s)
END LOOP;

EXIT;
EXIT WHEN condition;

As anyone who has accidentally run such an infinite loop can attest, it’s likely
that the loop will consume large portions of the CPU. The solution for this, in addition
to ensuring that your data gathering is performed as efficiently as possible, is to pause
between iterations:

LOOP
  data_gathering_procedure;
  DBMS_LOCK.sleep(10); -- do nothing for 10 seconds
END LOOP;

During the sleep period, the program uses virtually no cycles.

The WHILE Loop
WHILE condition
LOOP
  executable statement(s)
END LOOP;
The Numeric FOR Loop
FOR loop index IN [REVERSE] lowest number .. highest number
 LOOP
  executable statement(s)
END LOOP;

Even when you specify a REVERSE direction, you must still list the lowest bound
before the highest bound. If the first number is greater than the second number,
the body of the loop will not execute at all. If the lowest and highest bounds have
the same value, the loop will execute just once.

The Cursor FOR Loop

Here is the basic syntax of a cursor FOR loop:

FOR record IN { cursor_name | (explicit SELECT statement) }
LOOP
  executable statement(s)
END LOOP;

where record is a record declared implicitly by PL/SQL with the %ROWTYPE attribute
against the cursor specified by cursor_name.

Example of Cursor FOR Loops

Suppose I need to update the bills for all pets staying in my pet hotel, the Share-a-Din-
Din Inn. The following example contains an anonymous block that uses a cursor, occupancy_cur, to select the room number and pet ID number for all occupants of the
Inn. The procedure update_bill adds any new changes to that pet’s room charges:

DECLARE
   CURSOR occupancy_cur IS
      SELECT pet_id, room_number
      FROM occupancy WHERE occupied_dt = TRUNC (SYSDATE);
   occupancy_rec occupancy_cur%ROWTYPE;
BEGIN
  OPEN occupancy_cur;
  LOOP
    FETCH occupancy_cur INTO occupancy_rec;
    EXIT WHEN occupancy_cur%NOTFOUND;
    update_bill
    (occupancy_rec.pet_id, occupancy_rec.room_number);
  END LOOP;
  CLOSE occupancy_cur;
END;

This code leaves nothing to the imagination. In addition to defining the cursor (line 2),
you must explicitly declare the record for the cursor (line 5), open the cursor (line 7),
start up an infinite loop (line 8), fetch a row from the cursor set into the record (line 9),
check for an end-of-data condition with the %NOTFOUND cursor attribute (line 10),
and finally perform the update (line 11). When you are all done, you have to remember
to close the cursor (line 14).

If I convert this PL/SQL block to use a cursor FOR loop, then I have:

DECLARE
  CURSOR occupancy_cur IS
  SELECT pet_id, room_number
  FROM occupancy WHERE occupied_dt = TRUNC (SYSDATE);
BEGIN
  FOR occupancy_rec IN occupancy_cur
  LOOP
    update_bill (occupancy_rec.pet_id, occupancy_rec.room_number);
  END LOOP;
END;

You can use the loop label to qualify the name of the loop indexing variable (either
a record or a number). Again, this can be helpful for readability. Here is an example

<<year_loop>>
FOR year_number IN 1800..1995
LOOP
  <<month_loop>>
  FOR month_number IN 1 .. 12
  LOOP
    IF year_loop.year_number = 1900 THEN ... END IF;
  END LOOP month_loop;
END LOOP year_loop;


EXIT loop_label;
EXIT loop_label WHEN condition;
The CONTINUE Statement

Here is a simple example of using CONTINUE WHEN to skip over loop body execution

for even numbers:
BEGIN
  FOR l_index IN 1 .. 10
  LOOP
    CONTINUE WHEN MOD (l_index, 2) = 0;
    DBMS_OUTPUT.PUT_LINE ('Loop index = ' || TO_CHAR (l_index));
  END LOOP;
END;
/
The output is:
Loop index = 1
Loop index = 3
Loop index = 5
Loop index = 7
Loop index = 9

Exception Handlers

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,367评论 6 512
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,959评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,750评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,226评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,252评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,975评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,592评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,497评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,027评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,147评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,274评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,953评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,623评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,143评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,260评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,607评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,271评论 2 358

推荐阅读更多精彩内容