本文共 3655 字,大约阅读时间需要 12 分钟。
PL/SQL(Procedural Language/SQL)是一种扩展的SQL语言,能够在Oracle数据库服务器和Oracle开发工具中运行。它通过PL/SQL引擎接收和执行PL/SQL块和子程序,从而将SQL语句提交给Oracle服务器执行。
在PL/SQL中,变量通常在PL/SQL块的声明部分定义,每个变量都有特定的数据类型。变量的声明格式如下:
<变量名> <数据类型> [(宽度):= <初始值> ] 初始值> 数据类型> 变量名>
例如,定义名为countNum的number类型变量,初始值为1,可以写为:
countNum number := 1;
PL/SQL提供了以下属性,可以简化变量的定义和管理:
%type属性:用于获取数据库列或变量的数据类型。例如,如果表A中字段B的数据类型未知,可以定义变量C为B%type,这样C将与B具有相同的数据类型。
%rowtype属性:用于获取表中一行记录的数据类型。例如,可以定义游标cursorName,并将其行记录存储在变量cursorRecord中:
declare cursor moduleCursor is select * from t_module; moduleRecord moduleCursor%rowtype;begin open moduleCursor; fetch moduleCursor into moduleRecord; while moduleCursor%found loop dbms_output.put_line(moduleRecord.name || '**********************'); fetch moduleCursor into moduleRecord; end loop; close moduleCursor;end;
PL/SQL支持自定义数据类型。例如,可以定义一个PersonRecord数据类型:
declare type PersonRecord is record ( name varchar(20), id number(3) ); person PersonRecord;begin select name, id into person from t_employee where username='admin'; dbms_output.put_line('**********' || person.name);end; 常量是在程序运行过程中值不变的量。常量的声明格式如下:
<常量名> constant <数据类型> := <值>值> 数据类型> 常量名>
例如,定义一个整型常量num,值为5,可以写为:
num constant integer := 5;
语法格式:
if booleanExpression then runExpressionend if;
示例代码:
declare totalCount number;begin select count(*) into totalCount from t_module; if totalCount > 0 then dbms_output.put_line('共有' || totalCount || '条记录'); end if;end; 语法格式:
if booleanExpression then runExpressionelse runExpression2end if;
示例代码:
declare a number := 1; b number := 2; c number := 3;begin if a = 1 then c := c + a; else c := c + b; end if; dbms_output.put_line(c); dbms_output.put_line(c);end;
语法格式:
if booleanExpression then runExpression1elsif booleanExpression2 then runExpression2else runExpression3end if;
在编写循环结构时,必须确保有退出条件满足。
语法格式:
loop runExpression; if booleanExpression then exit; end if;end loop;
示例代码,利用loop-exit-end循环求10的阶乘:
declare n number := 10; results number := 1;begin loop results := results * n; n := n - 1; if n = 0 then exit; end if; end loop; dbms_output.put_line('10的阶乘是:' || results);end; 语法格式:
loop runExpression; exit when booleanExpression;end loop;
示例代码,利用此结构求10的阶乘:
declare a number := 1; b number := 1;begin loop b := a * b; a := a + 1; exit when a > 10; end loop; dbms_output.put_line('the result is: ' || b);end; 语法格式:
while booleanExpressionloop runExpression;end loop;
示例代码:
declare a number := 1; b number := 1;begin while a <= 10 loop b := a * b; a := a + 1; end loop; dbms_output.put_line('the result is: ' || b);end; 语法格式:
for countVar in count1..countnloop runExpression;end loop;
示例代码:
declare b number := 1;begin for c in 1..10 loop b := b * c; end loop; dbms_output.put_line('the result is: ' || b);end; 可以使用case语句,对数值列表做出选择。
语法格式:
case inputNamewhen expression1 then result1when expression2 then result2when expressionn then resultnelse resultend case;
示例代码:
declare num1 number;begin num1 := 6; case num1 when 1 then dbms_output.put_line('the result is no.1'); when 2 then dbms_output.put_line('the result is no.2'); when 3 then dbms_output.put_line('the result is no.3'); else dbms_output.put_line('the result is not in top 3'); end case;end; 转载地址:http://oopfk.baihongyu.com/