博客
关于我
Oracle——08PL/SQL简介,基本程序结构和语句
阅读量:793 次
发布时间:2023-02-24

本文共 3655 字,大约阅读时间需要 12 分钟。

PL/SQL变量与常量及程序结构

PL/SQL(Procedural Language/SQL)是一种扩展的SQL语言,能够在Oracle数据库服务器和Oracle开发工具中运行。它通过PL/SQL引擎接收和执行PL/SQL块和子程序,从而将SQL语句提交给Oracle服务器执行。

1. PL/SQL变量

(1)变量的声明

在PL/SQL中,变量通常在PL/SQL块的声明部分定义,每个变量都有特定的数据类型。变量的声明格式如下:

<变量名>
<数据类型>
[(宽度):=
<初始值>
]

例如,定义名为countNumnumber类型变量,初始值为1,可以写为:

countNum number := 1;

(2)变量的属性

PL/SQL提供了以下属性,可以简化变量的定义和管理:

  • %type属性:用于获取数据库列或变量的数据类型。例如,如果表A中字段B的数据类型未知,可以定义变量CB%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;

(3)自定义数据类型

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;

2. PL/SQL常量

常量是在程序运行过程中值不变的量。常量的声明格式如下:

<常量名>
constant
<数据类型>
:=
<值>

例如,定义一个整型常量num,值为5,可以写为:

num constant integer := 5;

3. PL/SQL基本程序结构和语句

(1)条件结构

a. if-then结构

语法格式:

if booleanExpression then
runExpression
end if;

示例代码:

declare totalCount number;
begin
select count(*) into totalCount from t_module;
if totalCount > 0 then
dbms_output.put_line('共有' || totalCount || '条记录');
end if;
end;

b. if-then-else结构

语法格式:

if booleanExpression then
runExpression
else
runExpression2
end 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;

c. if-then-elsif-then-else结构

语法格式:

if booleanExpression then
runExpression1
elsif booleanExpression2 then
runExpression2
else
runExpression3
end if;

(2)循环结构

在编写循环结构时,必须确保有退出条件满足。

a. loop-exit-end循环

语法格式:

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;

b. loop-exit-when-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;

c. while-loop-end循环

语法格式:

while booleanExpression
loop
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;

d. for-in-loop-end循环

语法格式:

for countVar in count1..countn
loop
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;

(3)选择语句

可以使用case语句,对数值列表做出选择。

语法格式:

case inputName
when expression1 then result1
when expression2 then result2
when expressionn then resultn
else result
end 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/

你可能感兴趣的文章