Solved

list of tables used in a DB2 stored procedure

Posted on 2013-07-01
6
2,099 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 45

Expert Comment

by:Kdo
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 45

Expert Comment

by:Kdo
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 45

Accepted Solution

by:
Kdo earned 500 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 (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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now