设为首页 加入收藏

TOP

PL/SQL编程基础简介及实践(二)
2018-03-18 16:21:36 】 浏览:200
Tags:PL/SQL 编程 基础 简介 实践
号信息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;


8、运算符
1、关系运算符:
=、<> ~= != ^= 、>、>=、<、<=
2、一般运算符:
+、-、*、/、:=(赋值号)、..(范围运算符)、||、=>(关系号)
3、逻辑运算符:
is null、in、and、or、not、between and
4、注意事项:
1)变量赋值:先声明再赋值。
v_storePhone varchar2(11); --手机号码
v_storePhone := '158' || lpad(abs(dbms_random.random), 8, 0);
2)null+数字 为null,null||字符串 为字符串
3)boolean类型的值只能取 true false null3个值


9、流程控制语句
1)语句分类:控制语句(IF)、循环语句(LOOP 、EXIT) 顺序语句(GOTO、NULL)
2)结构说明
a)
IF <布尔表达式> THEN
 PL/SQL语句和SQL语句
END IF;
b)
IF <布尔表达式> THEN
 PL/SQL语句和SQL语句
ELSE
其他语句
END IF;


IF <布尔表达式1> THEN
 PL/SQL语句和SQL语句1
ELSIF <布尔表达式2> THEN
其他语句1
ELSIF <布尔表达式3> THEN
其他语句2
ELSE
其他语句3
END IF;


IF语句示例
declare
 v_roleId varchar2(20); --角色编号
v_result varchar2(60);
begin
 for vv in (select distinct su.role_id
 from test_ur su
 where su.role_id in ('project_sz',
 'project_bj',
 'project_gz',
 'project_sh')) loop
 if (vv.role_id = 'project_sz') then
 v_result := vv.role_id || '_表示的是_角色1';
 dbms_output.put_line(v_result);
 elsif (vv.role_id = 'project_sh') then
 v_result := vv.role_id || '_表示的是_角色2';
 dbms_output.put_line(v_result);


elsif (vv.role_id = 'project_gz') then
 

首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Oracle 18C 建库提示 ORA-12754 .. 下一篇MySQL中的DDL(Data Definition La..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目