1

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

这样做的原因是,我们使用 PLEX,它有一个定义的方法来一次获取 64 条记录的块中的数据,所以调用 SQLBlockFetch 的函数将所有记录提取到一个临时表中,一次只返回 64 条记录从这个临时表中,并照此处理。

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

QTEMP 库在会话中处于活动状态,一旦会话结束,所有表都将删除到该库中。

还要注意的是,QTEMP 中的所有表都没有记录,从我在谷歌上的研究来看,它不可能。

问题
我的 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;
}
};
4

1 回答 1

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;
    }
};
于 2012-08-10T09:43:12.443 回答