简体   繁体   中英

C++ DB2 Clearing a non Journal table in QTEMP

I am developing a C++ program to fetch and filter through the data, and store it into a temporal table (P6SUBFCH) in the QTEMP library on DB2 AS400.

Reason for doing this, we using PLEX that has a defined method of fetching data in blocks of 64 records at a time, so the function called a SQLBlockFetch, fetches all the records into a temporal table, and only 64 records are returned at a time from this temporary table, and processed as such.

I am developing my own SQLBockfetch, and the C++ part of it, is using embedded SQL, to do all SQL queries. Which is fetching all the data creating the table (P6SUBFCH) in the QTEMP library.

The QTEMP library is active for a session, as soon as a session ends all tables are drops in that library.

Also to note is that all tables in QTEMP are not journaled, and from what I have researched on google is that it can't be.

The problem
My C++ program works great the first time it is called in a session, but the second time, it just appends the data to the previous data in the table QTEMP/P6SUBFCH. which is a problem, I tried clearing the data first (SQL DELETE statement) but I get this SQL error on the AS400 job logs.

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

SQL ERROR:-7008

I have read the following ql7008-error-workaround

To disable transaction isolation

Not sure if this is maybe my problem? I am still new to embedded SQL, not sure how I'll go about doing this.

Here is my code:

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;
}
};

Ok I worked it out, I just added the EXEC SQL SET TRANSACTION ISOLATION LEVEL NO COMMIT; before I called the strcpy(Query2, "DELETE FROM QTEMP/P6SUBFCH"); query.

Here is my full code:

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;
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM