存储SQL的结果并在Informix

问题描述 投票:0回答:1

我们有一个包含2列的视图:tatter_start_time,tatter_end_time。 该功能中的选择查询将其转换为分钟,使用该结果我们正在处理以获取不使用的移位覆盖范围。函数正在创建,但是处理不会发生,并获得以下错误: sqlerror [ix000]:常规(my_list)无法解决。 另外,请在此处示出thermest thermest lop循环直到结果的长度。 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;

您收到的错误是因为
informix
1个回答
2
投票
不是有效的Informix语法来访问

LIST

元素。 Informix将
my_list(j)
解释为对名为
my_list
的函数的调用。
您可以使用临时表来“模仿”与您的逻辑的数组,类似的东西:
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.

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.