大佬教程收集整理的这篇文章主要介绍了oracle游标,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
游标名%属性
动态游标:*/
1.使用隐式游标的属性,判断对雇员的工资修改是否成功
begin update luffy.student_test a set a.tot_cred = a.tot_cred + 1 where a.ID = 40116; if sql%FOUND then DBMS_OUTPUT.put_line('修改学生学分成功'); commit; else DBMS_OUTPUT.put_line('修改学生学分失败'); end if; end;
<pre name="code" class="sql">--2.显示游标
declare v_name VARCHAR2(20); v_cred number(3); cursor stu_cursor is SELEct a.name,a.tot_cred from luffy.student_test a where a.ID = 40116; begin open stu_cursor; fetch stu_cursor into v_name,v_cred; DBMS_output.put_line(v_name || ',' || v_cred); close stu_cursor; end;
--3.显示游标 使用记录变量
declare cursor stu_cursor is SELEct a.name,a.tot_cred,a.dept_name from luffy.student_test a where a.ID = 40116; stu_record stu_cursor%rOWTYPE; --记录变量 begin open stu_cursor; fetch stu_cursor into stu_record; DBMS_output.put_line(stu_record.name || ',' || stu_record.tot_cred || ',' || stu_record.dept_name); close stu_cursor; end;
--该程序在游标定义中使用了ORDER BY子句进行排序,并使用循环语句来提取多行数据。 declare v_name VARCHAR2(20); v_cred number(3); cursor stu_cursor is SELEct a.name,a.tot_cred from luffy.student_test a order by a.tot_cred desc; begin open stu_cursor; for I in 1..10 loop fetch stu_cursor into v_name,' || v_cred); end loop; close stu_cursor; end;
--可以看到该循环形式非常简单,隐含了记录变量的定义、游标的打开、提取和关闭过程。 --stu_record为隐含定义的记录变量,循环的执行次数与游标取得的数据的行数相一致。 declare cursor stu_cursor is SELEct a.name,a.dept_name,a.tot_cred from luffy.student_test a where rownum <= 100; begin for stu_record in stu_cursor loop DBMS_output.put_line(stu_record.name || ',' || stu_record.dept_name || ',' || stu_record.tot_cred); end loop; end;
declare v_name VARCHAR2(20); v_cred number(3); cursor stu_cursor is SELEct a.name,a.tot_cred from luffy.student_test a where rownum <= 4; begin open stu_cursor; if stu_cursor%ISOPEN then loop fetch stu_cursor into v_name,v_cred; exit when stu_cursor%NOTFOUND; DBMS_output.put_line(v_name || ',' || v_cred); end loop; DBMS_output.put_line('查找数量:' || stu_cursor%rOWCOUNT); else DBMS_output.put_line('没有打开游标'); end if; close stu_cursor; end;
--对于查询结果为一行的SELECT语句,可以用动态生成查询语句字符串的方法 --execute immediate 查询语句字符串 into 变量1[,变量2...]; declare v_name varchar2(100); v_cred number(3); str varchar2(150); begin str :='SELEct name,tot_cred from luffy.student_test a where a.ID = 40116'; execute immediate str into v_name,v_cred; DBMS_output.put_line(v_name || ',' || v_cred); end;
--使用了二重循环,在外循环体中,动态生成游标的SELECT语句,然后打开。 --通过语句letter:=chr(ascii(letter)+1)可获得字母表中的下一个字母。 declare type cur_type is ref cursor; cur cur_type; rec luffy.student_test%rowtype;-- str varchar2(100);--动态sql letter char := ''; begin loop str := 'SELEct name from luffy.student_test a where a.name like ''%' || letter || '%'''; open cur for str; DBMS_output.put_line('包含字母'||letter||'的名字: '); loop fetch cur into rec.name; exit when cur%notfound; DBMS_output.put_line(rec.Name); end loop; exit when letter='Z'; letter := chr(ascii(letter)+1); end loop; end;
--“NO_DATA_FOUND”是系统预定义的错误类型 declare v_name VARCHAR2(20); begin SELEct namE into v_name from luffy.student_test a where a.ID = 1234; DBMS_output.put_line('该学生名字为:' || v_Name); exception when no_data_found then DBMS_output.put_line('ID错误,没有找到对应的学生。'); when others then DBMS_output.put_line('发生其他错误!'); end;
DECLARE v_temp numbER(5) := 1; BEGIN v_temp := v_temp / 0; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_liNE('发生系统错误!'); DBMS_OUTPUT.PUT_liNE('错误代码:' || sqlCODE()); DBMS_OUTPUT.PUT_liNE('错误信息:' || sqlERRM()); END;
--说明:NulL_INSERT_ERROR是自定义异常,同系统错误1400相关联。 DECLARE V_ENAME VARCHAR2(10); NulL_INSERT_ERROR EXCEPTION; PRAGMA EXCEPTION_INIT(NulL_INSERT_ERROR,-1400); BEGIN INSERT INTO luffy.student_test (Name) VALUES (null); EXCEPTION WHEN NulL_INSERT_ERROR THEN DBMS_OUTPUT.PUT_liNE('无法插入NulL值!'); WHEN OTHERS THEN DBMS_OUTPUT.PUT_liNE('发生其他系统错误!'); END;
--插入新学生信息,限定插入ID的编号在80000~90000之间 declare new_id number(5); new_excp1 exception; new_excp2 exception; begin new_ID := 70000; insert into luffy.student_test (ID,name,dept_name,tot_cred) values (new_ID,'luffysan','haizei',5); if new_ID < 80000 then raise new_excp1; end if; if new_ID > 90000 then raise new_excp2; end if; commit; exception when new_excp1 then rollBACk; DBMS_output.put_line('学生ID号小于80000!'); when new_excp2 then rollBACk; DBMS_output.put_line('学生ID号大于90000!'); end;
declare new_ID := 22222; insert into luffy.student_test (ID,tot_cred) values (new_ID,5); if new_ID < 80000 then rollBACk; raise_application_error(); end if;
--说明:stu_rec变量是根据student_test表定义的记录变量,SELECT...INTO...语句将整个记录传给该变量。 --INSERT语句将整个记录变量插入student_test2表,如果插入成功(sql%FOUND为真),则提交事务,否则回滚撤销事务 declare v_id number(5) := 33546; stu_rec student_test%rowtype; begin SELEct * into stu_rec from luffy.student_test a where ID = v_ID; insert into luffy.student_test2 values stu_rec; if sql%found then commit; DBMS_output.put_line('复制成功!'); else rollBACk; DBMS_output.put_line('复制失败'); end if; end;
--第一个rpad函数产生对齐效果,第二个rpad函数根据学分产生不同数目的*。 --该程序采用了隐式的简略游标循环形式。 begin for re in (SELEct name,tot_cred from luffy.student_test a where rownum <= 1000) loop DBMS_output.put_line(rpad(re.name,12,' ') || rpad('*',re.tot_cred/2,'*')); end loop; end;
declare v_count number(5); v_sum number(5); v_dname varchar2(50); cursor List_cursor is SELEct a.dept_name,count(*),sum(a.tot_cred) from luffy.student_test a group by a.dept_name; begin open List_cursor; DBMS_output.put_line('--------------------学院统计-----------------'); DBMS_output.put_line('学院名称 总人数 总学分 '); fetch List_cursor into v_dname,v_count,v_sum; while List_cursor%found loop SELEct a.dept_name into v_dname from luffy.department a where a.dept_name = v_dname; DBMS_output.put_line(rpad(v_dname,15) || rpad(TO_CHAR(v_count),8) || rpad(TO_CHAR(v_sum),10)); fetch List_cursor into v_dname,v_sum; end loop; DBMS_output.put_line('---------------------------------------------'); close List_cursor; end;
--18.修改学分
--为学生增加学分 declare v_name VARCHAR2(20); v_cred number(3); v_addcred number(2) := 10;--增加的学分 v_num number(4) := 0; --增加学分的人数 cursor stu_cursor is SELEct a.name,a.tot_cred from luffy.student_test a order by a.tot_cred asc; begin open stu_cursor; DBMS_output.put_line('------------------------'); loop fetch stu_cursor into v_name,v_cred ; exit when stu_cursor%notfound; if v_cred < 50 then v_cred := v_cred + v_addcred; v_num := v_num + 1; DBMS_output.put_line(v_name || TO_CHAR(v_cred,'9999') || TO_CHAR(v_cred + v_addcred,'9999') ); update luffy.student_test set tot_cred = tot_cred + v_addcred where name = v_name; else DBMS_output.put_line(v_name || TO_CHAR(v_cred,'9999') || TO_CHAR(v_cred,'9999')); end if; end loop; DBMS_output.put_line('----------------------------'); DBMS_output.put_line('增加学分的人数:' || v_num); close stu_cursor; -- commit; end;
以上是大佬教程为你收集整理的oracle游标全部内容,希望文章能够帮你解决oracle游标所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。