![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
How do I count number of rows in tables in a schema? I am using DB2 on ZOS. Thanks, Joydeep |
#3
| |||
| |||
|
|
I wrote this function : CREATE FUNCTION getRowCount(t_name VARCHAR(10)) RETURNS INT LANGUAGE SQL RETURN SELECT count(*) FROM t_name; But it's giving me error "SQL0204N "SCHEMA.T_NAME" is an undefined name." Please help |
#4
| |||
| |||
|
|
On 2011-04-06 06:12, Joydeep Chakrabarty wrote: [...] I wrote this function : CREATE FUNCTION getRowCount(t_name VARCHAR(10)) RETURNS INT LANGUAGE SQL RETURN SELECT count(*) FROM t_name; But it's giving me error "SQL0204N "SCHEMA.T_NAME" is an undefined name." Please help This might not apply to z/os, but in general you can't use a variable as the tablename. I don't think you can use execute immediate in a function, it would work in a stored procedure though, so one option is to stuff that into a procedure and call that from the function. If I wanted to encapsulate such functionality I would probably do it in the application layer. /Lennart |
#5
| |||
| |||
|
|
I wrote this procedure. CREATE PROCEDURE return_count (IN schema_name VARCHAR(20)) LANGUAGE SQL SPECIFIC return_count tr: BEGIN DECLARE t_name VARCHAR(50); DECLARE sql_str VARCHAR(200); DECLARE cnt_rows INT; DECLARE r_cur CURSOR FOR SELECT tabname FROM syscat.tables WHERE TABSCHEMA = schema_name AND TYPE = 'T'; OPEN r_cur; FETCH FROM r_cur INTO t_name; WHILE (SQLSTATE = '00000') DO SET sql_str = "SELECT count(*) FROM " || tname; EXECUTE IMMEDIATE sql_str INTO cnt_rows; FETCH FROM r_cur INTO t_name; END WHILE; CLOSE r_cur; END tr I am still getting lot of errors. Please help. |
#6
| |||
| |||
|
|
On 2011-04-06 09:49, Joydeep Chakrabarty wrote: [...] I wrote this procedure. CREATE PROCEDURE return_count (IN schema_name VARCHAR(20)) LANGUAGE SQL SPECIFIC return_count tr: BEGIN DECLARE t_name VARCHAR(50); DECLARE sql_str VARCHAR(200); DECLARE cnt_rows INT; DECLARE r_cur CURSOR FOR SELECT tabname FROM syscat.tables WHERE TABSCHEMA = schema_name AND TYPE = 'T'; OPEN r_cur; FETCH FROM r_cur INTO t_name; WHILE (SQLSTATE = '00000') DO SET sql_str = "SELECT count(*) FROM " || tname; EXECUTE IMMEDIATE sql_str INTO cnt_rows; FETCH FROM r_cur INTO t_name; END WHILE; CLOSE r_cur; END tr I am still getting lot of errors. Please help. Apperantly you can't use execute immediate for select stmts, sorry if I mislead you there. /Lennart [...] |
#7
| |||
| |||
|
|
But I am getting "SQL0104N An unexpected token "into" was found following "TE IMMEDIATE sql_str". Expected tokens may include: "CONCAT". LINE NUMBER=18. SQLSTATE=42601" |
#8
| |||
| |||
|
|
On 04/06/2011 12:45 PM, Joydeep Chakrabarty wrote: [...] But I am getting "SQL0104N An unexpected token "into" was found following "TE IMMEDIATE sql_str". Expected tokens may include: "CONCAT". LINE NUMBER=18. SQLSTATE=42601" Not sure this is such a great idea ..., but here it goes: create procedure return_count(in tabname varchar(128)) language sql begin declare stmt varchar(255); declare global temporary table t ( cnt int not null ); begin declare c1 cursor with return for select cnt from session.t; set stmt = 'insert into session.t (cnt) select count(1) from syscat.' || tabname; execute immediate stmt; open c1; end; end @ db2 "call return_count('tables')" Result set 1 -------------- CNT ----------- 396 1 record(s) selected. You'll need a user temporary tablespace for the temporary table, and you will also have to add handling of SQLSTATE 42710 in case session.t already exists during declaration |
#9
| |||
| |||
|
|
I am trying this way, but it's not displaying any data. create procedure return_count(in s_name varchar(128)) language sql begin declare stmt varchar(255); declare tab1 varchar(250); declare global temporary table t ( t_name varchar(250), cnt int ); begin FOR v_row AS SELECT tabname from syscat.tables where tabschema = s_name DO set tab1 = v_row.tabname; INSERT INTO session.t (t_name) VALUES (tab1); set stmt = 'update session.t t1 set CNT = (select count(1) from ' ||s_name||'.'||tab1||') where t1.T_NAME = ''' || tab1||''''; execute immediate stmt; END FOR; end; end @ |
#10
| |||
| |||
|
|
On 2011-04-13 07:47, Joydeep Chakrabarty wrote: [...] I am trying this way, but it's not displaying any data. create procedure return_count(in s_name varchar(128)) language sql begin declare stmt varchar(255); declare tab1 varchar(250); declare global temporary table t ( t_name varchar(250), cnt int ); begin FOR v_row AS SELECT tabname from syscat.tables where tabschema = s_name DO set tab1 = v_row.tabname; INSERT INTO session.t (t_name) VALUES (tab1); set stmt = 'update session.t t1 set CNT = (select count(1) from ' ||s_name||'.'||tab1||') where t1.T_NAME = ''' || tab1||''''; execute immediate stmt; END FOR; end; end @ In my example I opened a cursor that returns the data, in your code nothing is exposed to the outside world. Do you need exact numbers? If your stats are reasonable up to date and you can live with an approximation, syscat.tables.card might be an alternative: select tabname, card from syscat.tables where tabschema = ? /Lennart |
![]() |
| Thread Tools | |
| Display Modes | |
| |