分离并附加分区db2 10.5版,导致sqlstate 55007错误

时间:2019-01-30 13:49:02

标签: sql db2 partitioning detach

我有一个db2过程,该过程将一个分区分离到临时表中,然后将其附加回该分区。当我执行分离,从快速sql拖放并附加时,它工作得很好,但是当我从存储过程中执行它时,我看到此错误。

类型表的对象“ udbadm.table_name”不能被更改,因为同一应用程序进程当前正在使用它。请指教。以下是我的存储过程。

编辑代码以包含dbms_alert.sleep(120);看到错误在不允许使用空值的上下文中指定了空值。sqlstate22004

db2版本是10.5。我正在快速SQL中执行此操作,但是当从行处理器作为db2调用=>调用OSL_CLNUP_MNTHLY_SBLDGR_DET(98,56,'08 / 31/2018',5)时,我收到相同的错误。

DROP SPECIFIC PROCEDURE OSLD02.OSL_CLNUP_MNTHLY_SBLDGR_DET
;
  CREATE PROCEDURE OSLD02.OSL_CLNUP_MNTHLY_SBLDGR_DET(IN    
                                          IN_ID_BUS_PROCSS SMALLINT,
                                                IN IN_ID_RUN SMALLINT,
                                                IN IN_DT_EOP DATE,
                                                IN IN_BATCH_SIZE INTEGER)
SPECIFIC OSLD02.OSL_CLNUP_MNTHLY_SBLDGR_DET
MODIFIES SQL DATA
NOT DETERMINISTIC
NULL CALL
LANGUAGE SQL EXTERNAL ACTION
INHERIT SPECIAL REGISTERS

 BEGIN

--------------------------------------------------------------------------    ---------------------------------------
-------------------- Declarations
--------------------------------------------------------------------------   ---------------------------------------
declare v_id_task_log    integer;
declare v_cnt_deleted    integer;
declare v_cnt_initial    integer;
declare v_count integer;
declare v_cd_acctg_purp character(10);
declare v_month_eop integer;
declare SQLCODE int default 0; 
declare v_sqlcode int;
declare v_partitionName character(20);
declare v_lowValue character(10);
declare v_highValue character(10);
declare v_detachStmt varchar(512);    
declare v_dropTable varchar(512);   
declare SQL_STMT varchar(512);   
declare v_exist int;
declare v_stagingSchemaTable varchar(512);
DECLARE v_outmessage VARCHAR(32672);
 DECLARE v_outstatus integer default 0;
 DECLARE v_seconds INTEGER default 300;
 declare wait_until timestamp;
declare detach_complete_flg character(2) default 'N';
declare loop_limit int default 0;

declare global temporary table session.detail_tmp
( partition character(10), start character(30), end character(30) ) on    
  commit preserve rows with replace not logged;

 BEGIN

 DECLARE C1 CURSOR WITH HOLD FOR
 select   DATAPARTITIONNAME, LOWVALUE, HIGHVALUE
 from syscat.DATAPARTITIONS  where tabname='SBLDGR_DET'
 and  (SUBSTR(HIGHVALUE, 2,LOCATE(',',HIGHVALUE)-3)
,SUBSTR(HIGHVALUE, LOCATE(',',HIGHVALUE)+1) ) in (
 select  CD_ACCTG_PURP, mo_eop
 from SBLDGR_DET
 where mo_eop< cast (month(IN_DT_EOP) as char(2))
 group by CD_ACCTG_PURP, mo_eop
 having count(*)>0
 )
with ur;

OPEN C1;--
set v_sqlcode=0;--

L1: LOOP
 FETCH C1 INTO v_partitionName, v_lowValue,v_highValue;
 IF SQLCODE<> 0 THEN 
 LEAVE L1; 
 END IF;

set v_detachStmt = 'ALTER TABLE SBLDGR_DET DETACH PARTITION '   
 ||v_partitionName|| ' INTO TABLE OSLSTG.SL_DET_'||v_partitionName;
 EXECUTE IMMEDIATE v_detachStmt ;
 commit;


 while (loop_limit < 10000 and detach_complete_flg = 'N') DO
call dbms_alert.sleep(60);

 IF EXISTS (SELECT 1 FROM SYSCAT.DATAPARTITIONS WHERE TABSCHEMA='OSLSTG'
     AND 
         TABNAME='SL_DET_'|| v_partitionName AND STATUS=' ')    
THEN             
 set detach_complete_flg = 'Y'; 
 END IF; 

 set loop_limit = loop_limit + 1;
 end while;

 set v_dropTable = 'drop table OSLSTG.SL_DET_'||v_partitionName; 
 EXECUTE IMMEDIATE v_dropTable ;
commit;

 SET SQL_STMT   = 'ALTER TABLE sbldgr_det ADD    
PARTITION '||v_partitionName||' STARTING FROM (' || v_lowValue|| ' )
ENDING AT (' || v_highValue|| ')';
 EXECUTE IMMEDIATE SQL_STMT;   
commit;

END LOOP L1;

 CLOSE   C1;--

 END
 ;

END
;

commit;

1 个答案:

答案 0 :(得分:0)

您的代码中存在许多问题。我不会为您提供一个完整的解决方案,只是一些注意事项。

1)如果commit的光标处理过程中,必须DECLARE CURSORWITH HOLD子句。

2)以下光标处理是不正确的。

WHILE (v_sqlcode = 0) DO
  FETCH C1 INTO v_partitionName, v_lowValue,v_highValue;
  set v_sqlcode=sqlcode;
  set v_detachStmt = 'ALTER TABLE SBLDGR_DET DETACH PARTITION'
    ||v_partitionName||' INTO TABLE OSLSTG.SL_DET_'||v_partitionName;
  EXECUTE IMMEDIATE v_detachStmt;
  ...
END WHILE;

即使您在END OF CURSOR之后获得了FETCH代码,您仍然尝试处理数据。 您必须在SQLCODE之后立即检查FETCH。使用类似这样的内容:

L1: LOOP
  FETCH C1 INTO v_partitionName, v_lowValue,v_highValue;
  IF SQLCODE<>0 THEN LEAVE L1; END IF;
  ...
END LOOP L1;

3)您应该执行以下操作。

之后:

EXECUTE IMMEDIATE v_detachStmt;
commit;

您应该实现:

-- Check in loop (sleep, let's say N sec, after each check) 
-- until you get a row with the following statement
-- (construct your table name dynamically):

SELECT 1 
FROM SYSCAT.DATAPARTITIONS
WHERE TABSCHEMA='OSLSTG' AND TABNAME='SL_DET_v_partitionName' AND STATUS=''
WITH UR;

请查看Detaching data partitions主题以获取更多详细信息。