[freetds] (ODBC) processing multiple statements on one connectio n

Roger Reynolds rogerr at softix.com
Fri Apr 30 03:10:50 EDT 2004

Right off, let state that the following may likely have
more to do with ODBC in general, than freetds in particular,
but maybe somebody here can help me out (I hope).

Essentially, the problem is that I need to operate on two
statements against the same connection in parallel.
That is, as each row is fetched from one statement, to be
able to execute a second statement on that same connection.

The following code illustrates:

    // assume you have a connection handle called svc
    // assume you have a table called "test_table" with 
    // an int column "t1" and test_table has at least 
    // one row of data
    HSTMT stmtHandle;
    SQLAllocStmt(svc, &stmtHandle);

    const char* s = "select t1 from test_table";

    SQLPrepare(stmtHandle, (SQLCHAR*) s, SQL_NTS);

    int val;
    long ind;
    SQLBindCol(stmtHandle, 1, SQL_C_LONG, &val, sizeof(val), &ind);

    while (SQLFetch(stmtHandle) == 0) {
	HSTMT s2;

	SQLAllocStmt(svc, &s2);

      printf ("val = %d\n", val);
	const char* s = "insert into t2 values(0)";
	if (SQLExecDirect(s2, (SQLCHAR*)s, SQL_NTS) != SQL_SUCCESS)
	    printf("s2 failed!\n");
	SQLFreeStmt (s2, SQL_CLOSE);
When I run this, I get the following error from SQLExecDirect:

    Attempt to initiate a new SQL Server operation with results pending.

and an SQLSTATE of 07005, and a "native error code" of 20019 

Interestingly, the description of SQLSTATE 07005 from


is described as
  Prepared statement not a cursor-specification

and isn't claimed to be generated by SQLExecDirect.

Anyway, please tell me if you can - there must be some attribute to set or
that would allow this scenario to succeed.


Thanks for your help.


More information about the FreeTDS mailing list