[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1680
  • Last Modified:

DB2 With statement

I want to remove the session temporay table and make it call based so I can quarantee no clashes in concurrent calls to the SP. Can anyone advise how a I change the seesion table to one using the with statement? I would like ot be able to reference the table from theclient code ( as a returned cursor I guess).
0
10Pints
Asked:
10Pints
  • 2
2 Solutions
 
10PintsAuthor Commented:
Ooops! sorry for got the code snippet ...
DROP PROCEDURE GetNVPairs <<
CREATE PROCEDURE GetNVPairs(str CLOB(64K)) 
  LANGUAGE SQL
  RESULT SETS 1

 P1:BEGIN
    DECLARE X integer;
    DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
    DECLARE v_a VARCHAR(250);
    DECLARE v_b VARCHAR(250);

    DECLARE GLOBAL TEMPORARY TABLE SESSION.ITEMS
    (
       ordinal integer,
       name   VarChar(50),
       val    VarChar(50)
    )
    WITH REPLACE 
    ON COMMIT
    PRESERVE ROWS
    NOT LOGGED;
  
  BEGIN
    DECLARE iordinal integer;
    DECLARE ia integer;
    DECLARE ib integer;
    DECLARE ic integer;
    DECLARE cur CURSOR FOR SELECT * FROM TABLE(elemIdx( str)) ORDER BY ordinal;
 
    DECLARE cur2 CURSOR  WITH RETURN TO CALLER FOR SELECT * FROM SESSION.ITEMS;
    DELETE FROM SESSION.ITEMS;

    OPEN cur;
    FETCH FROM cur INTO iordinal, ia, ib, ic;

    WHILE(SQLSTATE = '00000') DO
         IF ((ia <> 0) AND ( ib <> 0) AND ( ic <> 0)) then
    	     INSERT INTO SESSION.items(ordinal, name, val) VALUES(iordinal,  SUBSTR( str, ia, ib-ia), SUBSTR( str, ib+1, ic-ib-1));
         END IF;

         FETCH FROM cur INTO iordinal, ia, ib, ic;
    END WHILE;

    CLOSE cur;

    OPEN cur2;

END;
END P1<<

Open in new window

0
 
momi_sabagCommented:
you can use the temporary table since every session gets its own copy, so you won't have colisions, but you can also use

with a as (
SELECT * FROM TABLE(elemIdx( str))
)
select iordinal,  SUBSTR( str, ia, ib-ia), SUBSTR( str, ib+1, ic-ib-1)
from a
order by ordinal
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi 10pints,

The two techniques have their own usage.

The common table expression that Momi shows is often used when a query needs to process the same set of data more than once or when recursion is needed.  It can also make a query a lot easier to write and understand.

The temporary table that you show is often used to simplify a complex query, when multiple queries need to process the same data, or when one job step (i.e. the stored procedure) needs to hand results to another step in a job.

Unless there are performs reasons to do otherwise, I'll choose the CTE whenever possible.


Good Luck,
Kent
0
 
10PintsAuthor Commented:
thanks guys - BRILLIANT!  this cuts my code in less than half and will avid any potential temporary table clashes as the server is multithreaded.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now