SQL語法 | PL/SQL | Function | TRIGGER | StoreProc | DBA

2009年8月25日 星期二

基本語法

程式碼

DECLARE

V_STRING VARCHAR2(20);

V_DIAMETER NUMBER(5, 2);

V_CIRCUMFERENCE NUMBER(5, 2);

V_RADIUS NUMBER(5, 2) := 5;

C_PI CONSTANT NUMBER(3,2) := 3.14;

V_COUNTER NUMBER(5);

BEGIN

-- THIS IS SINGLE-LINE COMMENT

/*

THIS IS

MULTI-LINE

COMMENT

*/

V_DIAMETER := 9;

V_CIRCUMFERENCE := V_DIAMETER * C_PI;

DBMS_OUTPUT.PUT_LINE('V_CIRCUMFERENCE = ' || V_CIRCUMFERENCE);

IF V_RADIUS IS NULL THEN

DBMS_OUTPUT.PUT_LINE('V_RADIUS IS NULL.');

ELSIF V_RADIUS > 9 THEN

DBMS_OUTPUT.PUT_LINE('V_RADIUS IS GREATER THAN 9.');

ELSIF V_RADIUS < 9 THEN

DBMS_OUTPUT.PUT_LINE('V_RADIUS IS LESS THAN 9.');

ELSE

DBMS_OUTPUT.PUT_LINE('V_RADIUS IS EQUAL TO 9.');

END IF;

V_COUNTER := 1;

LOOP

EXIT WHEN V_COUNTER >= 6;

DBMS_OUTPUT.PUT_LINE('LOOP: V_COUNTER = ' || V_COUNTER);

V_COUNTER := V_COUNTER + 1;

END LOOP;

V_COUNTER := 1;

WHILE (V_COUNTER <= 5) LOOP

DBMS_OUTPUT.PUT_LINE('WHILE LOOP: V_COUNTER = ' || V_COUNTER);

V_COUNTER := V_COUNTER + 1;

EXIT WHEN V_COUNTER >= 9;

END LOOP;

FOR V_LOCAL_COUNTER IN 1 .. 5 LOOP

DBMS_OUTPUT.PUT_LINE('FOR LOOP: V_LOCAL_COUNTER = ' || V_LOCAL_COUNTER);

END LOOP;

FOR V_LOCAL_COUNTER IN REVERSE 1 .. 5 LOOP

DBMS_OUTPUT.PUT_LINE('FOR LOOP REVERSE: V_LOCAL_COUNTER = ' || V_LOCAL_COUNTER);

END LOOP;

END;

/

 

執行結果

V_CIRCUMFERENCE = 28.26

V_RADIUS IS LESS THAN 9.

LOOP: V_COUNTER = 1

LOOP: V_COUNTER = 2

LOOP: V_COUNTER = 3

LOOP: V_COUNTER = 4

LOOP: V_COUNTER = 5

WHILE LOOP: V_COUNTER = 1

WHILE LOOP: V_COUNTER = 2

WHILE LOOP: V_COUNTER = 3

WHILE LOOP: V_COUNTER = 4

WHILE LOOP: V_COUNTER = 5

FOR LOOP: V_LOCAL_COUNTER = 1

FOR LOOP: V_LOCAL_COUNTER = 2

FOR LOOP: V_LOCAL_COUNTER = 3

FOR LOOP: V_LOCAL_COUNTER = 4

FOR LOOP: V_LOCAL_COUNTER = 5

FOR LOOP REVERSE: V_LOCAL_COUNTER = 5

FOR LOOP REVERSE: V_LOCAL_COUNTER = 4

FOR LOOP REVERSE: V_LOCAL_COUNTER = 3

FOR LOOP REVERSE: V_LOCAL_COUNTER = 2

FOR LOOP REVERSE: V_LOCAL_COUNTER = 1

沒有留言: