【问题标题】:Detecting last line with dbms_output.put_line使用 dbms_output.put_line 检测最后一行
【发布时间】:2014-05-21 00:19:01
【问题描述】:

我很好奇如何编写这个过程。我正在使用集合对象从表中提取信息。然后我使用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. 

【问题讨论】:

  • 贴出你当前使用的代码。除其他外,这将取决于您使用的集合类型,是稀疏还是密集等。

标签: oracle plsql dbms-output


【解决方案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

【讨论】:

  • 我会使用dbms_output.new_line而不是dbms_output.put_line(''),但效果是一样的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多