首页 > 解决方案 > 将连接表的值传递给函数参数

问题描述

我想知道是否有一种方法可以使用informix(IDS 12.xx)将参数传递给从连接表值中获取的函数

让我用代码解释一下这种情况:

-- we have 2 tables with some values
create table tab1 (
    c1 int,
    c2 varchar(10)
);

insert into tab1 values ( 1, 'one' );
insert into tab1 values ( 2, 'two' );

create table tab2 (
    c1 int,
    c2 varchar(10)
);

insert into tab2 values ( 1, 'one' );
insert into tab2 values ( 2, 'also two' );

然后我们有一个带有 1 个参数的函数

create function myfunc( myarg int )
returning varchar(10);
    define ret1 varchar(10);
    foreach cur for
        select c2 into ret1
        from tab2 where c1 <= myarg
        return ret1 with resume;
    end foreach;
end function;

现在,让我们运行一些查询:

-- we can build this function into a select statment:
select * from table( function myfunc( 3 ) ) as myfunctab2( c2 );

-- we can also join tab1 into this select statment
select * from tab1
join table( function myfunc( 2 ) ) as myfunctab2( c2 ) on myfunctab2.c2 = tab1.c2;

-- but trying to add a column from the joined table as an argument fails
select * from tab1
join table( function myfunc( tab1.c1 ) ) as myfunctab2( c2 ) on myfunctab2.c2 = tab1.c2;

我怎么知道myfunc从 中获取它的参数值tab1.c1

我知道该示例只需将函数参数替换为附加返回值,然后将其添加到ON... 查询部分即可,但因为这只是一个简化示例,所以它不是我的选择。

任何帮助将不胜感激,

谢谢

标签: sqlinformix

解决方案


推荐阅读