PL/SQL:open for [using] 语句

本文介绍PL/SQL中如何使用OPEN FOR和OPEN FOR USING语句来操作动态游标,提高程序灵活性。通过两个示例展示如何声明ref cursor类型,并使用动态SQL查询学生信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >




※ OPEN FOR [USING] 语句 ※


目的:
          和ref cursor配合使用, 可以将游标变量分配给不同的SQL (而不是在declare中把游标给定死), 增加处理游标的灵活性
语法:
declare
    type type_cursor  is ref cursor [return 记录类型];  --使用 ref cursor 才能把游标分配给不同的SQL,return不能用在动态SQL中
    v_cursor type_cursor ;
begin
     OPEN v_cursor FOR  select first_name, last_name from student ;

     OPEN v_cursor FOR  select first_name,last_name  from student where zip = :1 '
    USING 绑定变量1 ;


open 静态SQL cursor cursor c1 is <静态SQL文本>
open c1; fetch ... into ... ; close c1;
open for     静态SQL     ref cursor type t_1 is ref cursor;
c2  t_1 ;
open c2 for <静态SQL语句>;
open for using 动态SQL type t_1 is ref cursor;
c2  t_1 ;
open c2 for <动态SQL语句> using ... ;


例子1:
declare
  type student_cur_type is ref CURSOR  RETURN test_stu%ROWTYPE;   --声明ref cursor类型, return类型固定
    v_first_name test_stu.first_name%TYPE;
    v_last_name test_stu.last_name%TYPE;
  cur_stud student_cur_type;
begin
  open cur_stud for select first_name,last_name from student ;   --带return的ref cursor只能用在静态sql中
 loop
    fetch cur_stud into v_first_name, v_last_name;
    exit when cur_stud%NOTFOUND;
    dbms_output.put_line(v_first_name || ' ' || v_last_name);
  end loop;
  close cur_stud; 
end;

例子2:
declare
  v_zip        varchar2(5) := '&sv_zip';
  v_first_name varchar2(25);
  v_last_name  varchar2(25);
 
  type student_cur_type is ref cursor;  --声明ref cursor类型
  student_cur student_cur_type;  --student_cur是游标变量 / student_cur_type 是引用游标类型
begin
  --2打开游标变量,让它指向一个动态select查询的结果集 ; 就是使用open for语句代替一般的open语句代开游标
  open student_cur for 'select first_name,last_name ' from student where zip = :1'
    using v_zip;
  loop
    fetch student_cur into v_first_name, v_last_name;
    exit when student_cur%NOTFOUND;
    dbms_output.put_line(v_first_name || ' ' || v_last_name);
  end loop;
  close student_cur;
end;


※ OPEN FOR [USING] 语句 ※


目的:
          和ref cursor配合使用, 可以将游标变量分配给不同的SQL (而不是在declare中把游标给定死), 增加处理游标的灵活性
语法:
declare
    type type_cursor  is ref cursor [return 记录类型];  --使用 ref cursor 才能把游标分配给不同的SQL,return不能用在动态SQL中
    v_cursor type_cursor ;
begin
     OPEN v_cursor FOR  select first_name, last_name from student ;

     OPEN v_cursor FOR  select first_name,last_name  from student where zip = :1 '
    USING 绑定变量1 ;


open 静态SQL cursor cursor c1 is <静态SQL文本>
open c1; fetch ... into ... ; close c1;
open for     静态SQL     ref cursor type t_1 is ref cursor;
c2  t_1 ;
open c2 for <静态SQL语句>;
open for using 动态SQL type t_1 is ref cursor;
c2  t_1 ;
open c2 for <动态SQL语句> using ... ;


例子1:
declare
  type student_cur_type is ref CURSOR  RETURN test_stu%ROWTYPE;   --声明ref cursor类型, return类型固定
    v_first_name test_stu.first_name%TYPE;
    v_last_name test_stu.last_name%TYPE;
  cur_stud student_cur_type;
begin
  open cur_stud for select first_name,last_name from student ;   --带return的ref cursor只能用在静态sql中
 loop
    fetch cur_stud into v_first_name, v_last_name;
    exit when cur_stud%NOTFOUND;
    dbms_output.put_line(v_first_name || ' ' || v_last_name);
  end loop;
  close cur_stud; 
end;

例子2:
declare
  v_zip        varchar2(5) := '&sv_zip';
  v_first_name varchar2(25);
  v_last_name  varchar2(25);
 
  type student_cur_type is ref cursor;  --声明ref cursor类型
  student_cur student_cur_type;  --student_cur是游标变量 / student_cur_type 是引用游标类型
begin
  --2打开游标变量,让它指向一个动态select查询的结果集 ; 就是使用open for语句代替一般的open语句代开游标
  open student_cur for 'select first_name,last_name ' from student where zip = :1'
    using v_zip;
  loop
    fetch student_cur into v_first_name, v_last_name;
    exit when student_cur%NOTFOUND;
    dbms_output.put_line(v_first_name || ' ' || v_last_name);
  end loop;
  close student_cur;
end;

PL/SQL 中统一查询条件通常指的是将查询条件模块化、通用化,以便可以方便地应用于多个 SQL 查询语句中。这样做不仅提高了代码的复用性和维护性,还使得程序更为简洁。 为了实现这一目标,在 PL/SQL 程序设计里有几种常见的做法: 1. **游标(Cursors)** 和 **包(Packages)** - 可以把一些常用的 WHERE 条件封装到包里的函数或过程内,然后通过传递参数的方式来改变实际使用的过滤规则。 2. **动态SQL(Dynamic SQL)** - 使用 `EXECUTE IMMEDIATE` 或者 `OPEN FOR ... USING` 这样的语法构建包含变量部分的字符串形式的 SQL,并将其作为执行命令的一部分提交给数据库引擎处理。这允许我们在运行时刻才确定具体的表名、列名及查询条件等信息。 3. **视图(Views)** - 创建基于特定业务逻辑而构造出来的虚拟表格(即视图),并将那些固定的筛选标准嵌入其中;之后当需要在此基础上增加额外约束时只需简单调整顶层 SELECT 的 FROM 子句即可。 4. **公用表达式(Common Table Expressions, CTE)** - 它们也叫做 WITH 子查询块,可以在一定程度上达到类似的效果——即将复杂的子查询提炼出来形成独立单元供后续引用。 下面给出一段简单的例子演示如何利用包内的函数来管理查询条件: ```sql CREATE OR REPLACE PACKAGE emp_mgr IS FUNCTION get_deptno(dept_name VARCHAR2) RETURN NUMBER; END; CREATE OR REPLACE PACKAGE BODY emp_mgr IS FUNCTION get_deptno(dept_name VARCHAR2) RETURN NUMBER AS v_deptno departments.department_id%TYPE; BEGIN SELECT department_id INTO v_deptno FROM departments d WHERE d.department_name = dept_name; RETURN v_deptno; EXCEPTION WHEN NO_DATA_FOUND THEN dbms_output.put_line('No such department.'); RETURN NULL; END; END; -- 调用这个包来进行查询: SELECT e.* FROM employees e WHERE e.department_id = emp_mgr.get_deptno('Marketing'); ``` 在这个示例中我们创建了一个名为 EMP_MGR 的包用于获取部门编号 (DEPARTMENT_ID),并在主查询里面直接调用了它而不是硬编码某个固定值。这样的写法让我们的应用程序更易于理解和修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值