Informix 存储过程返回值
我需要创建一个返回 select 语句的存储过程,比如大约 4-5 列的值。
我需要从 Java 应用程序调用它,这样我就无法将输出放入临时表中,因为该临时表无法从另一个会话访问。所以我基本上需要调用存储过程,然后从返回值创建一个数组列表。
返回的数据将是select * from table
。
I need to create a stored procedure which returns a select statement, say about 4-5 columns with values.
I need to call that from a Java app so I can't put the output into a temp table as that temp table won't be accessible from another session. So I basically need to call the stored procedure then create an array list from the return values.
The returned data will be select * from table
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将存储过程视为返回值的 SELECT 语句。
在 ESQL/C 等语言中,您准备“EXECUTE PROCEDURE”语句,然后为其声明一个游标,然后是 OPEN、循环中的 FETCH 和 CLOSE。类似的操作也应该在 JDBC 和 Java 中工作 - 可能的区别是您可能不需要分离准备和执行阶段。
我可以拼写“Java”——让我们想想;它以“C”开头...不是吗?像“C”一样?
在 ESQL/C 中,您可以这样写(忽略错误检查和变量声明):
三个占位符问号代表传递到存储过程的输入值;这些值在 OPEN 操作中传递给它。存储过程可能返回(零或)一行或多行数据;每行依次处理,如图所示。
该代码与处理 SELECT 语句之间的唯一区别是 PREPARE 行:
现在 - 您必须弄清楚如何将上面的 ESQL/C 转换为 JDBC。您可能不需要太担心 FREE 操作 - 在 ESQL/C 中,甚至这两个操作之一可能是多余的(但无害)。但需要复制 PREPARE、DECLARE、OPEN、FETCH-in-a-loop 和 CLOSE 序列。
我假设您可以阅读手册并知道如何编写 Informix 存储过程,但以防万一:
这是一个或多或少完成这项工作的最小过程——您不会经常编写一个简单返回无需对数据进行更多操作即可选择值。
Treat the stored procedure as if it was a SELECT statement that returned the values.
In languages such as ESQL/C, you prepare the 'EXECUTE PROCEDURE' statement, then declare a cursor for it, followed by OPEN, FETCH in a loop and CLOSE. The analogous operations should work in JDBC and Java too - with the possible difference that you may not need to separate the prepare and execute phases.
I can spell 'Java' - let's think; it starts with 'C'...doesn't it? Like 'C'?
In ESQL/C, you would write (ignoring error checking and variable declarations):
The three placeholder question marks represent the input values passed into the stored procedure; the values are passed to it in the OPEN operation. The stored procedure may return (zero or) one or more rows of data; each row gets handled in turn as shown.
The only difference between that code and handling a SELECT statement is the PREPARE line:
Now - you are going to have to work out how to translate the ESQL/C above into JDBC. You probably don't need to worry so much about the FREE operations - in ESQL/C, even, one of those two may be redundant (but harmless). But the PREPARE, DECLARE, OPEN, FETCH-in-a-loop and CLOSE sequence will need to be replicated.
I'm assuming you can read the manual and know how to write an Informix stored procedure, but just in case:
This is a more or less minimal procedure that does the job -- you wouldn't often write a procedure that simply returns the values selected without doing something more with the data.