[转帖]PL/SQL 处理流程_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3708 | 回复: 0   主题: [转帖]PL/SQL 处理流程        下一篇 
yun
注册用户
等级:少校
经验:1082
发帖:83
精华:4
注册:2012-12-17
状态:离线
发送短消息息给yun 加好友    发送短消息息给yun 发消息
发表于: IP:您无权察看 2012-12-24 13:26:14 | [全部帖] [楼主帖] 楼主

    ***********PL/SQL 简介***************

    1、PL/SQL 是过程语言(Procedural Language)与结构化查询语言(SQL)结合而成的编程语言

    2、PL/SQL 是对SQL的扩展

    3、支持多种数据类型,如大对象和集合类型,可使用条件和循环等控制结构

    4、可用于创建存储过程、触发器和程序包,给SQL语句的执行添加程序逻辑

    5、与 Oracle 服务器和 Oracle 工具紧密集成,具备可移植性、灵活性和安全性

    §11.2.2  *********************PL/SQL 可用的SQL语句*******************

    PL/SQL是Oracle系统的核心语言,现在Oracle的许多部件都是由PL/SQL写成。在PL/SQL中可以使用的SQL语句有:

    在PL/SQL中可以用的SQL语句有:

 INSERT

UPDATE

DELETE

SELECT INTO

COMMIT

ROLLBACK

SAVEPOINT

    提示:在 PL/SQL中只能用 SQL语句中的 DML 部分,不能用 DDL 部分,

    如果要在PL/SQL中使用DDL(如Create  table  等)的话,只能以动态的方式来使用。

    Oracle 的 PL/SQL 组件在对 PL/SQL 程序进行解释时,同时对在其所使用的表名、列名及数据类型进行检查。

    PL/SQL 可以在SQL*PLUS 中使用。PL/SQL 可以在高级语言中使用。

    PL/SQL可以 在Oracle的 开发工具中使用。

    其它开发工具也可以调用PL/SQL编写的过程和函数,如Power Builder 等都可以调用服务器端的PL/SQL过程。

 /**************************PL/SQL块的结构如下:*************************/

Declare

/*   声明部分: 在此  声明PL/SQL用到的变量,类型及光标 */

begin

/*  执行部分:  过程及SQL 语句  , 即程序的主要部分  */

Exception

/* 执行异常部分: 错误处理  */

End;

    ************************************第十三章PL/SQL ���理流程**************************

    在PL/SQL程序中,要使程序能按照逻辑进行处理,除了有些语句是SQL语句外,还必须有能进行逻辑控制的语句。下面就介绍进行处理流程的语句结构。

 §13.1  /*********条件语句**********条件语句*********************条件语句******/

IF <布尔表达式> THEN

PL/SQL 和 SQL语句

END IF;

IF <布尔表达式> THEN

PL/SQL 和 SQL语句

ELSE

    其它语句

 END IF;

IF <布尔表达式> THEN

PL/SQL 和 SQL语句

ELSIF < 其它布尔表达式> THEN

其它语句

END IF;

    提示: ELSIF 不能写成 ELSEIF

    例:

 declare

v_salary annualsalary.nannualsalary%TYPE;--annualsalary.nannualsalary%TYPE 为 "表名.字段名%type"

v_output varchar(100);

begin

select nannualsalary into v_salary    --为v_salary 赋值

from annualsalary where nyear=1997;

if v_salary <40000 then

    v_output:='1员工工资小于40000';

 elsif v_salary>50000 then

    v_output:='1员工工资大于50000';

 else

    v_output:='1员工工资在40000到50000之间';

 end if;

dbms_output.put_line(v_output);

end;

    §13.2  循环

    1.  简单循环  ****************loop end loop*************loop  end loop*******************loop   end loop****************

 Loop

    要执行的语句;

 end loop;

--此循环将执行到遇到一条 exit 语句为止.

    例1.

 declare

x  number;

begin

x:= 0;

loop

x:=x+1;

dbms_output.put_line(to_char(x));

exit  when x=10;

end loop;

end;

    例 2.

 --节选自在线代码 simple.sql

DECLARE

V_counter  BINARY_INTEGER  := 1;

Begin

LOOP

Inert into temp_table

Values( v_counter, ‘loop index’ );

V_counter  := v_counter  + 1;

If v_counter > 50  then

Exit;

End if ;

End loop;

End;

    例 3.

 --节选自在线代码 exitwhen.sql

DECLARE

V_counter  binary_index := 1;

Begin

Loop

Insert  into temp_table

Values ( v_counter,’ loop index ‘ );

Exit  when  v_counter > 50  ;

End loop;

End;

    2.  WHILE 循环 /********While <> Loop  End Loop*******************While <> Loop  End Loop*******************

    While 循环

    While  <布尔表达式>  loop

    要执行的语句;

 end loop;

    例1.

 declare

x  number;

begin

x:= 1;

while  x<10  loop

    dbms_output.put_line(to_char(x)||’还小于10’);

 x:= x+1;

end loop;

end;

    例 2.

 --节选自在线代码 while1.sql

DECLARE

V_counter  binary_integer  := 1;

Begin

While v_counter <= 50  loop

Inert  into temp_table

Values( v_counter, ‘loop index ‘) ;

V_counter  := v_counter + 1;

End loop;

End;

    3.  数字式循环

    For 循环/****************************************For  循环计数器  in  下限 ..  上限

    For  循环计数器  in  下限 ..  上限

 loop

    要执行的语句;

 end loop;

FOR loop_counter  IN [ REVERSE ] low_bound  . . high_bound  LOOP

Sequence_of_statements;

END LOOP;

    例1.

 begin

for  I   in   1    ..   10    loop

dbms_output.put_line(‘in=’||to_char(I));

end loop;

end;

    例 2.

 --节选自在线代码 forscope.sql

DECLARE

V_counter  number := 7;

Begin

Inert  into temp_table  (num_col)

Values ( v_counter );

For  v_counter  IN 20 .. 30 loop

Insert  into temp_table (num_col )

Values ( v_counter );

End  loop;

Inert  into temp_table (num_col )

Values( v_counter );

End ;

    注:*******************************************如果在for 中用 INVERSE 关键字,则循环索引将从最大向最小进行迭代.

    §13.3  ***************标号和GOTO************标号和GOTO****************标号和GOTO

    PL/SQL中GOTO语句是无条件跳转到指定的标号去的意思。语法如下:

 GOTO   label;

. . .  . . .

<<label>>

    例:

 --节选自在线代码 goto.sql

DECLARE

V_counter  BINARY_INTEGER := 1;

Begin

Loop

Inert  into temp_table

Values( v_counter,’loop count’ );

V_counter  := v_counter + 1;

If  v_counter > 50 then

Goto l_endofloop;

End  if;

End loop;

<<l_endofloop>>

insert  into  temp_table ( char_col )

values(‘Done !’);

End ;

    §13.4  ***********NULL 语句 *******************NULL 语句 *****************NULL 语句

    在 PL/SQL 程序中,null语可以用 null 语句来说明“不用做什么”的意思。如:

 declare

. . .

begin

if( v_num is null then

goto print1;

end if;

<<print1>>

NULL;  -- 不需要处理任何数据。

End;




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论