如何在一个PL / SQL语句中编写多个SQL查询语句和PL / SQL语句

时间:2019-12-09 09:33:02

标签: sql oracle plsql

在1个PL / SQL块中,必须使用多个SELECT查询和一个块语句。在此block语句中,我们必须在进行insert查询之前进行计数,并且一旦insert语句运行之后,之后必须进行其后述的id值的after_counts。

set heading off
set colsep '|'
set feedback off
set sqlformat csv
set trimspool on
spool output.txt
declare
 ln_rec tab1%rowtype;
 lv varchar(20);
 sid tab.id%type;
 b_cnts number;
 a_cnts number;
 type sh_id is varray(10) of tab.col1%type;
 id sh_id := sh_id(1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
begin
 select a.id, count(b.sub_id) into sid, b_cnts as "before_counts" from tab a, tab1 b;

 for i in (select distinct b.sub_id from tab a, tab1 b where a.id in (1, 3, 5, 7, 9, 11, 13, 15, 17, 19))
 loop
  select * into ln_rec from tab1 where sub_id = i.sub_id;

  insert into new_tab values(id, i.sub_id, lv);
  commit;
 end loop;

 select a.id, count(b.sub_id) into sid, a_cnts as "after_counts" from tab a, tab b;
end;
spool off

但是当我执行它时,由于上面的SET system variable summary和由于insert而在id语句中出现了错误。我想要以csv格式或输出格式输出,例如应生成3列作为id,before_counts,after_counts及其正确值。像这样:-

<id>   <before_counts>    <after_counts>   -- This heading should not appear because above used heading off
1       135                138
3       246                250
5       298                302
7       389                399
.........

1 个答案:

答案 0 :(得分:0)

类似这样的东西:

venue