有爱网  好市网  小说 非主流美女图片 模板资源下载 免费手机主题下载 Mobile Development WPF Windows CE asp.net服务器组件开发 SharepPoint 序列化 XML 网络编程 wcf 设计模式 成语典故 时事娱乐 Seo 美食 健康 旅游指南 实验_资源下载 法律政策 access 社保 c#3.0 Visual Studio DIRECT3D GDI+ SOCKET HMTL&CSS JAVASCRIPT DB2 Oracle JSp 服务器 WinForms(c#) Asp T-SQL C++(VC++) C#2.0 老的数据 公告  新闻  日志  资源下载 
当前位置: 首页 > 新闻>Oracle游标实用本篇文章来源于:

Oracle游标实用本篇文章来源于:

来源:www.maillove.cn        发布日期:2010-02-01 22:45:21


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 emp
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;
 
本篇文章来源于:开发学院 http://edu.codepub.com   原文链接:http://edu.codepub.com/2010/0114/19797.php


来源:www.maillove.cn        发布日期:2010-02-01 22:45:21

------分隔线----------------------------
------分隔线----------------------------