markgargan
Programmer
I originally thought this would be an easy select statement to perform.
After a few minutes I realised I could only do it with a stored procedure. I recently switched jobs and hence dbs from Oracle to DB2 so if my questions seem basic please grant me the benefit of the doubt.
I thought I had it nailed. I'd all the tablenames in my schema coming back via a fetch into a variable 'vTableName'.
Then I tried to prepare a select statement within the for loop (I've posted the code at the bottom) but I couldn't prepare a statement with a SELECT? Maybe someone could explain this.
nor could I just call the select statement with the table as the variable name.
If anyone has any idea how to do this I'd really appreciate the help. I've managed to retrieve the results I needed from a java programme but I'd love to know how to do it with a stored procedure.
The error I get when I try to build the procedure is as follows. I understand the error, I just dunno how to get it to insert the variable name as the table name
DB2ADMIN.tableCount: 40: [IBM][CLI Driver][DB2/NT] SQL0204N "DB2ADMIN.VTABLENAME" is an undefined name. LINE NUMBER=40. SQLSTATE=42704
Thanks,
MArk.
Here's the code from my stored procedure.
------------------------------------------
CREATE PROCEDURE tableCount()
LANGUAGE SQL
BEGIN
DECLARE SQLSTATE CHAR(5);
DECLARE vTableName VARCHAR(20);
DECLARE vTableCount INTEGER;
DECLARE stmt varchar(2000);
DECLARE at_end INT DEFAULT 0;
DECLARE not_found CONDITION FOR SQLSTATE '02000';
DECLARE c1 CURSOR FOR
SELECT tabname from syscat.tables where tabschema='DB2ADMIN';
DECLARE CONTINUE HANDLER FOR not_found
SET at_end = 1;
-- continue if sqlstate 42704 ('undefined object name')
DECLARE CONTINUE HANDLER FOR SQLSTATE '42704'
SET stmt = '';
Delete from COUNTERS;
OPEN c1;
getRows:
LOOP
FETCH c1 INTO vTableName;
IF at_end = 1 THEN
LEAVE getRows;
END IF;
SELECT count(*) into vTableCount from vTableName;
INSERT INTO COUNTERS (tableName, tableCount)
VALUES (vTableName, vTableCount);
END LOOP;
CLOSE c1;
END
---------------------------------------------------
After a few minutes I realised I could only do it with a stored procedure. I recently switched jobs and hence dbs from Oracle to DB2 so if my questions seem basic please grant me the benefit of the doubt.
I thought I had it nailed. I'd all the tablenames in my schema coming back via a fetch into a variable 'vTableName'.
Then I tried to prepare a select statement within the for loop (I've posted the code at the bottom) but I couldn't prepare a statement with a SELECT? Maybe someone could explain this.
nor could I just call the select statement with the table as the variable name.
If anyone has any idea how to do this I'd really appreciate the help. I've managed to retrieve the results I needed from a java programme but I'd love to know how to do it with a stored procedure.
The error I get when I try to build the procedure is as follows. I understand the error, I just dunno how to get it to insert the variable name as the table name
DB2ADMIN.tableCount: 40: [IBM][CLI Driver][DB2/NT] SQL0204N "DB2ADMIN.VTABLENAME" is an undefined name. LINE NUMBER=40. SQLSTATE=42704
Thanks,
MArk.
Here's the code from my stored procedure.
------------------------------------------
CREATE PROCEDURE tableCount()
LANGUAGE SQL
BEGIN
DECLARE SQLSTATE CHAR(5);
DECLARE vTableName VARCHAR(20);
DECLARE vTableCount INTEGER;
DECLARE stmt varchar(2000);
DECLARE at_end INT DEFAULT 0;
DECLARE not_found CONDITION FOR SQLSTATE '02000';
DECLARE c1 CURSOR FOR
SELECT tabname from syscat.tables where tabschema='DB2ADMIN';
DECLARE CONTINUE HANDLER FOR not_found
SET at_end = 1;
-- continue if sqlstate 42704 ('undefined object name')
DECLARE CONTINUE HANDLER FOR SQLSTATE '42704'
SET stmt = '';
Delete from COUNTERS;
OPEN c1;
getRows:
LOOP
FETCH c1 INTO vTableName;
IF at_end = 1 THEN
LEAVE getRows;
END IF;
SELECT count(*) into vTableCount from vTableName;
INSERT INTO COUNTERS (tableName, tableCount)
VALUES (vTableName, vTableCount);
END LOOP;
CLOSE c1;
END
---------------------------------------------------