-1

Enviroment:

  • DB2 Version 11.1,
  • OS - Linux

How to get the result table of stored procedure into a temp table?

The table and the result have the same table configuration (firstColumn int, secondColumn nvarchar(255))

mao
  • 11,321
  • 2
  • 13
  • 29
michael-mammut
  • 2,595
  • 5
  • 28
  • 46

1 Answers1

2

I'm assuming your stored procedure returns an open cursor, so you want to consume that cursor, inserting its contents into a session table (declared global temporary table) on Db2-LUW.

In addition to fetch and insert statements, you need to understand the following statements:

associate result set locator ... with procedure ...

allocate ... cursor for result set ...

Here is a deliberately artificial example of a nested stored procedure, which shows fetching a result-set from a nested procedure into a session table. The purpose is to show how the syntax works, rather than to do anything useful with data (as the net effect can be equally met by a simple catalog query in this case). This example can be run at the Db2 command-line (for example at the bash shell, after you connected to a database with appropriate permissions):

update command options using s on ;

--#SET TERMINATOR @

create or replace procedure alltabs
dynamic result sets 1
language sql
specific alltabs
begin
  declare v_cur cursor with return to caller for select tabschema,tabname,type  from syscat.tables ;
  open v_cur;
end@


declare global temporary table session.thetables(tabschema varchar(128), tabname varchar(128))
not logged with replace on commit preserve rows  @


create or replace procedure populate_dgtt()
language sql
specific populate_dgtt
begin
  declare v_rs result_set_locator varying;
  declare v_tabschema varchar(128);
  declare v_tabname   varchar(128);
  declare v_type      char(1);
  declare sqlstate char(5) default '00000';
  call alltabs;
  associate result set locator (v_rs) with procedure alltabs;
  allocate v_rscur cursor for result set v_rs;
  fetch from v_rscur into v_tabschema, v_tabname, v_type;
  while ( sqlstate = '00000') do
      if v_type='V' and v_tabschema='SYSSTAT'
      then
          insert into session.thetables(tabschema,tabname) values (v_tabschema, v_tabname);
      end if;
      fetch from v_rscur into v_tabschema, v_tabname, v_type;
  end while;
  return;
end@

call populate_dgtt()@

select rtrim(Tabschema)||'.'||rtrim(tabname) from session.thetables @
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mao
  • 11,321
  • 2
  • 13
  • 29
  • If i hance the line ` insert into session.thetables(tabschema,tabname) values (v_tabschema, v_tabname);` to `set newDeclaratedVariable = v_tabname`, the procedure never stop. In the DB2 Studio the state is "started" and never stops. change back to the `insert into `-Line it works well. what is the difference of this two lines? – michael-mammut Sep 30 '19 at 14:37
  • 1
    @michael-mammut , A `SET` statement will not affect the table, unlike an INSERT . It is better if you ask a new question that shows all your code in a small example, because your follow-up question is not suitable for a comment. If you have a loop that will not stop, then it is possible you have a mistake in your code. You can debug your procedure in DataStudio also. – mao Sep 30 '19 at 15:18