user2863005
user2863005

Reputation: 1

Detecting last line with dbms_output.put_line

I'm curious on how to code this process. I am using collection objects to pull information from a table. Then I am using dbms_output.put_line to display the values that I collected.

I want to append a line to the last line of my output. For example:

table A

col1 col2  
1     3  
2     4  

I pull the values and use dbms_output.put_line to display those items.

It would display:

1,3  
2,4

Is there a way to append "This is the last line" to the last line collected/displayed to show...

1,3  
2,4 This is the last line

I tried to add another dbms_output.put_line after my loop during the collection process, but it just treats it as 2 lines.

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

Upvotes: 0

Views: 369

Answers (1)

krokodilko
krokodilko

Reputation: 36127

One way to do it is to insert a new line before each line of text - except the first line.

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;
/

Results:

1,2
3,4   This is the last line

Upvotes: 1

Related Questions