请教一个简单的程序块
刚学习DB2,想写以下代码:
意图:
轮训系统表syscat.tables 各个表在数据库各个节点的分步情况。将结果插入到自己建好的一张表:
tmp_test
我自己写的一段代码:
begin
declare cursor1 cursor with hold for select tbspace,tabname from syscat.tables where tbspace like 'TBS_0%'order by tabname;
select count(*) into tab_num from syscat.tables where tbspace like 'TBS%';
open cursor1;
set cnt_1=1;
fetch cursor1.tabname into TABLE_NAME;
fetch cursor1.tbspace into TABLE_SPC;
while cnt_1 < tab_num+1
insert into tmp_test(tbspace,tabname,nodid,lszkb,pszkb,iszkb)
select '''||TABLE_SPC||''','''||TABLE_NAME||''',DBPARTITIONNUM,SUM(DATA_OBJECT_L_SIZE) lszkb,
SUM(DATA_OBJECT_P_SIZE) pszkb, SUM(INDEX_OBJECT_P_SIZE) iszkb
from TABLE(SYSPROC.ADMIN_GET_TAB_INFO_V95(''XY'','''||TABLE_NAME||''')) AS T
GROUP BY '''||TABLE_SPC||''','''||TABLE_NAME||''',DBPARTITIONNUM ;
commit;
end while;
close cursor1;
end
测试无法通过。。
请论坛中各位前辈指教。。。谢谢!
[解决办法]
亲,你写的代码中,有明显的4个错误:
1、fetch from 语句要加在while循环中,要不你循环多次取得都是同一个表的信息(要加from);
2、单引号的书写,如果你要想在查询的结果中增加一个单引号,那么你就得写两个单引号;
3、计数器不自增,就是死循环;
4、while后要加do关键字。
还有ADMIN_GET_TAB_INFO_V95这个存储过程,是V9.5特有的吗。
试一下以下的代码:
begin
declare cursor1 cursor with hold for select tbspace,tabname from syscat.tables where tbspace like 'TBS_0%' order by tabname;
select count(*) into tab_num from syscat.tables where tbspace like 'TBS%';
open cursor1;
set cnt_1=1;
while cnt_1 < tab_num+1
do
fetch from cursor1 into TABLE_NAME, TABLE_SPC;
insert into tmp_test(tbspace,tabname,nodid,lszkb,pszkb,iszkb)
select TABLE_SPC,TABLE_NAME,DBPARTITIONNUM,SUM(DATA_OBJECT_L_SIZE) lszkb,
SUM(DATA_OBJECT_P_SIZE) pszkb, SUM(INDEX_OBJECT_P_SIZE) iszkb
from TABLE(SYSPROC.ADMIN_GET_TAB_INFO_V95(XY,TABLE_NAME)) AS T
GROUP BY TABLE_SPC,TABLE_NAME,DBPARTITIONNUM ;
commit;
set cnt_1 = cnt_1 + 1;
end while;
close cursor1;
end