See Pl/Sql tips, Nested tables and Arrays
Dynamic cursor example
--!! As you may see from this example the variable binding is only based on the order. Pay attention !!
create or replace function exampleDynamic return number is
result timestamp;
v1 varchar2(1) := 'I';
v2 number := 4334;
begin
execute immediate 'select decode(:op, ''D'', systimestamp, nvl(verify_date, systimestamp)) from '
|| rc.owner || '.' || rc.table_name || ' where seq= :seq' into result using v1, v2;
return result;
end; |
Cursor loop - EXPLICIT
cursor c1 is
select table_name from history_tables order by table_name;
rc c1%rowtype;
BEGIN
open c1;
LOOP
fetch c1 into rc;
exit when c1%notfound;
result := result || '''' || rc.table_name || '''';
END LOOP;
result := result || ';';
return result;
close c1;
END; |
Cursor loop - FOR
declare
cursor c1 is select object_name from all_objects where rownum < 5;
begin
FOR rc1 in c1 LOOP
dbms_output.put_line(c1%rowcount || ' ' || rc1.object_name);
END LOOP;
end;
|
Cursor attributes
%ISOPEN
- Returns TRUE if the cursor is open, FALSE if the cursor is closed.
%FOUND
- Returns INVALID_CURSOR if cursor is declared, but not open; or if cursor has been closed.
- Returns NULL if cursor is open, but fetch has not been executed.
- Returns TRUE if a successful fetch has been executed.
- Returns FALSE if no row was returned.
%NOTFOUND
- Returns INVALID_CURSOR if cursor is declared, but not open; or if cursor has been closed.
- Return NULL if cursor is open, but fetch has not been executed.
- Returns FALSE if a successful fetch has been executed.
- Returns TRUE if no row was returned.
%ROWCOUNT
- Returns INVALID_CURSOR if cursor is declared, but not open; or if cursor has been closed.
- Returns the number of rows fetched.
- The ROWCOUNT attribute doesn't give the real row count until you have iterated through the entire cursor.
In other words, you shouldn't rely on this attribute to tell you how many rows are in a cursor after it is opened. |