关于此question,我正在使用以下代码来执行保存在表中的选择查询。
begin
for rec in (select col1,col2,col3 from Table_1) loop
execute immediate 'insert into Table_3 SELECT DISTINCT ''' || rec.col1 ||''', CODE, '''|| rec.col2 ||''' FROM Table_2 WHERE col1 = ''' || rec.col1 || ''' AND ' || rec.col2;
commit;
end loop;
end;
选择不重复的语句有可能没有从Table_2返回任何值。在当前代码中,当Table_2没有返回任何值时,不会在Table_3中插入任何行。
我的目标是,在没有从Table_2返回任何行的情况下,在Table_3中插入Null值。类似于以下示例,因为rec.col1包含一个值。
+----------+-------+-------+
| 3col1 | 3col2 | 3col3 |
+----------+-------+-------+
| rec.col1 | Null | Null |
+----------+-------+-------+
我如何做到这一点?
您可以使用该功能:
insert into Table_3
with t as (
SELECT DISTINCT . . .
FROM Table_2
WHERE col1 . . .
)
select t.*
from t
union all
select ?, NULL, NULL, . . .
from dual
where not exists (select 1 from t);
您需要使用您的值填写. . .
。您可能想学习在execute immediate
中使用参数。