PL SQL笔记(三)
loopif credit_rating < 3 then..exit;end if; end loop;
select to_char(sysdate, 'YYYY-MM-DD HH24:MI:SS') from dual; select cast(sysdate as timestamp) from dual;
复合类型数据
1.记录:
declaretypeemp_record_typeis record(r_name emp.ename%type,r_job emp.job%type);emp_record emp_record_type; beginselect t.ename, t.job into emp_record from emp t where t.empno = '7369';dbms_output.put_line('ename = ' || emp_record.r_name || ', r_job = ' || emp_record.r_job); end; /
2.联合数组:
declaretypeemp_table_typeis table ofemp.ename%typeindex by binary_integer;emp_table emp_table_type; beginselect ename into emp_table(0) from emp where empno = '7369';dbms_output.put_line('ename = ' || emp_table(0)); end; /
3.嵌套表:
嵌套表和联合数组类似,但嵌套表可以作为列的数据类型使用,而联合数组不能。
create or replace type item_type as object (t_username varchar2(20),t_password varchar2(20),t_age smallint ); declaretype itemtable is table of item_type;v_table itemtable := itemtable(); beginv_table.extend;v_table(v_table.last) := item_type('dys', 'dys123', 10); end;
利用嵌套表当表列数据类型:
create or replace type itemtable is table of Item_Type; create table TestTable (address varchar2(100),phoneNumber varchar2(11),itemList itemtable ) nested table itemList store as itemList;
4.变长数组:
变长数组可以作为表列的数据类型或对象类型属性的数据类型,嵌套表长度没有限制,而变长数组长度有限制:
create or replace type idArray_Type as varray(100) of number;create or replace type item_type as object (v_itemCode char(10),v_name varchar2(20) );create or replace type itemArray as varray(10) of item_type;create table TestT (v_id number(8),items itemArray )
pl sql 基本结构:
declarev_id number(8) := 10;v_username varchar2(20); begindelete from A;insert into A values(v_id, 'ding', 'ding123');select username into v_username from A where id = v_id;dbms_output.put_line('v_username = ' || v_username);exceptionwhen no_data_found thendbms_output.put_line('no data'); end; /
常量:
declarePI constant number(9) := 3.1415926; begincommit; end;
变量:
declareage number(3) := 26; begincommit; end;
其他类型:
emp.empno%type emp%rowtype
分支:
if ... then
if sales > 10 thencompute_bonus(empid);update payroll set pay = pay + bonus where empno = emp_id; end if;
if .. then ... else
if trans_type = 'CR' thenupdate accounts set balance = balance + debit where ... elseupdate accounts set balance = balance - debit wehre ... end if;
if trans_type = 'CR' thenupdate accounts set balance = balance - debit where ... elseif new_balance >= minimum_balance thenupdate accounts set balance = balance - debit where ...elseraise insufficient_funds;end if; end if;
if .. then ...elsif
beginif sales > 50000 thenbonus := 1500;elsif sales > 35000 thenbonus := 500;elsebonus := 100;end if;insert into payroll values(emp_id, bonus...); end;
case语句:
case gradewhen 'A' thendbms_output.put_line('A');when 'B' thendbms_output.put_line('B');elsedbms_output_put_line('wrong!'); end case;
搜寻式case语句:
casewhen grade = 'A' thendbms_output.put_line('A');when grade = 'B' thendbms_output.put_line('B');elsedbms_output.put_line(''wrong!); end case;
loop
loop.... end loop;
exit(只能入到循环中,如果普通PL SQL 块要退出用return)
loopif a > 3 then...exit;end if; end loop;
exit .. when
loopfetch c1 into ...exit when c1%notfound;... end loop; close c1;
if == exit ... when
if a > 100 thenexit; end if;----------------------------------------------------------- exit when a > 100;
loop label(循环标签)
<<outer>> loop...loop...exit outer when ...end loop; end loop outer;
while ... loop
while a < 100 loop...select sal into salary from emp where x = x;...total := total + salary; end loop;
其他用法:
loop...exit when a > 10; end loop; -------------------------------------------- do{ } while() --------------------------------------------- done := false; while not done loop....done := boolean_expression; end loop;
for ... loop
declaretype datelist is table of date index by binary_integer;dates datelist;k constant integer := 5; beginfor j in 1 .. 3 loopdates(j * k) := sysdate;end loop; end;
select count(empno) into emp_count from emp; for i in 1 .. emp_count loop... end loop; ----------------------------------------------------------- <<main>> declarectr integer; begin...for ctr in 1 .. 25 loop...if main.ctr > 10 then...end if;end loop; end main;
for exit
for j in 1 .. 10 loopfetch cl into emp_rec;exit when cl%notfound;... end loop; ------------------------------------------------- <<outer>> for i in 1 .. 5 loop...for j in 1 .. 10 loopfetch cl into emp_rec;exit outer when cl%notfound;...end loop; end loop outer;
goto
declaredone boolean;for i in 1 .. 10 loopif done thengoto end_loop;end if;...<<end_loop>>null;end loop; endl;
declaremy_ename char(10); begin<<get_name>>select ename into my_ename from emp wher ...begin...goto get_name;end; end;
null
exceptionwhen zero_divide thenrollback;when value_error theninsert into errors values...when others thennull;
if rating > 90 thencompute_bonus(emp_id); elsenull end if;
DCL(数据控制语句)
权限 | 说明 |
create user | 创建其他用户(dba角色) |
drop user | 删除其他用户 |
select any table | 查询任何用户表或视图 |
create any table | 在任何表空间中创建表 |
drop any table | 删除在任何表空间中所创建的表 |
create session | 连接数据库 |
create table | 在用户自己表空间中创建表 |
create view | 在用户自己表空间中创建视图 |
create sequence | 在用户自己的表空间中创建序列 |
create proceudre | 在用业内自己表空间中创建存储过程 |
授权:
grant create any table to scott;
撤销授权:
revoke create any table from scott;
保存点:
savepoint a;
execute dbms_transaction.savepoint('B');
回滚保存点:
rollback to B;
exeucte dbms_transaction.rollback_savepoint('A');
回滚全部事物:
rollback;
execute dbms_transaction.rollback;
转载于:https://www.cnblogs.com/dingyingsi/p/3246587.html
PL SQL笔记(三)相关推荐
- pl/sql中三种游标循环效率对比
pl/sql中三种游标循环效率对比 - Oracle数据库栏目 - 红黑联盟 http://www.2cto.com/database/201307/224636.html 转载于:https://b ...
- Oracle PL/SQL 第三章--运算符与表达式
Oracle PL/SQL 第三章--运算符与表达式 目录 Oracle PL/SQL 第三章--运算符与表达式 1.运算符分类 1.1.算术运算符 1.2.关系运算符 1.3.比较运算符 1.4.逻 ...
- 二十三、oracle pl/sql分类三 包
包用于在逻辑上组合过程和函数,它由包规范和包体两部分组成. 1).我们可以使用create package命令来创建包,如: i.创建一个包sp_package ii.声明该包有一个过程update_ ...
- PL/SQL第三课(学习笔记)
TRIGGER触发器 1.TRIGGER与过程/函数类似 都是带有名字的执行块 都有声明,执行体和异常处理部分 2.TRIGGER与过程/函数的差别 TRIGGER必须存储在数 ...
- [Oracle]高效的PL/SQL程序设计(三)--Package的优点
使用Package的优点在于提供了必需的程序设计结构, 促进了模块化编程设计, 最重要的是Package断开了依赖链, 使得对某个数据库模式的改动不会导致整个模式的无效,从而避免了昂贵的重编译! 例如 ...
- PL/SQL入门,非常详细的笔记
PL/SQL入门,非常详细的笔记 -- PL/SQL简介: 1.PL/SQl是过程语言PL与结构化语言SQL结合而成的编程语言 2.PL/SQL引擎驻留在Oracle服务器中 3.该引擎接收PL/SQ ...
- Oracle PL/SQL语句基础学习笔记(上)
PL/SQL是ORACLE对标准数据库语言的扩展,ORACLE公司已经将PL/SQL整合到ORACLE 服务器和其他工具中了,近几年中更多的开发人员和DBA开始使用PL/SQL,本文将讲述PL/SQL ...
- 大型数据库复习笔记——PL/SQL
PL/SQL介绍 [优点] PL/SQL是ORACLE在标准SQL语言上的过程性扩张,允许嵌入SQL语句,允许定义常量和变量,允许过程语言结果,允许使用异常处理ORACLE错误. PL/SQL能提高程 ...
- PL/SQL详细介绍
PL/SQL笔记 PL/SQL块中只能直接嵌入SELECT,DML(INSERT,UPDATE,DELETE)以及事务控制语句(COMMIT,ROLLBACK,SAVEPOINT),而不能直接嵌入DD ...
最新文章
- Javascript中的类实现
- 三天流量有效期具体怎么算_信用证具体的费用怎么算?
- MySQL为表和字段取别名
- Spring-bean的作用域(六)
- ubuntu 配置ip地址命令
- 左移寄存器vhdl_用VHDL实现的通用循环移位寄存器
- Web前端三大框架的总结,你是否知道呢?
- 【机器学习基础】常用激活函数(激励函数)理解与总结
- python合并多个txt文件
- 自然语言处理(NLP)常用算法入门笔记
- 小米游戏本bios_年轻人的第一台游戏本?——小米游戏本2019评测
- 故障恢复控制台命令全攻略
- 天津少儿编程培训班费用多少呢?值不值?
- 【生活】教你有效戒糖
- 利用python将中文名转换为英文名
- 【汇正财经】沪指冲高回落,创业板全天领涨
- centos7 SFTP
- ubuntu server 14.04 编译安装xen4.4.2配置vtpm(三)——创建DomU(a PV VM)
- 服务器后还有一系列留后门,服务器留后门的原则是什么
- File does not exist or is not accessible:‘c:/Users/Administrator/Desktop/FX2_Stream_IN/FX2_Str
热门文章
- 区块链技术入门,都涉及哪些编程语言?
- 区块链编程完全指南:平台、语言与结论
- python构建json_如何使用Python构建JSON API
- Missing space before value for key ‘routes‘ key-spacing
- 1048 Find Coins(散列解法)
- UI设计培训分享:ui设计师如何培养设计思维?
- web应用的绝对路径和相对路径
- 如何使用OWASP Dependency Check的命令行(CLI)模式进行依赖库安全漏洞扫描
- 第163天:js面向对象-对象创建方式总结
- 最优化:拉格朗日乘子法