PL/SQL学习笔记二

1.常量

定义语法格式:

常量名 constant 类型标识符 [not null]:=值;

如:PI constant number(9):=3.1415;

 

2.基本数据类型变量

基本数据类型

number  数字型

int 整数型

pls_integer 整数型,产生溢出时出现错误

binary_integer 整数型,表示带符号的整数

char 定长字符型,最大255个字符

varchar2 变长字符型,最大2000个字符

long 变长字符型,最大2GB

date 日期型

boolean 布尔型(true,false,null三者之一)

 

基本数据类型变量的定义语法格式

变量名 类型标识符 [not null]:=值;

如:myvar varchar2(10):='right';

 

3.复合数据类型变量

3.1 表字段类型变量(使用%type定义)

变量的类型与数据表中的字段的数据类型一致。当数据库表的字段类型修改后,相应的变量的类型也自动修改。

定义语法格式:变量名 表字段名%type;

如:mydate tempuser.testtable.currentdate%type 定义了名为mydate的变量,其类型与tempuser.testtable表中的currentdate字段类型一致。

 

3.2 记录类型变量

记录类型定义语法格式:

type 数据类型名 is record(

     字段1 类型标识符1,

     字段2 类型标识符2,

     ...

);

记录类型变量定义:

变量名 数据类型名;

 

如定义记录类型:

type myrecord is record(myrecordnumber int, mycurrentdate date);

定义myrecord类型变量srecord:

srecord myrecord;

访问myrecordnumber字段值:

srecord.myrecordnumber

 

注意:字段1,字段2等也可以是复合数据类型。

 

3.3 表记录型变量(使用%rowtype定义)

定义语法格式:变量名 表名%rowtype

变量可以获得整个表记录的数据类型,相当于先定义了一个记录类型,其各字段类型分别对应表的各字段类型;然后再定义这种记录类型的变量

 

3.4 一维表类型变量

定义语法格式:

type 表类型 is table of 类型 index by binary_integer;

表类型变量名 表类型;

 

相当定义一维数组,这里的类型可以是前面的类型定义,index by binary_integer子句代表以符号整数为索引,这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)“。

示例:

declare 

  type tabletype1 is table of varchar2(4) index by binary_integer; --定义一维表类型tabletype1

  type tabletype2 is table of tempuser.testtable."recordnumber"%type index by binary_integer; --定义一维表类型tabletype2

  "table1" tabletype1; -- 定义表类型tabletype1变量table1

  "table2" tabletype2; -- 定义表类型tabletype2变量table2

begin

  "table1"(1):='大学';

  "table1"(2):='大专';

  "table2"(1):=88;

  "table2"(2):=55;

  dbms_output.put_line("table1"(1) || "table2"(1));

  dbms_output.put_line("table1"(2) || "table2"(2));

end;

 

3.5二维表类型变量

定义语法格式:

type 表类型 is table of 表名%rowtype index by binary_integer;

 

注意:在定义好的表类型(一维和二维)表类型变量里,可以使用count,delete,first,last,next,exists和prior等属性进行操作,使用方法为"表变量名.属性”,返回的是数字

示例:

set serveroutput on

declare

  type tabletype1 is table of varchar2(9) index by binary_integer;

  "table1" tabletype1;

begin

  "table1"(1):='成都市';

  "table1"(2):='北京市';

  "table1"(3):='青岛市';

  dbms_output.put_line('总记录数:' || to_char("table1".count));

  dbms_output.put_line('第一条记录:' || "table1".first);

  dbms_output.put_line('最后一条记录:' || "table1".last);

  dbms_output.put_line('第2条的前一条记录:' || "table1".prior(2));

  dbms_output.put_line('第2条的后一条记录:' || "table1".next(2));

end;

 

4.表达式

4.1数值表达式

运算符:+(加法),-(减法),*(乘法),/(除法)和**(乘方)

 

4.2字符表达式

字符连接运算符: ||

 

4.3关系表达式

关系表达式有字符表达式或数值表达式与关系运算符组成,共9种关系运算符:

< 小于

> 大于

<= 小于等于

>= 大于等于

= 等于

!=不等于

like 类似于

in 在...之中

between 在...之间

 

4.4逻辑表达式

逻辑运算符:Not,Or,And

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值