Simple example using oratcl:
Create Table and simple package
CREATE TABLE test (id INTEGER,val INTEGER);
INSERT INTO test (id,val) values (1,10);
INSERT INTO test (id,val) values (2,20);
INSERT INTO test (id,val) values (3,30);
INSERT INTO test (id,val) values (4,40);
CREATE OR REPLACE PACKAGE tst is
FUNCTION get_test RETURN sys_refcursor;
END;
/
CREATE OR REPLACE PACKAGE BODY tst is
FUNCTION get_test RETURN sys_refcursor
IS
retval sys_refcursor;
BEGIN
OPEN retval FOR SELECT * FROM test ORDER BY id;
RETURN retval;
END;
END;
/
Tcl functions are below:
package require Oratcl
proc getLoginStr {} {
set userName "xxx"
set password "xxxx"
set db "xxx"
append retval $userName "/" $password "@" $db
}
set _lda ""
set _sqlH ""
proc init {} {
global _lda
global _sqlH
set _lda [oralogon [getLoginStr]]
set _sqlH [oraopen $_lda]
}
proc prepare {} {
global _sqlH
set Sql {
begin
:retval := tst.get_test();
end;
}
::db_ora::parseSql $_sqlH $Sql
}
proc go {} {
global _lda
global _sqlH
set curH [oraopen $_lda]
set pv_lst [list :retval $curH]
orabind $_sqlH :retval $curH
oraexec $_sqlH
set retval ""
while {[orafetch $curH -datavariable row] == 0} {
puts "row : $row"
lappend retval $row
}
return $retval
}
Run following script once:
source test.tcl
init
prepare
go
Output:
row : 1 10
row : 2 20
row : 3 30
row : 4 40
{1 10} {2 20} {3 30} {4 40}
re-Run go procedure
go
Output:
row :
row :
row :
row :
row :
row :
row :
row :
row :
row :
ORA-24338: statement handle not executed
Any Ideas? Why script did not work on re-run? If I am not mistaken it should reuse the opened handle _SqlH.
I think you don’t need tcl to see this behavior. You can reproduce this with plain SQL. Once you have iterated over the
sys_refcursorthat has been returned bytst.get_test(), you cannot “rewind” it and iterate again.Consult the docs on how to use ref_cursors (10.2) http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/sqloperations.htm#i7106
So in this current form, you can only run the tcl
goagain after you also runprepareagain.