从 Informix 存储过程返回的数据源

发布于 2024-07-11 14:47:49 字数 1546 浏览 10 评论 0原文

我有一个 Informix 存储过程,它返回两列和多行。 我可以使用“EXECUTE FUNCTION curr_sess(2009,'SP')”,但是如何将结果放入临时表中。

编辑:我们正在使用版本 10.00.HC5


测试 Jonathan Leffler 的想法不起作用。

EXECUTE FUNCTION curr_sess(2009,'SP')

工作正常。 然后我做了

CREATE TEMP TABLE t12(yr smallint, sess char(4));

但是当我尝试

INSERT INTO t12 EXECUTE FUNCTION curr_sess(2009,'SP');

它不起作用时,我得到一个“SPL例程中的非法SQL语句”。 错误。


curr_sess 编辑的来源

begin procedure

DEFINE _yr smallint;
DEFINE _sess char(4);

SELECT
    DISTINCT
    sess_vw.yr,
    sess_vw.sess,
    sess_vw.sess_sort
FROM
    sess_vw
ORDER BY
    sess_vw.sess_sort DESC
INTO temp tmp_sess WITH NO LOG;

SELECT
    FIRST 1
    tmp_sess.yr,
    tmp_sess.sess
FROM
    tmp_sess
WHERE
    tmp_sess.sess_sort = sess_sort(iYear,sSess)
INTO temp tmp_final WITH NO LOG;

FOREACH cursor1 FOR
    SELECT
        tmp_final.yr,
        tmp_final.sess
    INTO
        _yr,
        _sess
    FROM
         tmp_final
    RETURN _yr, _sess WITH RESUME;
END FOREACH;

DROP TABLE tmp_sess;
DROP TABLE tmp_final;

end procedure

: sess_sort() 进行查找。

我尝试将该函数重写为一个查询。 这是 next_sess:

SELECT
    FIRST 1
    sess_vw.sess_sort
FROM
    sess_vw
WHERE
    sess_vw.sess_sort > sess_sort(2009,'SP')
ORDER BY
    sess_vw.sess_sort ASC

来自 IBM 的某人给我发电子邮件并建议使用类似这样的东西:

SELECT
    *
FROM
    TABLE(next_sess(2009,'SP'))

但这仍然不起作用。

I have an Informix stored procedure that returns two columns and multiple rows. I can use "EXECUTE FUNCTION curr_sess(2009,'SP')" fine, but how do I get the results into a temp table.

EDIT: We are on version 10.00.HC5


Testing Jonathan Leffler's idea didn't work.

EXECUTE FUNCTION curr_sess(2009,'SP')

works fine. Then I did

CREATE TEMP TABLE t12(yr smallint, sess char(4));

But when I try

INSERT INTO t12 EXECUTE FUNCTION curr_sess(2009,'SP');

It doesn't work, I get a " Illegal SQL statement in SPL routine." error.


The source for curr_sess

begin procedure

DEFINE _yr smallint;
DEFINE _sess char(4);

SELECT
    DISTINCT
    sess_vw.yr,
    sess_vw.sess,
    sess_vw.sess_sort
FROM
    sess_vw
ORDER BY
    sess_vw.sess_sort DESC
INTO temp tmp_sess WITH NO LOG;

SELECT
    FIRST 1
    tmp_sess.yr,
    tmp_sess.sess
FROM
    tmp_sess
WHERE
    tmp_sess.sess_sort = sess_sort(iYear,sSess)
INTO temp tmp_final WITH NO LOG;

FOREACH cursor1 FOR
    SELECT
        tmp_final.yr,
        tmp_final.sess
    INTO
        _yr,
        _sess
    FROM
         tmp_final
    RETURN _yr, _sess WITH RESUME;
END FOREACH;

DROP TABLE tmp_sess;
DROP TABLE tmp_final;

end procedure

EDIT: sess_sort() does a lookup.

I have tried to rewrite the function as one query. Here is next_sess:

SELECT
    FIRST 1
    sess_vw.sess_sort
FROM
    sess_vw
WHERE
    sess_vw.sess_sort > sess_sort(2009,'SP')
ORDER BY
    sess_vw.sess_sort ASC

Someone from IBM emailed me and suggested using something like this:

SELECT
    *
FROM
    TABLE(next_sess(2009,'SP'))

But that still didn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

猫九 2024-07-18 14:47:49

一种可能性是存储过程。 另一个(在 IDS 11.50.FC1 上测试),我不确定是否有效,是:

CREATE PROCEDURE r12() RETURNING INT, INT;
   RETURN 1, 2 WITH RESUME;
   RETURN 2, 3 WITH RESUME;
END PROCEDURE;

CREATE TEMP TABLE t12(c1 INT, c2 INT);

INSERT INTO t12 EXECUTE PROCEDURE r12();

最后一行是重要的一行。


鉴于观察到存储过程无法如上所示执行(因为它包含一些未经允许的 SQL 语句),那么您需要以另一种方式使用存储过程 - 如此测试代码所示(有效:第一次有效,其中让我感到惊喜):

CREATE TEMP TABLE t12(yr smallint, sess char(4));

CREATE PROCEDURE curr_sess(yearnum SMALLINT, sesscode CHAR(2))
    RETURNING SMALLINT AS yr, CHAR(4) AS sess;
    RETURN yearnum, (sesscode || 'AD') WITH RESUME;
    RETURN yearnum, (sesscode || 'BC') WITH RESUME;
END PROCEDURE;

CREATE PROCEDURE r12(yearnum SMALLINT, sesscode CHAR(2))
    DEFINE yr SMALLINT;
    DEFINE sess CHAR(4);
    FOREACH EXECUTE PROCEDURE curr_sess(yearnum, sesscode) INTO yr, sess
    INSERT INTO t12 VALUES(yr, sess);
    END FOREACH;
END PROCEDURE;

EXECUTE PROCEDURE r12(2009,'SP');

SELECT * from t12;

您可以将临时表的创建合并到存储过程中; 您甚至可以安排删除与临时表同名的预先存在的表(使用异常处理)。 鉴于您使用的是 IDS 10.00,您必须使用临时表的固定名称。 尽管我不建议(我)使用 11.50 中的动态 SQL 工具在运行时命名临时表,但这是可能的。

请注意,访问临时表的存储过程在重用时会重新优化 - 使用的表与上次不同(因为它是临时表),因此查询计划没有太大帮助。

One possibility is a stored procedure. Another (tested on IDS 11.50.FC1), which I wasn't sure would work, is:

CREATE PROCEDURE r12() RETURNING INT, INT;
   RETURN 1, 2 WITH RESUME;
   RETURN 2, 3 WITH RESUME;
END PROCEDURE;

CREATE TEMP TABLE t12(c1 INT, c2 INT);

INSERT INTO t12 EXECUTE PROCEDURE r12();

The last line is the important one.


Given the observation that the stored procedure cannot be executed as shown just above (because it contains some non-permitted SQL statement), then you need to use stored procedures another way - illustrated by this test code (which works: worked first time, which pleasantly surprised me):

CREATE TEMP TABLE t12(yr smallint, sess char(4));

CREATE PROCEDURE curr_sess(yearnum SMALLINT, sesscode CHAR(2))
    RETURNING SMALLINT AS yr, CHAR(4) AS sess;
    RETURN yearnum, (sesscode || 'AD') WITH RESUME;
    RETURN yearnum, (sesscode || 'BC') WITH RESUME;
END PROCEDURE;

CREATE PROCEDURE r12(yearnum SMALLINT, sesscode CHAR(2))
    DEFINE yr SMALLINT;
    DEFINE sess CHAR(4);
    FOREACH EXECUTE PROCEDURE curr_sess(yearnum, sesscode) INTO yr, sess
    INSERT INTO t12 VALUES(yr, sess);
    END FOREACH;
END PROCEDURE;

EXECUTE PROCEDURE r12(2009,'SP');

SELECT * from t12;

You could incorporate the creation of the temp table into the stored procedure; you could even arrange to drop a pre-existing table with the same name as the temp table (use exception handling). Given that you're using IDS 10.00, you are stuck with a fixed name for the temp table. It would be possible, though not recommended (by me) to use the dynamic SQL facility in 11.50 to name the temp table at runtime.

Be aware that stored procedures that access temporary tables get reoptimized when reused - the table that is used is not the same as the last time (because it is a temporary) so the query plan isn't all that much help.

自在安然 2024-07-18 14:47:49

这可能会失败,因为“删除表”在此上下文中使用的过程中不是有效的语句?
http://publib.boulder.ibm.com/infocenter/idshelp/v115/topic/com.ibm.sqls.doc/ids_sqs_1755.htm#ids_sqs_1755

That fails possibly because the 'drop table' is not valid statment in a procedure that is used in this context?
http://publib.boulder.ibm.com/infocenter/idshelp/v115/topic/com.ibm.sqls.doc/ids_sqs_1755.htm#ids_sqs_1755

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