Skip to Content.
Sympa Menu

freetds - [freetds] [FreeTDS][SQL Server]Invalid cursor state error when using 2 statements from a connection.

freetds AT lists.ibiblio.org

Subject: FreeTDS Development Group

List archive

Chronological Thread  
  • From: BALAKRISHNAN RAJKUMAR <messagetobala AT gmail.com>
  • To: freetds AT lists.ibiblio.org
  • Subject: [freetds] [FreeTDS][SQL Server]Invalid cursor state error when using 2 statements from a connection.
  • Date: Thu, 5 Jan 2012 19:12:48 +0530

Hi All,


I am obtaining two handles from a connection and trying two execute
two queries on successive lines. While the first execute statement
succeeds, the second one reports this "invalid cursor state" error. Can we
use more thane one statement using the same connection in FreeTDS. Could
some one please clarify.

Below is the code snipped and the ouput. Full code is attached with
this email.

ret = SQLExecDirect(smt_handle1, (SQLCHAR*)"select * from
employee", SQL_NTS);

if(ret != SQL_SUCCESS)
{

cout<< "\n SQLExecDirect failed ";
}
else
{
cout<< "\nSQLExecDirect success";
}

ret = SQLExecDirect(smt_handle2, (SQLCHAR*)"select * from
employee", SQL_NTS);


if(ret != SQL_SUCCESS)
{
char status[10];
SQLINTEGER err;
char msg[100];
SQLSMALLINT msglen;


SQLGetDiagRec(SQL_HANDLE_STMT,smt_handle2,1,(SQLCHAR*)status,&err,(SQLCHAR*)msg,100,&msglen);

cout<< "\n SQLExecDirect failed [" << msg << "]";
}



Output:

[root@localhost OdbcClasses]# ./StatementTest

SQLConnect success.
SQLExecDirect success
SQLExecDirect failed [[FreeTDS][SQL Server]Invalid cursor state]

Thanks,
Bala.
#include <stdlib.h>
#include <iostream>
#include <string>

#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>

using namespace std;

char                     V_OD_stat[10]; 
SQLINTEGER               V_OD_err,V_OD_rowanz,V_OD_id;
SQLSMALLINT              V_OD_mlen;
char             V_OD_msg[200],V_OD_buffer[200];


int main(int argc, char** argv)
{

	SQLRETURN ret;

	SQLHENV env_handle;

	SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env_handle);

	SQLSetEnvAttr(env_handle, SQL_ATTR_ODBC_VERSION, (void*) SQL_OV_ODBC3, 0);

	SQLHDBC db_handle;

	SQLAllocHandle(SQL_HANDLE_DBC, env_handle, &db_handle);

	ret != SQLConnect(db_handle, (SQLCHAR*)"WinDSN", SQL_NTS, (SQLCHAR*)"raghav", SQL_NTS, (SQLCHAR*)"raghav", SQL_NTS);
	
	if(ret != SQL_SUCCESS)
	{
		string message;
		cout<< "\n SQLExecDirect failed [" << message.c_str()<< "]";
	}
	else
	{
		cout<< "\n SQLConnect success.";
	}		

	SQLHSTMT smt_handle1,smt_handle2;

	SQLAllocHandle(SQL_HANDLE_STMT, db_handle, &smt_handle1);

	SQLAllocHandle(SQL_HANDLE_STMT, db_handle, &smt_handle2);

	ret = SQLExecDirect(smt_handle1, (SQLCHAR*)"select * from employee", SQL_NTS);
	
	if(ret != SQL_SUCCESS)
	{
		cout<< "\n SQLExecDirect failed.";
	}		
	else
	{
		cout<< "\nSQLExecDirect success";
	}	

	ret = SQLExecDirect(smt_handle2, (SQLCHAR*)"select * from employee", SQL_NTS);


	if(ret != SQL_SUCCESS)
	{
		 char status[10];
                 SQLINTEGER err;
                 char msg[100];
                 SQLSMALLINT msglen;
                
		 SQLGetDiagRec(SQL_HANDLE_STMT,smt_handle2,1,(SQLCHAR*)status,&err,(SQLCHAR*)msg,100,&msglen);

 		 cout<< "\n SQLExecDirect failed [" << msg << "]";
	}		
	
	return 0;
}



Archive powered by MHonArc 2.6.24.

Top of Page