使用 oratcl 的简单示例:
创建表和简单包
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函数如下:
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
}
运行以下脚本一次:
source test.tcl
init
prepare
go
输出:
row : 1 10
row : 2 20
row : 3 30
row : 4 40
{1 10} {2 20} {3 30} {4 40}
重新运行 go 程序
go
输出:
row :
row :
row :
row :
row :
row :
row :
row :
row :
row :
ORA-24338: statement handle not executed
有任何想法吗?为什么脚本在重新运行时不起作用?如果我没记错的话,它应该重用打开的句柄_SqlH。