学习DB2数据库的使用,DB2游标是一定需要掌握的,下面对DB2游标定义及使用方面进行了详细的介绍,并举例说明,希望对您有所启迪。
定义游标:
DECLARE 游标名 CURSOR FOR
Select 语句;
打开游标:
OPEN 游标名;
取值:
FETCH 游标名 INTO 变量列表
例:
- DECLARE c1 CURSOR FOR
- SELECT CAST(salary AS DOUBLE)
- FROM staff
- WHERE DEPT = deptNumber
- ORDER BY salary;
- DECLARE EXIT HANDLER FOR NOT FOUND
- SET medianSalary = 6666;
- SET medianSalary = 0;
- SELECT COUNT(*) INTO v_numRecords
- FROM staff
- WHERE DEPT = deptNumber;
- OPEN c1;
- WHILE v_counter < (v_numRecords / 2 + 1) DO
- FETCH c1 INTO medianSalary;
- SET v_counterv_counter = v_counter + 1;
- END WHILE;
- CLOSE c1;
注:游标的申明如果放在中间段,要用”begin。。。end;”.段分割标志分割开。
【编辑推荐】