星空网 > 软件开发 > 数据库

PL/SQL笔记

流程控制

1.If,then,else,elsif(不是elseif)

if a='1' then null;endif;

PL/SQL笔记

PL/SQL笔记

PL/SQL笔记

 

2.Case

简单case表达式:

PL/SQL笔记

搜索型Case表达式:

PL/SQL笔记

 

3.goto语句

beginif true then goto label2;end if;<<label1>> SYS.DBMS_OUTPUT.PUT_LINE('label1'); <<label2>> SYS.DBMS_OUTPUT.PUT_LINE('label2');end;

 

循环控制

简单循环: Exit,Exit when condition退出循环

PL/SQL笔记

while循环:

PL/SQL笔记

 

for循环:两种,一个是面向数值的for,一种是面向游标的for

面向数值:   (..)是范围操作符,1..5表示1到5

PL/SQL笔记

beginFOR j IN 1..5 LOOP  dbms_output.Put_line(j); END LOOP;END;end;

面向游标:

PL/SQL笔记

declarecursor myCursor is select * from ouser;beginFOR s IN myCursor LOOP  dbms_output.Put_line(s.userid); END LOOP;END;

continue,continue when语句

结束本轮循环;

--只输出偶数beginFOR j IN 1..100 LOOP  Continue when Mod(j,2)=1;  dbms_output.Put_line(j);END LOOP;end;

异常处理

1.命名异常和匿名异常

命名异常有名字,匿名异常只有异常代码和消息

SQLCODE函数可以获取最后一个异常的异常代码,SQLERRM:异常消息

declaremyexception exception; --声明一个命名异常v_row Sys_ACC_User%RowType;Pragma EXCEPTION_INIT (myexception, -20002); --将一个命名异常和一个异常代码绑定begin select * into v_row from Sys_ACC_User where rownum=1; raise myexception;  --手动抛出异常 RAISE_APPLICATION_ERROR(-20001,'这是一个匿名异常,我没有名字'); --手动抛出一个匿名异常 Exception  when no_data_Found then --捕获名为no_data_found的异常   dbms_output.Put_line('not data found'||'异常代码:'||SQLCODE||' 异常消息'||SQLERRM);  when myexception then --捕获名为 myexception的异常   dbms_output.Put_line('myexception'||'异常代码:'||SQLCODE||' 异常消息'||SQLERRM);  when others then --其他命名异常和匿名异常在这里捕获   dbms_output.Put_line('异常代码:'||SQLCODE||' 异常消息'||SQLERRM);end;

 

Oracle块:

块组成:块头,声明单元,执行单元,异常处理单元

函数,存储过程均为块结构,命名块

PL/SQL笔记

create or replace function WordCount(str in varchar2)return number --块头is numCount number default:=0;--声明单元 begin --执行单元 return Length(LTrim(str,'0')); Exception --异常处理单元  when others then:   SYS.DBMS_OUTPUT.PUT_LINE('error');end ;

匿名块

匿名块没有块头

PL/SQL笔记

declare --声明单元 v_n1 varchar2(100);begin --执行单元 v_n1:='20'; SYS.DBMS_OUTPUT.PUT_LINE(v_n1); exception --异常处理单元  when others then   SYS.DBMS_OUTPUT.PUT_LINE('error');end;

 

其他:

1.转义: q’<s’d>’,表示为: s’d  ,<和>必须成对出现,可用(),{},[]等代替

2.Function必须返回值,不想返回值的用Procedure

3.如果Procedure有参数(In/Out),调用方式: ProcedureName(param1,param2);如果procedure没有参数,则直接: ProcedureName或者ProcedureName(),Function类似…

4.空字符 ’’is Null  =>true

 

 

zhxjdwh:http://www.cnblogs.com/zhxj/




原标题:PL/SQL笔记

关键词:sql

sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Carte Blue:https://www.goluckyvip.com/tag/16878.html
Carvana:https://www.goluckyvip.com/tag/16879.html
转行跨境电商:https://www.goluckyvip.com/tag/1688.html
CasasBahia:https://www.goluckyvip.com/tag/16880.html
Cascadia:https://www.goluckyvip.com/tag/16881.html
Case:https://www.goluckyvip.com/tag/16882.html
春季热卖单品!空气净化器单周销售额近三十万!:https://www.goluckyvip.com/news/188215.html
托克劳/Tokelau/托克劳群岛:https://www.kjdsnews.com/a/1836548.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流