Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

list of tables used in a DB2 stored procedure

Posted on 2013-07-01
6
Medium Priority
?
2,531 Views
Last Modified: 2013-07-02
Hi,

 We are on DB2 9.7. I want to get the list of tables used in specific stored procedures.
All our database tables start with "TB_"

Can someone please help me with a SQL or stored procedure to get this information from the metadata?

Thanks!
0
Comment
Question by:pvsbandi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 39291778
Hi pvsbandi,

The objects used in a stored procedure aren't cross referenced in another table.  You'll have to parse the procedure's source code to get the names.

Depending on your coding practices, it could be relatively easy, and it could be quite a task.  But I think that we can do it.

The SQL below will find all of the procedures where '<table-name>' occurs.  It's not very scientific, but if you know the table name it's an easy way to find all of the procedures that use it.

SELECT routinename,text FROM syscat.routines 
WHERE language='SQL' 
  AND locate('<table-name>',text) > 0

Open in new window


Two other approaches exist to find all of the names in a procedure.  Scanning the procedure source for tokens like TB_ and scanning the source for all names that occur after the words FROM and JOIN.  That's probably recursive SQL there....


Kent
0
 

Author Comment

by:pvsbandi
ID: 39291789
Kent,

  I have tried a stored procedure, but it is throwing an error when running it.
This is the error.
A database manager error occurred.[IBM][CLI Driver][DB2/AIX64] SQL0420N  Invalid character found in a character string argument of the function "INTEGER".  SQLSTATE=22018

Open in new window

Here is the code i tried.
CREATE PROCEDURE CHESSIE.FIND_TAB(IN AS_TAB_NAME VARCHAR(50))
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN
DECLARE V_NUM INTEGER;
DECLARE V_POS INTEGER;
DECLARE GLOBAL TEMPORARY TABLE TMP_TABLE(V_TABLE_NM VARCHAR(50))
WITH REPLACE;

SET V_NUM = 1;
FOR C1 AS
SELECT UPPER(CAST(TEXT AS VARCHAR(32000))) AS CODE
FROM SYSCAT.ROUTINES
WHERE ROUTINENAME = AS_TAB_NAME
DO

SET V_POS = LOCATE_IN_STRING(C1.CODE,1,V_NUM,'TB_',OCTETS);
INSERT INTO SESSION.TMP_TABLE
SELECT SUBSTR(C1.CODE,V_POS,5) FROM SYSIBM.SYSDUMMY1;
 SET V_NUM = V_NUM + 1;
END FOR;

P2: BEGIN
DECLARE C2 CURSOR WITH RETURN TO CALLER FOR
SELECT * FROM SESSION.TMP_TABLE;
OPEN C2;
END P2;

END P1 

Open in new window

0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 39291846
It looks like the parameter to LOCATE_IN_STRING are in the wrong order

  LOCATE_IN_STRING(C1.CODE,1,V_NUM,'TB_',OCTETS);

should probably be

LOCATE_IN_STRING(C1.CODE, 'TB_', 1, V_NUM, OCTETS);

And I think that you want to loop until the function returns not_found (0).


Kent
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:pvsbandi
ID: 39292794
Thanks, Kent! After correcting that it gets me only one row, with the first instance of a "TB_".
 Can you please help me with this procedure?
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 39293235
DB2 (like most versions of SQL) doesn't have a particularly robust looping structure.  The do/while controls available in C would be perfect here, but since they don't exist we'll have to fake it.

This should be pretty close.  Adding the integer variable V_OCC to count the number of occurrences withing the procedure and then looping on the procedure source until the string 'TB_' is not found.

CREATE PROCEDURE CHESSIE.FIND_TAB(IN AS_TAB_NAME VARCHAR(50))
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN
DECLARE V_NUM INTEGER;
DECLARE V_OCC INTEGER;
DECLARE V_POS INTEGER;
DECLARE GLOBAL TEMPORARY TABLE TMP_TABLE(V_TABLE_NM VARCHAR(50))
WITH REPLACE;

SET V_NUM = 1;
FOR C1 AS
  SELECT UPPER(CAST(TEXT AS VARCHAR(32000))) AS CODE
  FROM SYSCAT.ROUTINES
  WHERE ROUTINENAME = AS_TAB_NAME

DO
  SET V_OCC = 1;
  SET V_POS = LOCATE_IN_STRING(C1.CODE, 'TB_', 1, V_OCC, OCTETS);
  WHILE V_POS > 0
  DO
    INSERT INTO SESSION.TMP_TABLE
    SELECT SUBSTR(C1.CODE,V_POS,5) FROM SYSIBM.SYSDUMMY1;
    SET V_NUM = V_NUM + 1;
    SET F_OCC = V_OCC + 1;
    SET V_POS = LOCATE_IN_STRING(C1.CODE, 'TB_', 1, V_OCC, OCTETS);
  END WHILE;
END FOR;

P2: BEGIN
DECLARE C2 CURSOR WITH RETURN TO CALLER FOR
SELECT * FROM SESSION.TMP_TABLE;
OPEN C2;
END P2;

END

Open in new window


Oh.  And like I tell everyone, INDENT!  It makes it a lot easier to see the program logic, especially on large procedures.  :)


Kent
0
 

Author Closing Comment

by:pvsbandi
ID: 39293424
You are a genius!! Thanks so much!
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question