cursor

1.显式游标 ----对应于静态select语句

2.参数游标 ----对应于静态select语句

3.游标变量 REF  cursor----对应于动态select 语句

4.游标表达式 CURSOR(subquery)  --用于在plsql块中处理更加复杂的基于多张表的关联数据。为了在PL/SQL块中取得嵌套游标的数据,需要使用嵌套循环。

 

 

 

package里面不能定义游标。

 

 

记录类型

 select * from dept;
 declare
  dept_record dept%rowtype;
 begin
  dept_record.deptno:=60;
  dept_record.dname:='SUNNY';
  dept_record.loc:='BEIJING';
  INSERT INTO dept values dept_record;
  end;

 

 

 

 游标表达式的使用

DECLARE
   TYPE refcursor IS REF CURSOR;
   CURSOR  dept_cursor (no number) IS
   SELECT a.dname,CURSOR(SELECT ename,sal FROM emp WHERE deptno=a.deptno)
   FROM dept a WHERE a.deptno=no;
  
   empcur refcursor;
   v_dname dept.dname%TYPE;
   v_ename emp.ename%TYPE;
   v_sal emp.sal%TYPE;
  
BEGIN
   OPEN dept_cursor(&no);
   LOOP
       FETCH dept_cursor INTO v_ename,empcur;
       EXIT WHEN dept_cursor%NOTFOUND;
       dbms_output.put_line('部门名:'||v_dname);
       LOOP
           FETCH empcur INTO v_ename,v_sal;
           EXIT WHEN empcur%NOTFOUND;
           dbms_output.put_line('雇员名:'||v_ename||',工资'||v_sal);
       END LOOP;
    END LOOP;
    CLOSE dept_cursor;
END;

 

 

 

 游标For循环的用法:当使用游标FOR循环时,ORACLE会隐含地打开游标、提取游标数据并关闭游标。

 

 

DECLARE
      CURSOR emp_cursor IS SELECT ename,sal FROM emp;
BEGIN
      FOR emp_record IN emp_cursor LOOP
      dbms_output.put_line('第'||emp_cursor%ROWCOUNT||'个雇员: '||emp_record.ename);
      END LOOP;
END;  

 

简化后:

BEGIN
    FOR emp_record in(SELECT ename,sal FROM emp ) LOOP
        dbms_output.put_line(emp_record.ename);
    END LOOP;
END;       

 

 

 

 

 

在显示游标中使用 FETCH ...BULK COLLECT INTO ...LIMIT 提取部分数据

DECLARE
      TYPE name_array_type IS VARRAY(5) OF VARCHAR2(10);
      name_array name_array_type;
      CURSOR emp_cursor IS SELECT ename FROM emp;
      row INT:=5;
      v_count INT:=0;
BEGIN
      OPEN emp_cursor;
      LOOP
          FETCH emp_cursor BULK COLLECT INTO name_array LIMIT row;
          dbms_output.put('雇员名:');
          FOR i IN 1 ..(emp_cursor%ROWCOUNT-v_count) LOOP
             dbms_output.put(name_array(i)||' ');
          END LOOP;
          dbms_output.new_line;
          v_count:=emp_cursor%ROWCOUNT;
          EXIT when emp_cursor%NOTFOUND;
     END LOOP;
     CLOSE emp_cursor;
END;   

### Cursor Infinite Issue in Programming or Database Context In programming, especially within the realm of databases, cursors are used to traverse records in a dataset. When dealing with cursor-related infinite issues, these typically arise from improper handling of loop conditions or failure to advance the cursor correctly through data entries[^1]. An improperly managed cursor might lead to an endless loop scenario where the termination condition never gets met. For instance, consider a situation wherein a developer intends to iterate over rows returned by a SQL query using a cursor but forgets to include logic that moves the cursor forward after processing each row: ```sql DECLARE db_cursor CURSOR FOR SELECT id FROM users; OPEN db_cursor; FETCH NEXT FROM db_cursor INTO @id; WHILE @@FETCH_STATUS = 0 -- Missing increment operation leading to potential infinite loop BEGIN PRINT 'Processing user ID: ' + CAST(@id AS VARCHAR); FETCH NEXT FROM db_cursor INTO @id; -- Correct way to move cursor END CLOSE db_cursor; DEALLOCATE db_cursor; ``` To prevent such scenarios, it's crucial to ensure every iteration advances the position of the cursor properly so as not to revisit previously processed elements unless intentionally required. Additionally, implementing robust error checking mechanisms helps identify and rectify any anomalies early on during development rather than encountering them post-deployment which could be more challenging due to increased complexity and interdependencies between components.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值