oracle 阅读笔记-1

1.函数与存储过程
函数必须有返回值,存储过程有in out参数,不需要返回值。
函数有确定性函数,关键字为deterministic,使用目的是该函数对于传入的相同的参数,其返回值是确定不变的,例如个税计算等。数据库会使用之前的计算结果,提高效率。

使用函数的原因是:可以将计算等封装复用。。。。。

使用存储过程的原因是:减少连接,提高效率;可复用;将复杂sql转为存储过程提高效率。


2.程序包(规范和主题的关系类似与接口和实现)


3.游标(显示游标 与 隐式游标)

显示游标相关:
显示游标的声明
declare cursor cur is select.....

带有参数的显示游标的声明
declare cursor cur(name in varchar2(100), age in number) is.....

列类型变量声明
name student.name%type

行类型变量声明
student student%rowtype

游标的使用
open cur;
fetch cur into student;
while cur%found loop
.....
.....
fetch cur into student;
end loop;
close cur;


隐式游标相关:
无需declare命令,不能被用户控制:fetch open close。
分为2种, oracle预定义的sql隐式游标和 cursor for loop进行循环的隐式游标

使用示例:
...if sql%count >0 then
sql隐式游标只能用来获取属性信息。

...
for student in (select * from student) loop
....
end loop


动态游标:分为强类型和弱类型
强类型:


--可以返回内置类型,或者 自定义类型
type mytype is record{
  id number,
  age number

};

ret mytype;

强类型必须指定返回类型
type dynamicCur is ref cursor return mytype;

....
open dynamicCur for ..select....;

....游标循环用法同上...



弱类型:

可以返回内置类型,或者 自定义类型
type mytype is record{
  id number,
  age number

};

ret mytype;

弱类型不指定返回类型
type dynamicCur is ref cursor ;

....
open dynamicCur for ..select....;
根据实际情况返回造作返回类型;
fetch dynamicCur into diffrenttype;
....游标循环用法同上...


4 触发器
行级触发器 与 语句级触发器
事前与事后

状态:inserting updating deleting
用法: ... if inserting then ...

5.系统函数
substr 字符位置从1开始,如substr('1234567',4,2)结果为45

instr 获得子字符串在父字符串中出现的位置,有instr(str,substr) instr(str,substr,startfrom)  instr(str,substr,startfrom,counts)三种形式。

to_char 将数值型转化为字符串 to_char(94.23,'999.00')  也可 将日期型转化为字符串 to_char(sysdate,'yyyy')




猜你喜欢

转载自swearyd7.iteye.com/blog/1559405