使用dbms_output.put_line检测最后一行

时间:2013-10-09 15:04:09

标签: oracle plsql dbms-output

我很好奇如何编写这个过程。我正在使用集合对象从表中提取信息。然后我使用dbms_output.put_line来显示我收集的值。

我想在输出的最后一行添加一行。例如:

表A

col1 col2  
1     3  
2     4  

我拉取值并使用dbms_output.put_line显示这些项目。

会显示:

1,3  
2,4

有没有办法将“这是最后一行”追加到收集/显示的最后一行显示......

1,3  
2,4 This is the last line

我尝试在收集过程中在循环后添加另一个dbms_output.put_line,但它只是将其视为2行。

1,3  
2,4  
This is the last line. 

1 个答案:

答案 0 :(得分:1)

一种方法是在每行文字之前插入一个新行 - 第一行除外。

create table taba(
 col1 number,
 col2 number
);
insert into taba values (1,2);
insert into taba values (3,4);
commit;

declare
  type tab_t is table of taba%rowtype;
  tab tab_t;
  cursor c is select * from taba;
  first_row boolean;
begin
  open c;
  fetch c bulk collect into tab;
  close c;

  first_row := true;
  for x in 1..tab.count loop
     if not first_row then
        dbms_output.put_line('');
     end if;
     first_row := false;
     dbms_output.put( tab( x ).col1 || ',' || tab( x ).col2 );
  end loop;
  if not first_row then
     dbms_output.put_line('   This is the last line');
  else
     dbms_output.put_line('The table is empty');
  end if;
end;
/

结果:

1,2
3,4   This is the last line