P249 -P254 oracle8i_9i数据库基础——查看本号百度文库
***********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;