Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

list of tables used in a DB2 stored procedure

Posted on 2013-07-01
6
Medium Priority
?
2,603 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
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
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…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

926 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