niceboykunal123
Technical User
CREATE PROCEDURE tableCount()LANGUAGE SQLBEGIN DECLARE SQLCODE INTEGER DEFAULT 0; DECLARE SQLSTATE CHAR(5); DECLARE vTableName VARCHAR(20); 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='DB2ADMIN'; DECLARE C2 DYNAMIC SCROLL CURSOR WITH RETURN FOR S2; DECLARE CONTINUE HANDLER FOR not_found SET stmt = ''; -- No Commitment Control Set Transaction Isolation Level NC; 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