将多个值传递给另一个函数

时间:2015-09-07 20:30:09

标签: sql stored-procedures informix

我有一个调用另一个函数并使用其值的函数。我已经搜索并发现(并证明)你必须这样做:

SELECT * from TABLE(FUNCTION proc1()) as tab_proc1(val1, val2);

直到那里,一切都没问题,但是我的函数(在这种情况下是proc1())HAS值我需要传递,因为那个句子在另一个函数中我需要将结果赋值给某些值 - 比如:

select * into impo_ori, impt_ori, impo_des 
from Table(procedure fc_moneda(cmon, importe, cmon_ori, cmon_des));

当我执行以下功能时:

create procedure ala(cmon integer, importe decimal, cmon_ori integer, cmon_des integer)
returning boolean;

    define impo_ori, impt_ori, impo_des decimal;
    define ver boolean;

    select * into impo_ori, impt_ori, impo_des
    from Table(procedure fc_moneda(cmon, importe, cmon_ori, cmon_des));

    //continues... 

    return ver;

end procedure;

但是将fc_moneda中的变量(如cmon,importe ...等)视为值,而不是变量。如果我传递值,那句话就可以了 像:

select * into impo_ori,impt_ori,impo_des
from Table(procedure fc_moneda(1, 100, 1, 2));

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

我无法访问 10.00.UC1 ,我能做的最好的是 11.50.FC9

我已经尝试了下一个例子,它的确有效:

[infx1210@tardis ~]$ dbaccess -e test test.sql

Database selected.

CREATE TABLE tab1 (
    col1 INTEGER
);
Table created.

INSERT INTO tab1 VALUES (1);
1 row(s) inserted.

INSERT INTO tab1 VALUES (2);
1 row(s) inserted.

CREATE TABLE tab2 (
    col1 INTEGER
);
Table created.

CREATE FUNCTION sp1(arg1 INTEGER)
RETURNING INTEGER;

        DEFINE var INTEGER;

        SELECT col1 + 10 INTO var
        FROM tab1
        WHERE col1 = arg1;

        RETURN var;

END FUNCTION;
Routine created.

CREATE PROCEDURE sp2(arg1 INTEGER)

    DEFINE var INTEGER;

        CALL sp1(arg1) RETURNING var;

        INSERT INTO tab2 VALUES (var);

END PROCEDURE;
Routine created.

EXECUTE PROCEDURE sp2(1);
Routine executed.

SELECT * FROM tab2;

       col1

         11

1 row(s) retrieved.

CREATE PROCEDURE sp3(arg1 INTEGER)

    DEFINE var INTEGER;

        SELECT * INTO var
        FROM TABLE(procedure sp1(arg1));

        INSERT INTO tab2 VALUES (var);

END PROCEDURE;
Routine created.

EXECUTE PROCEDURE sp3(2);
Routine executed.

SELECT * FROM tab2;

       col1

         11
         12

2 row(s) retrieved.

Database closed.

[infx1210@tardis ~]$

查看您的测试似乎唯一有效的方法是使用CALL。但是你可以尝试另一种方法,将参数传递给临时表:

[infx1150@tardis ~]$ dbaccess -e test test.sql

Database selected.

CREATE TABLE tab1 (
        col1 INTEGER
);
Table created.

INSERT INTO tab1 VALUES (1);
1 row(s) inserted.

INSERT INTO tab1 VALUES (2);
1 row(s) inserted.

CREATE TABLE tab2 (
        col1 INTEGER
);
Table created.

CREATE FUNCTION sp1(arg1 INTEGER)
        RETURNING INTEGER;

        DEFINE var INTEGER;

        SELECT col1 + 10 INTO var
        FROM tab1
        WHERE col1 = arg1;

        RETURN var;

END FUNCTION;
Routine created.

CREATE PROCEDURE sp1(arg1 INTEGER)

        DEFINE var INTEGER;

        CREATE TEMP TABLE tmp1(
            col1 INTEGER
        ) WITH NO LOG;

        INSERT INTO tmp1 VALUES (arg1);

        SELECT sp1(col1) INTO var
        FROM tmp1;

        INSERT INTO tab2 VALUES (var);

END PROCEDURE;
Routine created.

EXECUTE PROCEDURE sp1(1);
Routine executed.

SELECT * FROM tab2;

       col1

         11

1 row(s) retrieved.

Database closed.

[infx1150@tardis ~]$

关于返回的多行,如果是预期的行为,则必须更改过程以处理返回的每一行,如:

[infx1150@tardis ~]$ dbaccess -e test test.sql

Database selected.

CREATE TABLE tabs(
    col1 INTEGER
);
Table created.

CREATE FUNCTION series(lim INTEGER)
RETURNING INTEGER;
        DEFINE i INTEGER;
        FOR i IN (1 to lim)
                RETURN i WITH RESUME;
        END FOR;
END FUNCTION;
Routine created.

CREATE PROCEDURE sp_i_tabs(lim INTEGER)
        DEFINE var INTEGER;

        FOREACH EXECUTE FUNCTION series(lim) INTO var
                INSERT INTO tabs VALUES (var);
        END FOREACH
END PROCEDURE;
Routine created.

EXECUTE PROCEDURE sp_i_tabs(5);
Routine executed.

SELECT * FROM tabs;

       col1

          1
          2
          3
          4
          5

5 row(s) retrieved.

Database closed.

[infx1150@tardis ~]$