我有30个表,即second_data_0,second_data_1,..........,second_data_29。我将这些表与另一个表first_data逐个连接,并将结果存储在Mysql存储过程的临时表中(如下所示)。有人能告诉我如何在oracle程序中执行此操作,尤其是SET @s = CONCAT("INSERT INTO firstTemp SELECT a.ID,b.CLSNO,b.FEES,b.FEES_T,b.FEES_DT FROM first_data a, second_data_",i," b WHERE a.CLASS_NO = b.CLSNO AND (b.FEES < a.FEES_AMOUNT) AND b.FEES_DT BETWEEN DATE('2017-11-20') AND DATE('2017-12-25')");
部分吗?
SET i = 1;
CREATE TEMPORARY TABLE firstTemp
SELECT a.ID,b.CLSNO,b.FEES,b.FEES_T,b.FEES_DT FROM first_data a, second_data_0 b WHERE a.CLASS_NO = b.CLSNO AND (b.FEES < a.FEES_AMOUNT) AND b.FEES_DT BETWEEN DATE('2017-11-20') AND DATE('2017-12-25');
label1: WHILE i < 30 DO
SET @s = CONCAT("INSERT INTO firstTemp SELECT a.ID,b.CLSNO,b.FEES,b.FEES_T,b.FEES_DT FROM first_data a, second_data_",i," b WHERE a.CLASS_NO = b.CLSNO AND (b.FEES < a.FEES_AMOUNT) AND b.FEES_DT BETWEEN DATE('2017-11-20') AND DATE('2017-12-25')");
PREPARE stmt1 FROM @s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
SET i = i + 1;
END WHILE label1;
答案 0 :(得分:0)
试试这个
stmt := 'CREATE TEMPORARY TABLE firstTemp(list if columns) ON COMMIT DELETE ROWS';
execute immediate stmt;
WHILE (i < 30) loop
stmt2 := 'INSERT INTO firstTemp values( SELECT a.ID,b.CLSNO,b.FEES,b.FEES_T,b.FEES_DT FROM first_data a, second_data_' || i ||
' b WHERE a.CLASS_NO = b.CLSNO AND (b.FEES < a.FEES_AMOUNT) AND b.FEES_DT BETWEEN DATE(''2017-11-20'') AND DATE(''2017-12-25''))';
execute immediate stmt2;
i := i + 1;
end loop;
stmt和stmt2都是长变量,i是整数。
答案 1 :(得分:0)
开始之前的一个重要提示:请勿使用JOINS
的逗号(a,b)语法,使用JOIN ON
语法。
您可以在PL / SQL中使用数字FOR LOOP
。
BEGIN
EXECUTE IMMEDIATE
'
CREATE GLOBAL TEMPORARY TABLE firstTemp
AS
SELECT a.ID,
b.CLSNO,
b.FEES,
b.FEES_T,
b.FEES_DT
FROM first_data a
JOIN second_data_0 b
ON a.CLASS_NO = b.CLSNO AND b.FEES < a.FEES_AMOUNT
WHERE b.FEES_DT BETWEEN DATE ''2017-11-20'' AND DATE ''2017-12-25''';
FOR i IN 1 .. 30
LOOP
EXECUTE IMMEDIATE
'
INSERT INTO firstTemp
SELECT a.ID,
b.CLSNO,
b.FEES,
b.FEES_T,
b.FEES_DT
FROM first_data a JOIN second_data_'
|| i
|| ' b ON a.CLASS_NO = b.CLSNO AND b.FEES < a.FEES_AMOUNT WHERE b.FEES_DT
BETWEEN DATE ''2017-11-20'' AND DATE ''2017-12-25''';
END LOOP;
END;