PLSQL程序设计语言

发布时间 : 星期三 文章PLSQL程序设计语言更新完毕开始阅读

level_score := '优'; when score >=40 then level_score := '良'; when score >=30 then level_score := '及格'; else level_score := '挂科'; end case; dbms_output.put_line(level_score); end;

·循环结果:

·简单循环 loop

语句块;

end loop;

这种循环语句没有终止,必须进行人为的控制,一般通常是加入

exit或者exit的语句进行结束循环。

·for循环

for 循环的变量 in[reverse] 起始值 。。终止值 loop

语句块;

end loop;

for循环中,次数是知道(固定不变的),如果存在reverse,则

表示的倒序循环

·while循环

while 条件表达式 loop

语句块;

end loop;

通过条件表达式来控制循环的执行,如果条件表达式为真,则执行循环,如果为false,则终止循环。 范例:从1+2+3+4+。。。100,输出结果

·使用简单循环

declare v_sum number(5) := 0;--定义循环相加后的结果 v_int number(3) := 1;--定义循环的变量 begin loop v_sum := v_sum + v_int; v_int := v_int+1; exit when v_int>100;--当v_int大于100则退出循环 end loop; dbms_output.put_line('相加后结果为:' || v_sum); end; ·使用for循环

declare v_sum number(5) :=0; begin for i in 1..100 loop v_sum := v_sum +i; end loop; dbms_output.put_line('相加后结果为:' || v_sum); end; ·使用while循环

declare v_sum number(5) := 0;--定义循环相加后的结果 v_int number(3) := 1;--定义循环的变量 begin while v_int <= 100 loop v_sum := v_sum + v_int; v_int := v_int+1; end loop; dbms_output.put_line('相加结果:' || v_sum); end; 游标:是从数据库中查询出来的结果集放在内存中,游标就是指向该内存的指针,通过移动游标来取得该结果集中的每一笔数据,执行不同的操作。

游标的操作步骤:

·声明游标:声明游标就是定义游标的名称,并将这一游标与select

语句相关联。

语法:cursor 游标名称 is select 语句 其中select以及后面语句就是查询语句

·打开游标

语法:open 游标名,游标使用之前必须打开,游标打开之后

该指针指向的是结果集中的第一笔数据。

·将结果集中的的数据提取(fetch)到声明的变量中

每次fetch提取到数据之后,游标指针就移向下一笔数据:fetch

游标名 into 变量列表

into之后的变量列表用来存储游标中相应的字段,变量的个数、

顺序、类型都需要和select 语句中查询的结果集相对应。

·关闭游标

语法:close 游标名,游标使用结束,都必须关闭,用来释放

游标所占用的资源

范例:将查询的结果集打印

declare cursor dh02tstudent is select stu_nm ,java_score from dh02t_student;--定义游标 score number(2) := 0; sname varchar2(10); begin open dh02tstudent; fetch dh02tstudent into sname,score; dbms_output.put_line(sname||'-----'||score); fetch dh02tstudent into sname,score; dbms_output.put_line(sname||'-----'||score); fetch dh02tstudent into sname,score; dbms_output.put_line(sname||'-----'||score); close dh02tstudent; end;

游标的属性:在PLSQL中游标有四种属性

·第一种:%found,如果前一个fetch提取数据,如果提取到了这

个值则为真,否则为假。

·第二种:%notfound, 如果前一个fetch提取数据,如果提取到

了这个值则为假,否则为真。

·第三种:%isopen ,判断游标是否打开,如果打开则为真,否

则为假。

·第四种:%rowcount,返回游标当前提取的行数

游标的四种属性前三种都是逻辑型,第四种是数值型,游标的属性可以用于取得游标当前的状态。

联系合同范文客服:xxxxx#qq.com(#替换为@)