If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Procedure to collect number of rows from all tables.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-05-09, 04:54
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
Procedure to collect number of rows from all tables.

Create Procedure TABLECOUNT ()
Dynamic Result Sets 0
Modifies SQL Data
Language SQL
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5);
DECLARE vTableName VARCHAR(50);
DECLARE vTableCount INTEGER;
DECLARE stmt varchar(2000);

DECLARE not_found CONDITION FOR SQLSTATE '02000';

DECLARE c1 CURSOR FOR
SELECT tabname from syscat.tables where tabschema='DB2ADMIN1';
DECLARE C2 CURSOR FOR S2;
DECLARE CONTINUE HANDLER FOR not_found

SET stmt = '';

-- No Commitment Control


Delete from COUNTERS;

OPEN c1;

getRows:
LOOP
FETCH c1 INTO vTableName;
IF SQLCODE = 0 THEN
SET stmt ='SELECT Count(*) FROM ' || vTableName;
PREPARE S2 FROM stmt;
OPEN C2;
SET vTableCount = 0;
FETCH C2 INTO vTableCount;
INSERT INTO COUNTERS (tableName, tableCount)
VALUES (vTableName, vTableCount);
CLOSE C2;
ELSE
LEAVE getRows;
END IF;
END LOOP getRows;

CLOSE c1;
END

Im trying to execute the above procedure to get number of rows for all tables lying under a schema.

When i executing this in the last object it is executing recursively(loop is not ending and im im having more entries for the last object.

One more thing is i want to give schema as IN Parameter.
to give the value during runtime for the below objects.


COUNTERS
'SELECT Count(*) FROM ' || SCHEMA.vTableName
Reply With Quote
  #2 (permalink)  
Old 01-05-09, 07:29
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
you did not declared any condition to be executed when not_found..

DECLARE CONTINUE HANDLER FOR not_found
SET at_end = 1;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
SET ab_end = 1 ;
....
FETCH c1 INTO
v_name, v_creator, v_sinterval,v_rinterval ;
If at_end = 1 then
LEAVE fetch_loop ;


CREATE PROCEDURE DB2MONITOR.BPINS (
IN OPT VARCHAR(100),
..

hope this will help
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #3 (permalink)  
Old 01-06-09, 03:14
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You also have a problem with delimited table names. I would use:
Code:
'SELECT Count(*) FROM "' || '"' || SCHEMA || '"."' || vTableName || '"'
Of course, this makes schema and table names case-sensitive as well.
Reply With Quote
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On