PL/SQL WITH 语句后的 FOR 循环
PL/SQL FOR loop after the WITH statement
我写了一个 WITH 语句如下
with tempTable as ( select t1.c1, t1.c2, t2.c3 from (t1 join t2))
select * from tempTable
给我:
c1
c2
c3
circle
blue
alpha
circle
red
beta
我想在查询中使用此语句,其中每条记录都使用 tempTable 列中的数据。我使用 select 'Random text' from DUAL
和 UNION ALL
向结果中添加行。结果在另一种编程语言中进一步使用。这是我想要得到的例子。
Random text
circle is shape
circle has color blue and red
Random text
而我想用循环实现第三行之类的。我没有创建视图的权限,这就是为什么我必须坚持使用 with 语句的原因。
问题是在 with 语句之后需要一个 select 。
我试过
BEGIN
for i in (select * from tempTable) LOOP
DBMS_OUTPUT.PUT_LINE (i)
END LOOP
END
问题是我无法从循环访问临时表。
temptable
不是 table 但它是 CTE。您可以直接在循环中使用它,如下所示:
BEGIN
for i in (select * from
(with tempTable as
( select t1.c1, t1.c2, t2.c3 from (t1 join t2))
select * from tempTable)
) LOOP
DBMS_OUTPUT.PUT_LINE (i);
END LOOP;
END;
我写了一个 WITH 语句如下
with tempTable as ( select t1.c1, t1.c2, t2.c3 from (t1 join t2))
select * from tempTable
给我:
c1 | c2 | c3 |
---|---|---|
circle | blue | alpha |
circle | red | beta |
我想在查询中使用此语句,其中每条记录都使用 tempTable 列中的数据。我使用 select 'Random text' from DUAL
和 UNION ALL
向结果中添加行。结果在另一种编程语言中进一步使用。这是我想要得到的例子。
Random text
circle is shape
circle has color blue and red
Random text
而我想用循环实现第三行之类的。我没有创建视图的权限,这就是为什么我必须坚持使用 with 语句的原因。 问题是在 with 语句之后需要一个 select 。 我试过
BEGIN
for i in (select * from tempTable) LOOP
DBMS_OUTPUT.PUT_LINE (i)
END LOOP
END
问题是我无法从循环访问临时表。
temptable
不是 table 但它是 CTE。您可以直接在循环中使用它,如下所示:
BEGIN
for i in (select * from
(with tempTable as
( select t1.c1, t1.c2, t2.c3 from (t1 join t2))
select * from tempTable)
) LOOP
DBMS_OUTPUT.PUT_LINE (i);
END LOOP;
END;