Chinaunix首页 | 论坛 | 博客
  • 博客访问: 925204
  • 博文数量: 264
  • 博客积分: 10107
  • 博客等级: 上将
  • 技术积分: 2455
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-09 16:34
文章分类

全部博文(264)

文章存档

2012年(1)

2011年(11)

2010年(128)

2009年(82)

2008年(42)

我的朋友

分类: Oracle

2009-07-21 09:28:27

oracle游标测试代码

练习一:

 

Sql代码 复制代码
  1. declare  
  2.   v_deptno dept.deptno %TYPE := &p_deptno;   
  3. begin  
  4.   delete from emp where deptno = v_deptno;   
  5.   if sql%NOTFOUND then  
  6.      delete from dept where deptno = v_deptno;   
  7.      commit;   
  8.   end if;   
  9.   rollback;   
  10. end;  

 练习二:

Sql代码 复制代码
  1. declare  
  2.   v_empno emp.empno%TYPE;   
  3.   v_sal emp.sal%TYPE;   
  4.   v_new_sal emp.sal%TYPE;   
  5.   cursor c_cursor is select empno, sal from emp;   
  6. begin  
  7.   open c_cursor;   
  8.   loop   
  9.        fetch c_cursor into v_empno, v_sal;   
  10.        exit when c_cursor%notfound;   
  11.        if v_sal <= 1200 then  
  12.           v_new_sal := v_sal + 100;   
  13.           update emp set sal = v_new_sal where empno = v_empno;   
  14.           dbms_output.put_line(v_empno||'update'||v_sal);   
  15.        end if;   
  16.    end loop;   
  17.    dbms_output.put_line(c_cursor%rowcount);   
  18.    close c_cursor;             
  19. end;  

  练习三:输出从1到100之间的素数:

Sql代码 复制代码
  1. declare    
  2.        
  3.     type prime_arr is table of number index by binary_integer;   
  4.     v_prime_arr  prime_arr;   
  5.     v_x number := 2;   
  6.     v_y number;   
  7.     v_count number := 1;   
  8.        
  9. begin  
  10.       
  11.    while v_x <= 100   
  12.    loop   
  13.        v_y := 2;   
  14.        while v_y <= v_x   
  15.        loop   
  16.          if mod(v_x,v_y)=0 then    
  17.             
  18.          exit;   
  19.             
  20.          end if;   
  21.          v_y := v_y + 1;   
  22.        end loop ;   
  23.           
  24.        if v_y = v_x then  
  25.           v_prime_arr(v_count) := v_x;   
  26.           dbms_output.put_line(v_prime_arr(v_count));   
  27.           v_count := v_count + 1;   
  28.              
  29.        end if;   
  30.        v_x := v_x + 1;   
  31.     
  32.    end loop;   
  33. end;  

 

demo2:

Sql代码 复制代码
  1. declare  
  2.     type prime_arr is varray(30) of number;   
  3.     --type prime_arr is table of number index by binary_integer;   
  4.     v_list prime_arr;      
  5.     v_count int ;   
  6.     i int ;   
  7.     res int := 1;   
  8.     j int :=0;     
  9. begin  
  10.     v_list := prime_arr();   
  11.     v_list.extend(30);   
  12.     for v_count in 2..100 loop     
  13.         for i in 2..v_count-1 loop         
  14.             if mod(v_count , i) =0 --and (i != v_count)            
  15.             then                           
  16.              j:=v_count;                
  17.             end if;               
  18.         end loop;          
  19.         if j =0 then  
  20.             -- dbms_output.put_line(v_list(res));   
  21.             --dbms_output.put_line(v_count);   
  22.             v_list(res) :=v_count;   
  23.             dbms_output.put(v_list(res)||',');   
  24.             res :=res+1;               
  25.         end if;   
  26.         j :=0;   
  27.     end loop;   
  28.      dbms_output.put_line('');   
  29. end;  

 练习四:

Sql代码 复制代码
  1. DECLARE  
  2.    v_job   emp.job%TYPE;   
  3.    v_sal     emp.sal%TYPE;   
  4.    CURSOR c_cursor   
  5.    IS  
  6.       SELECT job, sal FROM emp WHERE ename like '%' || upper('&ename') ||'%';   
  7. BEGIN  
  8.    OPEN c_cursor;   
  9.    FETCH c_cursor INTO v_job, v_sal;   
  10.    WHILE c_cursor%FOUND   
  11.    LOOP   
  12.       DBMS_OUTPUT.put_line (v_job || '---' || TO_CHAR (v_sal));   
  13.       FETCH c_cursor INTO v_job, v_sal;   
  14.    END LOOP;   
  15.    CLOSE c_cursor;   
  16. END;  

 

阅读(648) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~