Solved

list of tables used in a DB2 stored procedure

Posted on 2013-07-01
6
2,183 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL1013N  The database alias name or database name "MRIIN" could not be found.  SQLSTATE=42705 5 1,685
SAS Email Chart as attachment code 2 290
Catalog Index 9 123
SQL400 max size 5 93
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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