Skip to Main Content
  • Questions
  • continuous scrolling output for table data

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Rajan.

Asked: April 06, 2017 - 1:48 pm UTC

Last updated: April 08, 2017 - 5:20 am UTC

Version: 12.1.0.2

Viewed 1000+ times

You Asked

hi tom,
I am a big fan of your work. We are in need of a procedure, which gives output of table data in ... tail -f format ...

meaning the output is continuous, never ending procedure that shows records as it happens in table. I am looking to have this capability with unified audit trail table.

your help is appreciated.
Rajan

and Connor said...

You could simulate it with a pipelined function, eg

SQL> create sequence seq;

Sequence created.

SQL> create table t ( id int default seq.nextval primary key, d date default sysdate, data varchar2(20) );

Table created.

SQL> create or replace
  2  function tail(p_start_Id int) return sys.odcivarchar2list pipelined is
  3    l_max_id int := p_start_id;
  4  begin
  5  loop
  6    for i in ( select * from t where id > l_max_id )
  7    loop
  8      pipe row ( rpad(i.id,10)||rpad(to_char(i.d,'HH24:MI:SS'),15)||i.data );
  9      l_max_id := i.id;
 10    end loop;
 11
 12    dbms_lock.sleep(5);
 13  end loop;
 14  end;
 15  /

Function created.

--
-- Session 1, adding rows to table every second
--
SQL> begin
  2    for i in 1 .. 1000 loop
  3       insert into t (data ) values ( 'My data '||i);
  4       commit;
  5       dbms_lock.sleep(1);
  6    end loop;
  7  end;
  8  /

--
-- Session 2, tail function
--
SQL> set arraysize 5
SQL> select * from table(tail(0));

COLUMN_VALUE
----------------------------------------------------------
1         13:41:22       My data 1
2         13:41:23       My data 2
3         13:41:24       My data 3
4         13:41:25       My data 4
5         13:41:26       My data 5
6         13:41:27       My data 6
7         13:41:28       My data 7
8         13:41:29       My data 8
9         13:41:30       My data 9
10        13:41:31       My data 10
11        13:41:32       My data 11
12        13:41:33       My data 12
13        13:41:34       My data 13
14        13:41:35       My data 14
15        13:41:36       My data 15
...
...


You'd need to add some smarts perhaps in there to be able to stop it (eg it reads a signal via dbms_application_info).

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

SQLCL

Rajeshwaran, April 07, 2017 - 12:26 pm UTC

If you need this flexibility from the command line, then SQLCL gives a new command "REPEAT" that does this.

Jeff Smith - have a demo for this at his blog.
http://www.thatjeffsmith.com/archive/2015/04/sqlcl-run-a-query-over-and-over-refresh-the-screen/
http://www.thatjeffsmith.com/archive/2015/04/tailing-the-alert-log-with-sqlcl/

Connor McDonald
April 08, 2017 - 5:20 am UTC

True, but you do get repeated data back in this case.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library