declare sqlstring varchar2(100); emprow emp_bak%rowtype; myno emp_bak.empno%type; begin --sqlstring:='create table emp_bak as select * from emp'; sqlstring:='select * from emp_bak where empno=:myno and sal>:mysal'; myno:='&请输入员工编号'; --动态SQL execute immediate sqlstring into emprow using myno,3001 ; dbms_output.put_line(emprow.sal); exception when no_data_found then dbms_output.put_line('输入的员工编号不存在'); end; select * from emp_bak --隐士游标 游标名字 SQL --隐式游标 是oracle 系统自动在运行DML语句的时候 生成的。 他自动打开 自动关闭。 -- 一般情况下 该游标 对于程序员来说 是透明的 declare begin update emp_bak set sal = 2500 where empno = 8888; --输出隐士游标中的一个属性 %rowcount 就是影响的行数 dbms_output.put_line(sql%rowcount); end; -- %ISOPEN - 游标是否打开 布尔值 -- %noopen 没有这个属性!! -- %FOUND – 游标中是否还有数据 -- %NOTFOUND – 游标中是否还有数据 -- %ROWCOUNT – SQL 语句影响的行数 -- CURSOR 游标 --显式游标 有程序员自己定义游标 自己定义 自己使用 --游标要使用的话 步骤 1 定义 2 打开 3 使用(循环) 4 关闭游标 declare --`1 定义游标 cursor mycursor is select ename,sal from emp_bak; emprow emp_bak%rowtype; begin -- 2 打开游标 open mycursor; -- 3 使用游标 loop -- 游标中的数据 是通过关键字 提取 fetch fetch mycursor into emprow.ename,emprow.sal; -- 1 提取数据 2 并且该游标会指向下一行 if(emprow.sal >2000 and emprow.sal<3000) then dbms_output.put_line(emprow.ename||' '|| emprow.sal); end if; -- dbms_output.put_line(emprow.ename||' '|| emprow.sal); exit when mycursor%NOTFOUND; --2 使用游标的%NOTFOUND属性检测游标是否还有数据 如果没有了 那么就退出循环!! end loop; -- 关闭游标 close mycursor; end; --简单写法 循环游标 declare --`1 定义游标 cursor mycursor is select ename,sal from emp_bak; begin for m in mycursor --这行代码 包含了 打开游标 提取游标 loop dbms_output.put_line(m.ename || m.sal); end loop; --循环结束 自动关闭游标 end; --带参数的游标 declare --`1 定义带参数游标 cursor mycursor(mysal emp_bak.sal%type) is select ename,sal from emp_bak where sal<mysal; emprow emp_bak%rowtype; sal emp_bak.sal%type; begin -- 2 打开游标并且给参数赋值 sal:='&请输入薪水'; open mycursor(sal); -- 3 使用游标 loop -- 游标中的数据 是通过关键字 提取 fetch fetch mycursor into emprow.ename,emprow.sal; -- 1 提取数据 2 并且该游标会指向下一行 dbms_output.put_line(emprow.ename||' '|| emprow.sal); exit when mycursor%NOTFOUND; --2 使用游标的%NOTFOUND属性检测游标是否还有数据 如果没有了 那么就退出循环!! end loop; -- 关闭游标 close mycursor; end; --带参数的游标的简单写法 declare --`1 定义带参数游标 cursor mycursor(mysal emp_bak.sal%type) is select ename,sal from emp_bak where sal<mysal; sal emp_bak.sal%type; begin -- 2 打开游标并且给参数赋值 sal:='&请输入薪水'; for m in mycursor(sal) loop dbms_output.put_line(m.ename||' '|| m.sal); end loop; end; --使用游标来更新数据 declare cursor mycursor is select * from emp_bak for update; --带更新的游标必须要加上一个语句 for update emprow emp_bak%rowtype; sal emp_bak.sal%type; begin open mycursor; -- 3 使用游标 loop -- 游标中的数据 是通过关键字 提取 fetch fetch mycursor into emprow; -- 1 提取数据 2 并且该游标会指向下一行 exit when mycursor%NOTFOUND; --2 使用游标的%NOTFOUND属性检测游标是否还有数据 如果没有了 那么就退出循环!! if(emprow.sal=800) then update emp_bak set sal = 801 where current of mycursor; --current of mycursor 表示游标当前这一行 elsif(emprow.sal> 900 and emprow.sal<2000) then update emp_bak set sal = 2000 where current of mycursor; --current of mycursor 表示游标当前这一行 end if; end loop; --循环完毕 提交 commit; -- 关闭游标 close mycursor; end; --使用游标来更新数据 declare cursor mycursor is select * from emp_bak for update; --带更新的游标必须要加上一个语句 for update emprow emp_bak%rowtype; sal emp_bak.sal%type; begin for m in mycursor loop if(m.sal=800) then -- update emp_bak set sal = 801 where current of mycursor; --current of mycursor 表示游标当前这一行 delete from emp_bak where current of mycursor; --删除 elsif(m.sal> 900 and m.sal<=2000) then update emp_bak set sal = 1800 where current of mycursor; --current of mycursor 表示游标当前这一行 end if; end loop; --循环完毕 提交 commit; end; -- 引用游标 1 不能使用简化方式写 2 不能使用更新方式 3 可以进行赋值 4 如果是弱类型的 可以让游标变量指向不同的查询结果 -- 引用游标 1 先定义一个游标类型 2 再定义游标变量 3 使用语句给游标变量赋值 -- 弱类型的引用游标 declare type mycursor is ref cursor; --定义了一个游标类型 int c mycursor; i int; emprow emp_bak%rowtype; deptrow dept%rowtype; begin open c for select * from emp_bak; --给游标变量赋值 并且打开 loop fetch c into emprow; exit when c%notfound; dbms_output.put_line(emprow.sal); end loop; close c; open c for select * from dept; loop fetch c into deptrow; exit when c%notfound; dbms_output.put_line(deptrow.dname); end loop; close c; end; -- 引用游标 1 先定义一个游标类型 2 再定义游标变量 3 使用语句给游标变量赋值 -- 强类型的引用游标 declare type mycursor is ref cursor return emp_bak%rowtype; --定义了一个强游标类型 c mycursor; k mycursor; i int; m int; emprow emp_bak%rowtype; deptrow dept%rowtype; begin open c for select * from emp_bak; --给游标变量赋值 并且打开 k:=c; i:=m; loop fetch k into emprow; exit when k%notfound; dbms_output.put_line(emprow.sal); end loop; close c; end;