declare type cur_totalvaue_type isrefcursor; cur_totalvaue cur_totalvaue_type; sql_stat VARCHAR2(100); record_totalvalue t_threeyear_hour%rowtype; begin sql_stat := 'select * from t_threeyear_hour t where t.time = :1'; open cur_totalvaue for sql_stat using to_date('20130209', 'yyyymmdd'); loop fetch cur_totalvaue into record_totalvalue; exit when cur_totalvaue%NOTFOUND; dbms_output.put_line(record_totalvalue.time || ' is ' || record_totalvalue.totalvalue); endloop; close cur_totalvaue; end; /*批量动态SQL(BULK COLLECT INTO )*/ declare type ename_table_type istableof emp.ename%typeindexby binary_integer; type sal_table_type is table of emp.sal%type index by binary_integer; ename_table ename_table_type; sal_table sal_table_type; sql_stat varchar2(120); v_percent number := &percent; v_dno number := &dno; begin sql_stat := 'update emp set sal = sal * (1 + :percent / 100)' || ' where deptno = :dno' || ' returning ename, sal into :name, :salary'; executeimmediate sql_stat using v_percent, v_dno returningbulkcollect into ename_table, sal_table; for i in 1 .. ename_table.count loop dbms_output.put_line('employee ' || ename_table(i) || ' salary is: ' || sal_table(i)); endloop; end; /*动态游标+BULK*/ declare type cur_emp_type isrefcursor; cur_emp cur_emp_type; type ename_table_type is table of emp.ename%type index by binary_integer; ename_table ename_table_type; sql_stat varchar2(120); begin sql_stat := 'select ename from emp where deptno = :dno'; open cur_emp for sql_stat using &dno; fetch cur_emp bulk collect into ename_table; for i in 1 .. ename_table.count loop dbms_output.put_line('employee name is ' || ename_table(i)); endloop; close cur_emp; end; /*FORALL+BULK(仅支持DML)*/ declare type ename_table_type istableof tb2.ename%type; type sal_table_type is table of tb2.sal%type; ename_table ename_table_type; sal_table sal_table_type; sql_stat varchar2(100); begin ename_table := ename_table_type('blake', 'ford', 'miller'); --为复合类型赋值 sql_stat := 'update tb2 set sal = sal * 1.1where ename = :1returning sal into :2'; forall i in 1 .. ename_table.count execute immediate sql_stat using ename_table(i) returning bulk collect into sal_table; for j in 1 .. ename_table.count loop dbms_output.put_line('the new salary is' || sal_table(j) || 'for' || ename_table(j)); end loop; end;