Oracle在存储过程中通过循环选择

时间:2017-11-17 01:07:33

标签: oracle stored-procedures

我尝试根据从另一个查询计算的过滤器数组从表中选择数据。我试图描述我想要完成的事情 在下面的伪代码中。

SELECT Equipment, MIN(TIME) as FractionStart, MAX(TIME) as FractionEnd
INTO FRACTIONS
FROM DATA
WHERE ID = 1
GROUP BY (Equipment)

/* Pseudo code */
FOR EACH ROW IN FRACTIONS
  INSERT INTO MYTABLE (SELECT * FROM EVENTTABLE WHERE EVTTIME BETWEEN ROW.FractionStart AND ROW.FractionEnd AND EVTAREA = ROW.Equipment);
FOR NEXT;

RETURN MYTABLE;

我一直在看游标,但我还没弄明白如何在循环中为它们添加行。我在看正确的功能吗?还是有更好的方法来解决这个问题?

1 个答案:

答案 0 :(得分:0)

你能试试吗?

INSERT INTO MYTABLE 
(
SELECT * FROM EVENTTABLE te,  
(
SELECT Equipment, MIN(TIME) as FractionStart, MAX(TIME) as FractionEnd
FROM DATA
WHERE ID = 1
GROUP BY (Equipment)
) td
WHERE te.EVTTIME BETWEEN td.FractionStart AND td.FractionEnd AND te.EVTAREA = td.Equipment
);