续
6、命名参考
1>标识符:
不区分大小写、不能包含减号(-)、首字符必须为字母,不能是SQL保留字、不能超过30个字符。
2>命名参考:
程序变量:v_name v_orderId 程序常量:c_name c_cityId 游标变量:cursor_name cursor_storeId 异常标示符:e_name e_agentId 记录类型:name_record test_city_record 绑定变量:g_name g_userId 错误:e_error
3>数据类型:(5大类)
(1) 字符类型(CHAR、NCHAR 、VARCHAR、 VARCHAR2、 NVARCHAR2)
(2) 数字类型(NUMBER 、INTEGER 、BINARY_FLOAT 、BINARY_DOUBLE)
(3) 时间类型(DATE 、TIMESTAMP 、INTERVAL YEAR 、INTERVAL DAY)、(4) 大对象类型(BLOB 、CLOB 、BFILE 、NCLOB)、
(5) 其他类型(LONG 、RAW LONG RAW 、ROWID 、UROWID) 。
bfile(moive):存放大的二进制数据对象,表中只存放文件的目录。大小<=4GB
blob(photo):存放大的二进制数据对象的位置,位置指向二进制对象的数据块。大小<=4GB
clob(book):存放大的字符数据对象的位置,位置指向字符的数据块。大小<=4GB
nclob(ncahr字符数据):存放大的nchar字符数据对象的位置,位置指向nchar字符的数据块。大小<=4GB
4>变量声明
v_flag boolean not null default false;
identifier [constant] datetype [not null] [:=value|default value |expression]
identifier:变量名称
datetype:变量类型
:=value 变量或常量的初始值
default value:默认值
expression 为函数 其他变量、文本值等
5>注释
--单行注释
/*多行注释*/
7、复合类型介绍
1>复合类型:(记录类型、数组类型、一维表类型、二维表类型)
1)记录类型:记录类型类似于c语言中的结构数据类型,它把逻辑相关的、分离的、基本数据类型的变量组成一个整体存储起来,它必须包括至少一个标量型或record 数据类型的成员,称作pl/sql record 的域(field),其作用是存放互不相同但逻辑相关的信息。在使用记录数据类型变量时,需要先在声明部分先定义记录的组成、记录的变量,然后在执行部分引用该记录变量本身或其中的成员。
type record_name is record(
v1 data_type1 [not null] [:= default_value ],
...
vn data_typen [not null] [:= default_value ] );
2)说明:%type:表示变量的数据类型与表对应的列的类型一致
%rowtype:表示变量的数据类型与表对应的所有列的类型一致
可以不用知道列的数据类型、当列的数据类型改变后,修改pl/sql代码
被赋值的变量与select中的列名要一一对应。
declare
id varchar2(32); --证件号码
province varchar2(10); -省份编号
city varchar2(10); --城市编号
district varchar2(10); --区域编号
--定义省份、城市、区域编号记录表对象
type base_info_type is record(
province base_info.province%type,
city base_info.city%type,
district base_info.district%type);
sp_record base_info_type;
begin
id := sys_guid();
--查询出关联的省份编号、城市编号、区域编号信息
select province, city, district
into sp_record
from base_info bi
where bi.store_id = 'storeId ′ ;??更新省份编号、城市编号、区域编号信息updatetest h ousefohsetfoh.province=sp r ecord.province,foh.city=sp r ecord.city,foh.region=sp r ecord.district,foh.address= ′ 商务路 ′ ||lpad(abs(dbms r andom.random),4,dbms r andom.string( ′ x ′ ,2))wherefoh.order i d= ′ storeId′;??更新省份编号、城市编号、区域编号信息updatetesthousefohsetfoh.province=sprecord.province,foh.city=sprecord.city,foh.region=sprecord.district,foh.address=′商务路′||lpad(abs(dbmsrandom.random),4,dbmsrandom.string(′x′,2))wherefoh.orderid=′{orderId}';
commit;
end;
2)数组类型:具有相同数据类型的记录的集合。
type array_name is varray(size) of elementType [not null];
array_name:数组类型名称 size:元素的大小 elementType:数据类型
--位置从1开始
declare
type city_array is varray(3) of varchar2(10);
v_city_array city_array;
begin
v_city_array := city_array('北京市', '上海市', '深圳市');
dbms_output.put_line('第3个城市名称 =' || v_city_array(3));
end;
1、绑定变量:使用variable来定义
variable return_cityId number;
SQL> variable returnValue number;
SQL> begin
2 select 3*6 into :returnValue from dual;
3 end;
4
PL/SQL procedure successfully completed
returnValue
---------
18
SQL> print returnValue;
returnValue
---------
3)表类型:定义记录表(或索引表)数据类型。它与记录类型相似,但它是对记录类型的扩展。它可以处理多行记录,类似于高级中的二维数组,使得可以在pl/sql中模仿其他数据库中的表。
type table is table of elementType [not null]
index by [binary_integer | pls_integer |varray2]
关键字index by表示创建一个主键索引,以便引用记录表变量中的特定行
--按一维数组使用记录表的示例
declare
type city_table is table of varchar2(20) index by binary_integer;
v_city_table city_table;
begin
v_city_table(1) := '北京市 ';
v_city_table(2) := ' 深圳市 ';
dbms_output.put_line(' 第2个城市名称 = ' || v_city_table(2));
end;
--按二维数组使用记录表的示例
declare
type bse_city_table is table of test_city%rowtype index by binary_integer;
v_bse_city_table bse_city_table;
begin
select city_id, city_name
into v_bse_city_table(1).city_id,v_bse_city_table(1).city_name
from test_city bc
where bc.p_city_id = '020'
and rownum = 1;
select city_id, city_name
into v_bse_city_table(2).city_id,v_bse_city_table(2).city_name
from test_city bc
where bc.p_city_id = '0755'
and rownum = 1;
dbms_output.put_line('记录1中区域编号=' || v_bse_city_table(1).city_id ||
'_记录1中区域名称=' || v_bse_city_table(1).city_name);
dbms_output.put_line('记录1中区域编号=' || v_bse_city_table(2).city_id ||
'_记录1中区域名称=' || v_bse_city_table(2).city_name);
end;
待续......
本文分享自微信公众号 - Oracle优化大师,如有侵权,请联系 service001@enmotech.com 删除。