1.函数与存储过程
[color=red]函数必须有返回值[/color],存储过程有in out参数,不需要返回值。
[color=red]函数有确定性函数,关键字为deterministic,使用目的是该函数对于传入的相同的参数,其返回值是确定不变的[/color],例如个税计算等。数据库会使用之前的计算结果,提高效率。
[color=red]使用函数的原因是:可以将计算等封装复用。。。。。
使用存储过程的原因是:减少连接,提高效率;可复用;将复杂sql转为存储过程提高效率。[/color]
2.程序包(规范和主题的关系类似与接口和实现)
3.游标(显示游标 与 隐式游标)
显示游标相关:
显示游标的声明
[color=red]declare cursor cur is select.....[/color]
带有参数的显示游标的声明
[color=red]declare cursor [b]cur(name in varchar2(100), age in number) [/b]is.....[/color]
列类型变量声明
name student.name%type
行类型变量声明
student student%rowtype
游标的使用
[color=red]open cur;
fetch cur into student;
while cur%found loop
.....
.....
fetch cur into student;
end loop;
close cur;[/color]
[color=red][b]隐式游标相关:[/b][/color]
无需declare命令,不能被用户控制:fetch open close。
分为2种,[color=red]oracle预定义的sql隐式游标和 cursor for loop进行循环的隐式游标[/color]
使用示例:
...if sql%count >0 then
sql隐式游标只能用来获取属性信息。
...
[color=red]for student in (select * from student) loop
....
end loop[/color]
[color=darkred]动态游标:分为强类型和弱类型
强类型:[/color]
--可以返回内置类型,或者[color=red]自定义类型[/color]
type mytype is record{
id number,
age number
};
ret mytype;
[color=red]强类型必须指定返回类型[/color]
type dynamicCur is [color=red]ref[/color] cursor [color=red]return mytype[/color];
....
open dynamicCur for ..select....;
....游标循环用法同上...
[color=darkred]弱类型:[/color]
可以返回内置类型,或者[color=red]自定义类型[/color]
type mytype is record{
id number,
age number
};
ret mytype;
[color=red]弱类型不指定返回类型[/color]
type dynamicCur is [color=red]ref[/color] 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')
[color=red]函数必须有返回值[/color],存储过程有in out参数,不需要返回值。
[color=red]函数有确定性函数,关键字为deterministic,使用目的是该函数对于传入的相同的参数,其返回值是确定不变的[/color],例如个税计算等。数据库会使用之前的计算结果,提高效率。
[color=red]使用函数的原因是:可以将计算等封装复用。。。。。
使用存储过程的原因是:减少连接,提高效率;可复用;将复杂sql转为存储过程提高效率。[/color]
2.程序包(规范和主题的关系类似与接口和实现)
3.游标(显示游标 与 隐式游标)
显示游标相关:
显示游标的声明
[color=red]declare cursor cur is select.....[/color]
带有参数的显示游标的声明
[color=red]declare cursor [b]cur(name in varchar2(100), age in number) [/b]is.....[/color]
列类型变量声明
name student.name%type
行类型变量声明
student student%rowtype
游标的使用
[color=red]open cur;
fetch cur into student;
while cur%found loop
.....
.....
fetch cur into student;
end loop;
close cur;[/color]
[color=red][b]隐式游标相关:[/b][/color]
无需declare命令,不能被用户控制:fetch open close。
分为2种,[color=red]oracle预定义的sql隐式游标和 cursor for loop进行循环的隐式游标[/color]
使用示例:
...if sql%count >0 then
sql隐式游标只能用来获取属性信息。
...
[color=red]for student in (select * from student) loop
....
end loop[/color]
[color=darkred]动态游标:分为强类型和弱类型
强类型:[/color]
--可以返回内置类型,或者[color=red]自定义类型[/color]
type mytype is record{
id number,
age number
};
ret mytype;
[color=red]强类型必须指定返回类型[/color]
type dynamicCur is [color=red]ref[/color] cursor [color=red]return mytype[/color];
....
open dynamicCur for ..select....;
....游标循环用法同上...
[color=darkred]弱类型:[/color]
可以返回内置类型,或者[color=red]自定义类型[/color]
type mytype is record{
id number,
age number
};
ret mytype;
[color=red]弱类型不指定返回类型[/color]
type dynamicCur is [color=red]ref[/color] 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')