存储sql的结果并在notifyix中处理

时间:2019-06-18 10:40:56

标签: informix

我们有一个包含2列的视图:pattern_start_time,pattern_end_time。 函数中的选择查询会将其转换为分钟,并使用该结果来处理未使用班次的覆盖率。正在创建该函数,但未进行处理并出现以下错误: SQLError [IX000]:例程(my_list)无法解决。 另外,请enter image description here建议循环直到结果的长度。

CREATE function myshifttesting(orgid int) returning int;
    DEFINE my_list LIST( INTEGER not null );
    DEFINE my_list1 LIST( INTEGER not null );
    define i, j, sub, sub1 int;
    define total int;
    TRACE ON;
    TRACE 'my testing starts';
    INSERT INTO TABLE( my_list ) 
        select
    ((extend(current, year to second) + (dots.v_shift_coverage.pattern_start_time - datetime(00:00) hour to minute) - current)::interval minute(9) to minute)::char(10)::INTEGER 
        from
            dots.v_shift_coverage 
        where
            org_guid = orgid;
    INSERT INTO TABLE( my_list1 ) 
        select
    ((extend(current, year to second) + (dots.v_shift_coverage.pattern_end_time - datetime(00:00) hour to minute) - current)::interval minute(9) to minute)::char(10)::INTEGER 
        from
            dots.v_shift_coverage 
        where
            org_guid = orgid;
    let sub = 0;
    let sub1 = 0;
    let total = 0;
    for j = 0 to 4
        if (my_list(j) < my_list1(j)) 
        then
            if (my_list(j + 1) > my_list1(j)) 
            then
                let sub = sub + my_list(j + 1) - my_list1(j);
            end if;
        end if;
    end for
    if (my_list(0) > my_list1(4)) 
    then
        let sub1 = my_list(0) - my_list1(4);
    end if;
    let total = sub + sub1;
    return total;

end function;

1 个答案:

答案 0 :(得分:1)

您收到的错误是因为my_list(j)是无效的Informix语法,无法访问LIST元素。 Informix将my_list(j)解释为对名为mylist的函数的调用。

您可以使用临时表通过逻辑“模拟”数组,如下所示:

CREATE TABLE somedata
(
    letter1 CHAR( 2 ),
    letter2 CHAR( 2 )
);
INSERT INTO somedata VALUES ( 'a1', 'a2' );
INSERT INTO somedata VALUES ( 'b1', 'b2' );
INSERT INTO somedata VALUES ( 'c1', 'c2' );
INSERT INTO somedata VALUES ( 'd1', 'd2' );
INSERT INTO somedata VALUES ( 'e1', 'e2' );


DROP FUNCTION IF EXISTS forloop;

CREATE FUNCTION forloop()
RETURNING CHAR( 2 ) AS letter1, CHAR( 2 ) AS letter2;

    DEFINE number_of_rows INTEGER;
    DEFINE iterator       INTEGER;
    DEFINE my_letter1     CHAR( 2 );
    DEFINE my_letter2     CHAR( 2 );

    -- Drop temp table if it already exists in the session
    DROP TABLE IF EXISTS tmp_data;
    CREATE TEMP TABLE tmp_data
    (
        tmp_id SERIAL,
        tmp_letter1 CHAR( 2 ),
        tmp_letter2 CHAR( 2 )
    );

    -- Insert rows into the temp table, serial column will be the access key
    INSERT INTO tmp_data
    SELECT 0, 
        d.letter1,
        d.letter2
    FROM somedata AS d
    ORDER BY d.letter1;

    -- Get total rows of temp table
    SELECT COUNT( * ) 
    INTO number_of_rows
    FROM tmp_data;

    FOR iterator = 1 TO number_of_rows
        SELECT d.tmp_letter1
        INTO my_letter1
        FROM tmp_data AS d
        WHERE d.tmp_id = iterator;
        -- Check if not going "out of range"
        IF iterator < number_of_rows THEN
            SELECT d.tmp_letter2
            INTO my_letter2
            FROM tmp_data AS d
            WHERE d.tmp_id = iterator + 1;
        ELSE
            -- iterator + 1 is "out of range", return to the beginning
            SELECT d.tmp_letter2
            INTO my_letter2
            FROM tmp_data AS d
            WHERE d.tmp_id = 1;        
        END IF;

        RETURN my_letter1, my_letter2 WITH RESUME;

    END FOR;

END FUNCTION;

-- Running the function
EXECUTE FUNCTION forloop();

-- Results
letter1 letter2

a1      b2
b1      c2
c1      d2
d1      e2
e1      a2

 5 row(s) retrieved.