Informix 存储过程返回值

发布于 2024-10-15 03:09:47 字数 184 浏览 1 评论 0原文

我需要创建一个返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我还不会笑 2024-10-22 03:09:47

将存储过程视为返回值的 SELECT 语句。

在 ESQL/C 等语言中,您准备“EXECUTE PROCEDURE”语句,然后为其声明一个游标,然后是 OPEN、循环中的 FETCH 和 CLOSE。类似的操作也应该在 JDBC 和 Java 中工作 - 可能的区别是您可能不需要分离准备和执行阶段。


我可以拼写“Java”——让我们想想;它以“C”开头...不是吗?像“C”一样?

在 ESQL/C 中,您可以这样写(忽略错误检查和变量声明):

EXEC SQL PREPARE prep_stmt FROM "EXECUTE PROCEDURE CursoryProcedure(?,?,?)";
EXEC SQL DECLARE cursor_nm FROM prep_stmt;
EXEC SQL OPEN cursor_nm USING :hostvar1, :hostvar2, :hostvar3;
while (SQLCODE == 0)
{
    EXEC SQL FETCH cursor_nm INTO :receiver1, :receiver2, :receiver3,
                                  :receiver4, :receiver5, :receiver6;
    if (SQLCODE != 0)
        break;
    ...use the values in the receiverN variables...
}
EXEC SQL CLOSE cursor_nm;
EXEC SQL FREE cursor_nm;
EXEC SQL FREE prep_stmt;

三个占位符问号代表传递到存储过程的输入值;这些值在 OPEN 操作中传递给它。存储过程可能返回(零或)一行或多行数据;每行依次处理,如图所示。

该代码与处理 SELECT 语句之间的唯一区别是 PREPARE 行:

select_str = "SELECT * FROM A_Table WHERE Col1 = ? AND (Col2 = ? OR Col3 < ?)";
EXEC SQL PREPARE prep_stmt FROM :select_str;

现在 - 您必须弄清楚如何将上面的 ESQL/C 转换为 JDBC。您可能不需要太担心 FREE 操作 - 在 ESQL/C 中,甚至这两个操作之一可能是多余的(但无害)。但需要复制 PREPARE、DECLARE、OPEN、FETCH-in-a-loop 和 CLOSE 序列。


我假设您可以阅读手册并知道如何编写 Informix 存储过程,但以防万一:

CREATE PROCEDURE CursoryProcedure(i INTEGER, j CHAR(10), k DATE)
    RETURNING CHAR(20) AS v1, INTEGER AS v2, DATE AS v3, 
              VARCHAR(255) AS v4, INTEGER AS v5, INTEGER AS v6; 
    DEFINE v1 CHAR(20);
    DEFINE v2 INTEGER;
    DEFINE v3 DATE;
    DEFINE v4 VARCHAR(255);
    DEFINE v5 INTEGER;
    DEFINE v6 INTEGER;
    FOREACH SELECT *
              INTO v1, v2, v3, v4, v5, v6
              FROM Table
             WHERE Col1 = i AND (Col2 = j OR Col3 < k)
        RETURN v1, v2, v3, v4, v5, v6 WITH RESUME;
    END FOREACH;
END PROCEDURE;

这是一个或多或少完成这项工作的最小过程——您不会经常编写一个简单返回无需对数据进行更多操作即可选择值。

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):

EXEC SQL PREPARE prep_stmt FROM "EXECUTE PROCEDURE CursoryProcedure(?,?,?)";
EXEC SQL DECLARE cursor_nm FROM prep_stmt;
EXEC SQL OPEN cursor_nm USING :hostvar1, :hostvar2, :hostvar3;
while (SQLCODE == 0)
{
    EXEC SQL FETCH cursor_nm INTO :receiver1, :receiver2, :receiver3,
                                  :receiver4, :receiver5, :receiver6;
    if (SQLCODE != 0)
        break;
    ...use the values in the receiverN variables...
}
EXEC SQL CLOSE cursor_nm;
EXEC SQL FREE cursor_nm;
EXEC SQL FREE prep_stmt;

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:

select_str = "SELECT * FROM A_Table WHERE Col1 = ? AND (Col2 = ? OR Col3 < ?)";
EXEC SQL PREPARE prep_stmt FROM :select_str;

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:

CREATE PROCEDURE CursoryProcedure(i INTEGER, j CHAR(10), k DATE)
    RETURNING CHAR(20) AS v1, INTEGER AS v2, DATE AS v3, 
              VARCHAR(255) AS v4, INTEGER AS v5, INTEGER AS v6; 
    DEFINE v1 CHAR(20);
    DEFINE v2 INTEGER;
    DEFINE v3 DATE;
    DEFINE v4 VARCHAR(255);
    DEFINE v5 INTEGER;
    DEFINE v6 INTEGER;
    FOREACH SELECT *
              INTO v1, v2, v3, v4, v5, v6
              FROM Table
             WHERE Col1 = i AND (Col2 = j OR Col3 < k)
        RETURN v1, v2, v3, v4, v5, v6 WITH RESUME;
    END FOREACH;
END PROCEDURE;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文