文档详情

游标异常动态执行sql语句(Cursor exception dynamically executes SQL statements).doc

发布:2017-08-19约7.55千字共16页下载文档
文本预览下载声明
游标异常动态执行sql语句(Cursor exception dynamically executes SQL statements) ---------------- 动态sql declare sql _ stmt varchar2 (200); the emp _ id number (4): = 7563; the emp _ rec emp% rowtype; begin exceute immediate create table bonusl (id number, new number); - - - - - - - -: id相当于一个参数变量.就像是java中的? sql _ stmt: = select * from emp where empno =: id; exceute immediate sql _ stmt into the emp _ rec using emp _ id; end; ------------------ 异常 - - - - - - - - - - - - - - - - 内置异常 declare ordernum varchar2 (10); begin select orderno into ordernum from order _ master; - - - - - - - - orthers可以捕获所有错误异常.本例中的例异常是一个变量接受多行. exception when orthers then dbms _ output.put _ line (返回多行 ); end; - - - - - - - - - - - - - - - - 用户自定异常 declare invalid exception; - - - - - - - - 定义自己定义的异常 category varchar2 (10); begin category: = category; - - 会弹出输入对话框. if cartegory not in (附件 , 顶盖 , 备件 ) then raise invalid; - - - 抛出自定义异常 else dbms _ output.put _ line (你输入的类别是: | | category); end if; exception when invalid then dbms _ output.put _ line (无法识别该类别 ); end; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ---------------------- 游标 ------ 隐式游标 影响一行. oracle为隐式游标的名为sql.如insert, update, delete, select都是隐式游标.有四个属性 sql% found: 只有在dml (数据操纵语句) 语句影响一行或多行时, 返回true. sql% notfound: 与上一个属性正好相反.如果dml没有影响任何行, 则返回true. sql% rowcount: 返回dml语句影响的行数.如果没有影响任何行.则返回0. sql% isopen: 是否打开.默认是false. oracle会自动的打开或关闭隐式游标. begin update order _ master set ordername = rose where orderno = 123; if sql% found then dbms _ output.put _ line (表已更新 ); dbms _ output.put _ line (影响的行数: | | sql% rowcount); elsif sql% notfound then dbms _ output.put _ line (没有更新, 编号未找到 ); elsif sql% rowcount = 0 then dbms _ output.put _ line (没有影响任何行 ); end if; end; -------- 显式游标 影响多行, 可以查询返回的行集可以包含0行或多行.这些行称为活动集.游标将指向活动集中的当前行. Open: open cursor Fetch: extracts rows from cursors Close: closes the cursor There are four attributes, too Explicit cursor%found: if the implementation of the la
显示全部
相似文档