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 > Issue trying count all records in tables of an Schema

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-01-09, 03:34
luca15mx luca15mx is offline
Registered User
 
Join Date: Aug 2007
Posts: 8
Unhappy Issue trying count all records in tables of an Schema

Hello:

Recently, my DB Crashed, loosing some data in some tables. Searching on DBForums for a procedure to count all rows in all tables, in all schemas of the crashed database, i found this:

DECLARE c1 CURSOR FOR
SELECT tabname from syscat.tables where tabschema in ('SY811', 'PD811', 'OL811', 'DD811', 'PS811', 'SVM811') AND tabname not LIKE ('%PSFT.F00165');

DECLARE C2 CURSOR FOR S2;

DECLARE CONTINUE HANDLER FOR not_found

SET stmt = '';

-- No Commitment Control
--Set Transaction Isolation Level NC;

Delete from sy811.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 sy811.COUNTERS (tableName, tableCount)
VALUES (vTableName, vTableCount);
CLOSE C2;
ELSE
LEAVE getRows;
END IF;
END LOOP getRows;

CLOSE c1;
END



Then, in Development Center, is ok to build and deploy; But when it runs show this error:

Data returned in result sets is limited to the first 100 rows.
Data returned in result set columns is limited to the first 20 bytes or characters.
SY811.TABLECOUNT - Exception occurred while running:
Se ha producido un error de gestor de bases de datos.[IBM][CLI Driver][DB2/NT] SQL0204N "PSFT.F00165" is an undefined name. SQLSTATE=42704
SY811.TABLECOUNT - Roll back completed successfully.
SY811.TABLECOUNT - Run failed.

The F00165 table is in OL811 and SY811 Schemas, but in PSFT.

PSFT is the power user i used to have access to database.

Any idea about to solve this issue?

Thanks a lot.
Reply With Quote
  #2 (permalink)  
Old 09-01-09, 07:36
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Obviously, you need to qualify the table name by adding its schema name when you create your select statement.
Reply With Quote
  #3 (permalink)  
Old 09-01-09, 10:41
luca15mx luca15mx is offline
Registered User
 
Join Date: Aug 2007
Posts: 8
Thanks Nick.

But i think it's a dynamic contructed statement, schema name and table name are fetched in cursor C1, with where clausule schemas.

I pretend to have an automatic way to have this task.
Reply With Quote
  #4 (permalink)  
Old 09-01-09, 11:33
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by luca15mx
schema name and table name are fetched in cursor C1
This is not true.
Reply With Quote
  #5 (permalink)  
Old 09-01-09, 13:05
Cougar8000 Cougar8000 is offline
Registered User
 
Join Date: Nov 2005
Location: IL
Posts: 554
I would agree with Nick on this one. Review your SQL that builds your C1 cursor and you will see what nick means.
__________________
--
IBM Certified DBA on DB2 for Linux, UNIX, and Windows

DB2 v9.1.0.2 os 5.3.0.0
Reply With Quote
  #6 (permalink)  
Old 09-02-09, 02:05
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
Declare a variable for the schema to give as input parameter.


derive a statement

eg.

stmt='select count(1) from '||schema1||'.'||tabname

and then prepare the statement and then execute it
Reply With Quote
  #7 (permalink)  
Old 09-03-09, 18:07
luca15mx luca15mx is offline
Registered User
 
Join Date: Aug 2007
Posts: 8
Red face

Thanks Lankar.


n_i you'r right, i have a big error in the script, now this is the corrected version... Builds ok, but when i run it, have the followin error.

[IBM][CLI Driver][DB2/NT] SQL0198N The statement string of the PREPARE or EXECUTE IMMEDIATE statement is blank or empty. SQLSTATE=42617


When debug it, stmt variable it's empty, before to PREPARE S2 FROM stmt;

Any idea?

Thanks in advance.

CREATE PROCEDURE PSFT.Contar ( )
LANGUAGE SQL
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5);
DECLARE vTableName VARCHAR(25);
DECLARE vTableSchema VARCHAR(10);
DECLARE vTableCount INTEGER;
DECLARE stmt varchar(2000);

DECLARE not_found CONDITION FOR SQLSTATE '02000';

DECLARE c1 CURSOR FOR
SELECT tabname, tabschema from syscat.tables where tabschema in ('SY811', 'PD811', 'OL811', 'DD811', 'PS811', 'SVM811');

DECLARE C2 CURSOR FOR S2;

DECLARE CONTINUE HANDLER FOR not_found

SET stmt = '';

-- No Commitment Control
--Set Transaction Isolation Level NC;

Delete from PSFT.COUNTERS;

OPEN c1;

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

CLOSE c1;
END
Reply With Quote
  #8 (permalink)  
Old 09-03-09, 18:20
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
IF SQLCODE = 0 THEN
SET stmt ='SELECT Count(*) FROM ' || vTableSchema || '.' || vTableName;
IF SQLCODE <> 0 THEN <what did you do?>
Reply With Quote
  #9 (permalink)  
Old 09-03-09, 22:26
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
luca15mx, Not sure if this is the problem or not but you have defined the variable to hold the table name as VARCHAR(25) and the table schema as VARCHAR(10). However both columns in SYSCAT.TABLES are VARCHAR(128).

Try either defining your variables as VARCHAR(128).
Reply With Quote
  #10 (permalink)  
Old 09-03-09, 23:02
luca15mx luca15mx is offline
Registered User
 
Join Date: Aug 2007
Posts: 8
tokuma, i'll try you consideration.

Stealth_DBA you are right, i'll try what you say.

Thanks a lot tokuma and Stealth_DBA
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