码迷,mamicode.com
首页 > 数据库 > 详细

PL/SQL

时间:2015-03-17 23:31:31      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

flow control

the if statement

syntax :

IF condition1 THEN

action1;

[ELsIF condition2 THEN

action2;

[ELSE

action3;]

END IF;

eg :

IF condition THEN

SQL or PL-SQL Commands;

ELSE

SQL or PL-SQL Statements;

END IF;

eg :

DECLARE

l_x NUMBER := 10;

l_y NUMBER := 20;

BEGIN

IF l_x > l_y THEN

DBMS_OUTPUT.put_line(‘ X is bigger than y‘);

ELSIF

DBMS_OUTPUT.put_line(‘

 

eg :

v_Number1 number(10) := 12;

v_Number2 number(10) := &v_Number2;

eg :add two numbers 

DECLARE

--v_Number1 number(10) := 10;

v_Number1 number(10) := &v_Number1;

--v_Number2 number(10) := 30;

v_Number2 number(10) := &v_Number2;

v_result number(10);

BEGIN

v_result := v_Number1 + v_Number2;

DBMS_OUTPUT.put_line(‘Total : ‘ || v_result);

END;

 

 

simple loop

a loop is a section of code that needs to be executed a number of times.

syntax :

LOOP

statements..

EXIT WHEN variable condition;

END LOOP;

 

eg : to display 1 to 10

DECLARE

V1 number := 0;

BEGIN

LOOP

V1 := V1 + 1;

DBMS_OUTPUT.put_line(‘The number is :‘ || V1);

EXIT WHEN V1 > 10;

END loop;

END;

IF condition to check

EXIT;

END IF;

 

the for loop

no need to declare variable in for loop

check the condition in the beginning

no need to increase the value of the variable

eg :

FOR i in 1..10

LOOP

-- SQL Commands

-- if condition

-- exit when i = 6;

-- if i > 6 then

exit;

    END if;

DBMS_OUTPUT.put_line(i);

END LOOP;

for i in reverse 1..10 (from 10 to 1.)

&Start_Num

&End_Num

for in in reverse Start_Num..End_Num

 

the while loop

a while loop will continue as long as the condition is true.

syntax :

while condition

loop

statements

end loop;

note :

condition check at the beginning

break the loop

increase or decrease the value of a variable

declare the variable

eg :

WHILE l_x <= 100

LOOP

l_x := l_x +10;

END LOOP;

 

simple loop :

loop

exit when condition true

end loop;

while loop :

while condition

loop

 

end loop;

 

labeling loop

you can give loops a label by prefixing the loop with th following :

<< label_name >>

eg :

<<my_loop>>

while l_contimue

 

 

eg : in nested loops

<<my_while>>

while i < 10

loop

<<my_for>>

for i in 1..10

loop

if ...

exit my_while; -- jump the outer while loop.

else

exit my_for; -- jump the inner for loop.

end loop;

end loop;

eg :

<<outer>>

while l_continue

loop

<<inner>>

for count in 20..29

loop

l_total := count + l_x;

exit outer when l_total > l_overflow;

end loop inner;

l_continue := l_max > l_total;

end loop ounter;

you can label a block

eg :

<<block1>>

declare

l_x n

 

the goto statement

to directly jump to a particular section of code. goto is unconditional and final, there is no return,

eg :

declare

l_x NUMBER := 30;

l_y NUMBER :=20;

BEGIN

IF l_x > l_y THEN

GOTO skip_calc;

END IF;

l_x := l_y;

END;

 

NESTED BLOCKS PL/SQL 

labels

labelname.bariablename(from the outer block, if you want to use variables in the inner block.)

eg :Even Numbers

from 1 to 100, display all even numbers using loop.

simple loop

while loop

for loop

 

END;

 

while loop :

DECLARE

V1 NUMBER(3) := 0;

BEGIN

WHILE V1 <= 100

LOOP

V1 := V1 + 2

-- or method 2

-- if MOD(V1,2) = 0 THEN

-- DBMS_OUTPUT.put_line(V1);

DBMS_OUTPUT.put_line(V1);

END LOOP;

END;

 MOD(V1, 2) = 0 THEN (remaining)

 

eg :

LOOP

V1 := V1 + 2;

DBMS_OUTPUT.put_line(V1);

EXIT WHEN V1 >= 100;

END LOOP;

 

eg :

LOOP

IF MOD(V1,2) = 0 THEN

 

END IF;

END LOOP;

 

eg : sum of numbers from 1 to 100(using the simple or while or for loop)

DECLARE

V1 NUMBER(10) := 0;

SUM NUMBER(10) := 0;

BEGIN

WHILE V1 <= 100

LOOP

V1 := V1 + 1;

SUM := SUM + V1;

END LOOP;

DBMS_OUTPUT.put_line(SUM);

END;

 

eg :

DECLARE

Sum1 number(3) := 0;

Num1 number(3) :set = 0;

BEGIN

while Num1 <= 100

loop

Sum1 := Sum1 + Num1;

Num := Num1 + 1;

END loop;

DBMS_OUTPUT.put_line(Sum1);

END;

set serveroutput on

eg : total of odd (1,3,5) numbers from 1 to 100

eg : total of even (2,4,6) numbers from 1 to 100

DECLARE 

V1 NUMBER(10) := -1;

total NUMBER(10) := 0;

BEGIN

WHILE V1 <100

LOOP

V1 := V1 + 2;

total := total + V1;

END LOOP;

END;

DECLARE

V1 NUMBER(10) := 0;

total NUMBER(10) := 0;

BEGIN

WHILE V1 <100

LOOP

V1 := V1 + 2;

total := total + V1;

END LOOP;

END;

 

select MOD(123, 10) from dual; --3

select TRUNC(123/10) from dual; --12

eg : 12345 print 1+2+3+4+5

DECLARE

V1 NUMBER(10) := &V1;

total NUMBER(10) := 0;

remaining NUMBER(10) := 0;

BEGIN

WHILE V1 != 0

LOOP

remaining := MOD(V1, 10);

total := remaining + tota;‘

V1 := TRUNC (V1/10);

END LOOP;

DBMS_OUTPUT.put_line(total);

END;

 

sum up:

if,elsif, else, end if

loop end loop

while loop

for loop

simple loop

%rowtype

&variable

declare

emp_no number(5) := &emp_no;

employee emp%rowtype;

begin

select * into employee from emp where empno=emp_no;

DBMS_OUTPUT.put_line(‘Employee Number : ‘ || employee.EMPNO);

DBMS_OUTPUT.put_line(‘Employee Number : ‘ || employee.ENAME);

DBMS_OUTPUT.put_line(‘Employee Number : ‘ || employee.JOB);

DBMS_OUTPUT.put_line(‘Employee Number : ‘ || employee.MGR);

DBMS_OUTPUT.put_line(‘Employee Number : ‘ || employee.HIREDATE);

DBMS_OUTPUT.put_line(‘Employee Number : ‘ || employee.SAL);

DBMS_OUTPUT.put_line(‘Employee Number : ‘ || employee.COMM);

DBMS_OUTPUT.put_line(‘Employee Number : ‘ || employee.DEPTNO);

exception 

when NO_DATA_FOUND then

DBMS_OUTPUT.put_line(‘EMP does not exist‘);

when OTHERS then

end;

 

eg : find the largest number from three numbers

eg : write a program to reverse the number (eg : user &input 5678, you output 8765)

DECLARE

NUM NUMBER(10) := &NUM;

REV NUMBER(10) := 0;

REMAIN NUMBER(10) := 0;

BEGIN

WHILE NUM != 0

LOOP

REMAIN := MOD(NUM, 10);

NUM := TRUNC(NUM/10);

REV := REMAIN

END;

 

PL/SQL

标签:

原文地址:http://www.cnblogs.com/jilili/p/4345791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!