我有第一个查询的结果,然后基于该结果我想与其他表连接,以便我可以选择查询结果和其他表中的字段。
我构建以下
BEGIN
FOR a_rec in (select col1, min(col2) as col2,col3 from a_tbl group by (col1, col3))
LOOP
select a_rec.col2, d_tbl.col4
from b_tbl b, c_tbl c, d_tbl d
where b.col1 = a_rec.col1 and c.col5 = b.col5 and d.col6 = c.col6;
END LOOP;
END;
然后它提醒我PLS-00428: an INTO clause is expected in this SELECT statement
,所以我创建了一个临时表,代码变成了:
BEGIN
FOR a_rec in (select col1, min(col2) as col2,col3 from a_tbl group by (col1, col3))
LOOP
select a_rec.col2, d_tbl.col4 into tmp_tbl
from b_tbl b, c_tbl c, d_tbl d
where b.col1 = a_rec.col1 and c.col5 = b.col5 and d.col6 = c.col6;
END LOOP;
END;
然后我收到类似PLS-00403: expression 'TMP_TBL' cannot be used as an INTO-target of a SELECT/FETCH statement
也许我应该将第一个查询结果放入临时表中,然后将其与其他表连接起来,尽管我想知道我是否可以在这样的过程中完成它?
答案 0 :(得分:2)
如果SELECT INTO
语句返回除单行之外的任何内容,则SELECT
会引发错误。所以这显然不是你想要的。由于tmp_tbl
是一个全局临时表,您可以在循环中执行INSERT SELECT
BEGIN
FOR a_rec in (select col1, min(col2) as col2,col3
from a_tbl
group by (col1, col3))
LOOP
INSERT INTO tmp_tbl( col2, col4 )
select a_rec.col2, d_tbl.col4
from b_tbl b,
c_tbl c,
d_tbl d
where b.col1 = a_rec.col1
and c.col5 = b.col5
and d.col6 = c.col6;
END LOOP;
END;
但是,由于您说您只是转而在SQL Developer中导出结果数据,因此临时表似乎并不特别有用。您可以删除所有PL / SQL,只需编写一个SELECT
语句
select a.col2, d.col4
from (select col1, min(col2) as col2, col3
from a_tbl
group by col1, col3) a,
b_tbl b,
c_tbl c,
d_tbl d
where b.col1 = a.col1
and c.col5 = b.col5
and d.col6 = c.col6;