如何通过 JDBC 从 Db2 匿名块 return 动态结果集?
How to return dynamic results sets from a Db2 anonymous block via JDBC?
我正在查看 Db2 LUW feature "returning result sets from SQL",它的工作方式似乎与 MySQL、SQL 服务器中的可能方式类似,通过 运行 一个简单的 SELECT
来自任何过程逻辑,或在 Oracle 中使用 DBMS_SQL.RETURN_RESULT
。以下匿名块似乎是有效的:
BEGIN
DECLARE i INTEGER DEFAULT 1;
WHILE i < 10 DO
BEGIN
DECLARE cur CURSOR WITH RETURN TO CLIENT FOR SELECT i FROM sysibm.dual;
OPEN cur;
SET i = i + 1;
END;
END WHILE;
END
然而,它产生了这个警告,并且没有任何结果被获取。海狸:
Procedure "BEGIN...END" returned "9" query result sets, which exceeds the defined limit "0".. SQLCODE=464, SQLSTATE=0100E, DRIVER=4.26.14
如果这是一个程序,我必须声明:
DYNAMIC RESULT SETS n
但是我怎样才能在匿名块中声明它呢?
如果不是来自过程,则不能return结果集
见docs
WITH RETURN
Specifies that the result table of the cursor is intended to be used as a result set that will be returned from a procedure. WITH
RETURN is relevant only if the DECLARE CURSOR statement is contained
with the source code for a procedure. In other cases, the precompiler
might accept the clause, but it has no effect.
我知道这可能只是一个例子,但是 return 1 到 10 之间的整数不需要过程语句,下面的查询也可以:
with loop(k) as (
values 1
union all select k+1 from loop where k < 10
)
select * from loop order by k
尝试以下操作。它至少适用于我的 11.1。
String str =
"begin"
+ " declare l_c CURSOR; "
+ " set l_c = cursor with hold for select empno from employee; "
+ " open l_c; "
+ " set ? = l_c; "
+ "end";
CallableStatement st = null;
ResultSet rs = null;
try
{
st = con.prepareCall(str);
st.registerOutParameter (1, com.ibm.db2.jcc.DB2Types.CURSOR);
st.execute();
rs = (java.sql.ResultSet) st.getObject(1);
if (rs != null)
{
while (rs.next())
System.out.println(rs.getString(1));
rs.close();
}
} ...
我正在查看 Db2 LUW feature "returning result sets from SQL",它的工作方式似乎与 MySQL、SQL 服务器中的可能方式类似,通过 运行 一个简单的 SELECT
来自任何过程逻辑,或在 Oracle 中使用 DBMS_SQL.RETURN_RESULT
。以下匿名块似乎是有效的:
BEGIN
DECLARE i INTEGER DEFAULT 1;
WHILE i < 10 DO
BEGIN
DECLARE cur CURSOR WITH RETURN TO CLIENT FOR SELECT i FROM sysibm.dual;
OPEN cur;
SET i = i + 1;
END;
END WHILE;
END
然而,它产生了这个警告,并且没有任何结果被获取。海狸:
Procedure "BEGIN...END" returned "9" query result sets, which exceeds the defined limit "0".. SQLCODE=464, SQLSTATE=0100E, DRIVER=4.26.14
如果这是一个程序,我必须声明:
DYNAMIC RESULT SETS n
但是我怎样才能在匿名块中声明它呢?
如果不是来自过程,则不能return结果集
见docs
WITH RETURN Specifies that the result table of the cursor is intended to be used as a result set that will be returned from a procedure. WITH RETURN is relevant only if the DECLARE CURSOR statement is contained with the source code for a procedure. In other cases, the precompiler might accept the clause, but it has no effect.
我知道这可能只是一个例子,但是 return 1 到 10 之间的整数不需要过程语句,下面的查询也可以:
with loop(k) as (
values 1
union all select k+1 from loop where k < 10
)
select * from loop order by k
尝试以下操作。它至少适用于我的 11.1。
String str =
"begin"
+ " declare l_c CURSOR; "
+ " set l_c = cursor with hold for select empno from employee; "
+ " open l_c; "
+ " set ? = l_c; "
+ "end";
CallableStatement st = null;
ResultSet rs = null;
try
{
st = con.prepareCall(str);
st.registerOutParameter (1, com.ibm.db2.jcc.DB2Types.CURSOR);
st.execute();
rs = (java.sql.ResultSet) st.getObject(1);
if (rs != null)
{
while (rs.next())
System.out.println(rs.getString(1));
rs.close();
}
} ...