C ++ DB2在QTEMP中清除非Journal表

时间:2012-08-10 07:45:51

标签: c++ sql db2 ibm-midrange

我正在开发一个C ++程序来获取和过滤数据,并将其存储在DB2 AS400上的QTEMP库中的时态表(P6SUBFCH)中。

这样做的原因是,我们使用的PLEX具有一次定义的方法,一次只能获取64条记录的数据,因此称为SQLBlockFetch的函数将所有记录提取到时态表中,并且只返回64条记录从这个临时表中一次,并按此处理。

我正在开发自己的SQLBockfetch,并且它的C ++部分是使用嵌入式SQL来执行所有SQL查询。这是在QTEMP库中获取创建表(P6SUBFCH)的所有数据。

QTEMP库对于会话是活动的,只要会话结束,所有表都会在该库中删除。

另外需要注意的是,QTEMP中的所有表格都没有记录,而且我在google上研究的是它不可能。

问题
我的C ++程序在第一次在会话中调用时效果很好,但第二次,它只是将数据附加到表QTEMP / P6SUBFCH中的先前数据。这是一个问题,我尝试首先清除数据(SQL DELETE语句),但我在AS400作业日志中收到此SQL错误。

Member P6SUBFCH not journaled to journal *N.
P6SUBFCH in QTEMP not valid for operation.  

SQL错误:-7008

我已阅读以下ql7008-error-workaround

  

禁用事务隔离

不确定这可能是我的问题?我还是嵌入式SQL的新手,不知道我将如何做到这一点。

这是我的代码:

void LinkRepository::SaveResultsToTable(InputParameters inputParameters)
{
EXEC SQL INCLUDE SQLCA;
EXEC SQL BEGIN DECLARE SECTION;
char Query2[2000] = { "" };
char Query3[2000] = { "" };

EXEC SQL END DECLARE SECTION;

strcpy(Query2, "CREATE TABLE QTEMP/P6SUBFCH");
strcat(Query2, " (PAOPIID CHAR(21) NOT NULL,");
strcat(Query2, " POPITPE CHAR(10),");
strcat(Query2, " POPISTPE CHAR(10),");
strcat(Query2, " POPIKNID CHAR(20),");
strcat(Query2, " PINSTAT CHAR(10),");
strcat(Query2, " PLEAFIND CHAR(1),");
strcat(Query2, " CLOPIID CHAR(21),");
strcat(Query2, " COPITPE CHAR(10),");
strcat(Query2, " COPISTPE CHAR(10),");
strcat(Query2, " COPIKNID CHAR(20),");
strcat(Query2, " CINSTAT CHAR(10),");
strcat(Query2, " CLEAFIND CHAR(1),");
strcat(Query2, " LINKIN CHAR(10))");

EXEC SQL EXECUTE IMMEDIATE :Query2;

// Error handling
if (sqlca.sqlcode == -601)
{
     strcpy(Query2, "DELETE FROM QTEMP/P6SUBFCH");

     EXEC SQL EXECUTE IMMEDIATE :Query2;

}

EXEC SQL COMMIT;

// Datalinks is a vector that has all the filter data to insert into the QTEMP/P6SUBFCH table
for(vector<Datalink*>::iterator dl = Datalinks.begin(); dl != Datalinks.end(); ++dl)
{
     EXEC SQL SET TRANSACTION ISOLATION LEVEL NO COMMIT;

     strcpy(Query3, "INSERT INTO QTEMP/P6SUBFCH (PAOPIID,POPITPE,");
     strcat(Query3, "POPISTPE,POPIKNID,PINSTAT,PLEAFIND,");
     strcat(Query3, "CLOPIID,COPITPE,COPISTPE,COPIKNID,CINSTAT,");
     strcat(Query3, "CLEAFIND,LINKIN)");
     strcat(Query3, "VALUES('");
     strcat(Query3, (*dl)->ParentOperationsItemId);
     strcat(Query3, "','");
     strcat(Query3, (*dl)->ParentOperationsItemType);
     strcat(Query3, "','");
     strcat(Query3, (*dl)->ParentOperationsItemSubType);
     strcat(Query3, "','");
     strcat(Query3, (*dl)->ParentKnownbyId);
     strcat(Query3, "','");
     strcat(Query3, (*dl)->ParentInternalStatus);
     strcat(Query3, "','");
     append_char(Query3, (*dl)->ParentLeafIndicator);
     strcat(Query3, "','");
     strcat(Query3, (*dl)->ChildOperationsItemId);
     strcat(Query3, "','");
     strcat(Query3, (*dl)->ChildOperationsItemType);
     strcat(Query3, "','");
     strcat(Query3, (*dl)->ChildOperationsItemSubType);
     strcat(Query3, "','");
     strcat(Query3, (*dl)->ChildKnownbyId);
     strcat(Query3, "','");
     strcat(Query3, (*dl)->ChildInternalStatus);
     strcat(Query3, "','");
     append_char(Query3, (*dl)->ChildLeafIndicator);
     strcat(Query3, "','");
     strcat(Query3, (*dl)->LinkInternalStatus);
     strcat(Query3, "')");

     EXEC SQL EXECUTE IMMEDIATE :Query3;

     EXEC SQL COMMIT;
}
};

1 个答案:

答案 0 :(得分:1)

好的我已经解决了,我在调用EXEC SQL SET TRANSACTION ISOLATION LEVEL NO COMMIT;查询之前添加了strcpy(Query2, "DELETE FROM QTEMP/P6SUBFCH");

这是我的完整代码:

void LinkRepository::SaveResultsToTable(InputParameters inputParameters)
{
    EXEC SQL INCLUDE SQLCA;
    EXEC SQL BEGIN DECLARE SECTION;
    char Query2[2000] = { "" };
    char Query3[2000] = { "" };

    EXEC SQL END DECLARE SECTION;

    strcpy(Query2, "CREATE TABLE QTEMP/P6SUBFCH");
    strcat(Query2, " (PAOPIID CHAR(21) NOT NULL,");
    strcat(Query2, " POPITPE CHAR(10),");
    strcat(Query2, " POPISTPE CHAR(10),");
    strcat(Query2, " POPIKNID CHAR(20),");
    strcat(Query2, " PINSTAT CHAR(10),");
    strcat(Query2, " PLEAFIND CHAR(1),");
    strcat(Query2, " CLOPIID CHAR(21),");
    strcat(Query2, " COPITPE CHAR(10),");
    strcat(Query2, " COPISTPE CHAR(10),");
    strcat(Query2, " COPIKNID CHAR(20),");
    strcat(Query2, " CINSTAT CHAR(10),");
    strcat(Query2, " CLEAFIND CHAR(1),");
    strcat(Query2, " LINKIN CHAR(10))");

    EXEC SQL EXECUTE IMMEDIATE :Query2;

    // Error handling
    if (sqlca.sqlcode == -601)
    {
         EXEC SQL SET TRANSACTION ISOLATION LEVEL NO COMMIT;

         strcpy(Query2, "DELETE FROM QTEMP/P6SUBFCH");

         EXEC SQL EXECUTE IMMEDIATE :Query2;

    }

    EXEC SQL COMMIT;

    // Datalinks is a vector that has all the filter data to insert into the QTEMP/P6SUBFCH table
    for(vector<Datalink*>::iterator dl = Datalinks.begin(); dl != Datalinks.end(); ++dl)
    {
         EXEC SQL SET TRANSACTION ISOLATION LEVEL NO COMMIT;

         strcpy(Query3, "INSERT INTO QTEMP/P6SUBFCH (PAOPIID,POPITPE,");
         strcat(Query3, "POPISTPE,POPIKNID,PINSTAT,PLEAFIND,");
         strcat(Query3, "CLOPIID,COPITPE,COPISTPE,COPIKNID,CINSTAT,");
         strcat(Query3, "CLEAFIND,LINKIN)");
         strcat(Query3, "VALUES('");
         strcat(Query3, (*dl)->ParentOperationsItemId);
         strcat(Query3, "','");
         strcat(Query3, (*dl)->ParentOperationsItemType);
         strcat(Query3, "','");
         strcat(Query3, (*dl)->ParentOperationsItemSubType);
         strcat(Query3, "','");
         strcat(Query3, (*dl)->ParentKnownbyId);
         strcat(Query3, "','");
         strcat(Query3, (*dl)->ParentInternalStatus);
         strcat(Query3, "','");
         append_char(Query3, (*dl)->ParentLeafIndicator);
         strcat(Query3, "','");
         strcat(Query3, (*dl)->ChildOperationsItemId);
         strcat(Query3, "','");
         strcat(Query3, (*dl)->ChildOperationsItemType);
         strcat(Query3, "','");
         strcat(Query3, (*dl)->ChildOperationsItemSubType);
         strcat(Query3, "','");
         strcat(Query3, (*dl)->ChildKnownbyId);
         strcat(Query3, "','");
         strcat(Query3, (*dl)->ChildInternalStatus);
         strcat(Query3, "','");
         append_char(Query3, (*dl)->ChildLeafIndicator);
         strcat(Query3, "','");
         strcat(Query3, (*dl)->LinkInternalStatus);
         strcat(Query3, "')");

         EXEC SQL EXECUTE IMMEDIATE :Query3;

         EXEC SQL COMMIT;
    }
};