0

I want to run SQL script from a C++ program. my code goes like this:

int main()
{ 
//.....
sql_stmt = "Insert into t1 values ('qwerty');\nInsert into t1 values ('dothar');"
           "//and many more INSERT statements";

sql_stmt = "DECLARE\nrollback_check_counter number;\n"
"BEGIN\n"
"rollback_check_counter :=1;\n"
"SAVEPOINT sp_1;\nIF rollback_check_counter = 1 THEN\n"
"BEGIN\n"+sql_stmt+"EXCEPTION\n"
"WHEN PROGRAM_ERROR THEN\n"
"rollback_check_counter :=0;\n"
"ROLLBACK TO sp_1;\n"
"WHEN OTHERS THEN\n"
"rollback_check_counter :=0;\n"
"ROLLBACK TO sp_1;\n"
"END;\n"
"END IF;\n"
"commit;\n"
"END;";


try 
{
    Connection *conn = env->createConnection(user,passwd); //error prone
    Statement *stmt = conn->createStatement();
    stmt->setSQL(sql_stmt);
    row_count = stmt->execute(); //stmt->execute(sql_stmt);

    Connection::conn->terminateStatement(Statement *stmt);
    //con->terminateStatement(stmt);
    env->terminateConnection(conn);
    Environment::terminateEnvironment(env);
}
catch(SQLException& ex)
{}
//.....
return 0;
}

Although when i run these insert statement only they fairly run well but when i forms a SQL Script structure they seems to fail. I want to do so because i want to implement rollback. What am i missing? Could anyone suggest any alternative to implement it.

MegaByte
  • 3
  • 3
  • just dump the sql statement, and run it directily in a sqlplus-alike tool to check if it works. – douyw Apr 16 '15 at 15:33

1 Answers1

0

There are ; missing after both ROLLBACK TO sp_1

Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
  • I missed ; while rewriting it. Thats all. Thanks for reply but problem is still persistent. – MegaByte Jun 27 '14 at 10:45
  • BTW, you could get rid of `rollback_check_counter` (doesn't do anything useful here) and the `WHEN PROGRAM_ERROR THEN` part, since the `WHEN OTHERS THEN` block does just the same. – Erich Kitzmueller Jun 27 '14 at 11:03
  • Actually the sql_stmt consist of 400-450 insert statement. I intend to execute say 400-500 INSERTS at a time, so if any error occurs then [rollback_check_counter] comes into play and it can be handled. Regarding "WHEN PROGRAM_ERROR THEN block and WHEN OTHERS THEN block", I couldn't agree more, must have been lousy programing by me. – MegaByte Jun 27 '14 at 11:28