简体   繁体   中英

Changing odbc timeout

I am trying to run following stored procedure using ODBC:

CREATE PROCEDURE [dbo].[Add]
--WITH ENCRYPTION
AS

DECLARE @LoopVar    BIGINT = 0
    , @MaxVar   BIGINT = 0
    , @rows     BIGINT = 0
SET @LoopVar = 1
set @rows = 125000

insert into debug values(987654321) 

insert into debug values(@LoopVar)
insert into debug values(@rows)


WHILE(@LoopVar <= @rows)
BEGIN

SET @LoopVar = @LoopVar + 1
    WAITFOR DELAY '00:00:01'
    insert into debug values(@LoopVar)  
END

insert into debug values(123456789) 


GO

The C++ code for running the stored procedure is:

RETCODE rc = SQL_SUCCESS;
HENV henv = SQL_NULL_HENV;
HDBC hdbc = SQL_NULL_HDBC;
SQLHSTMT hstmt = SQL_NULL_HSTMT;
SQLTCHAR * pszConnection = _T("DRIVER={SQL Server Native Client 10.0};Server=myserver;Trusted_Connection=Yes;Initial Catalog=testdb;");



SQLTCHAR * pszInsertStmt = _T("{call [testdb].[dbo].Add}");
SQLLEN cbParamLength;

SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HENV, &henv);
SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);

SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
SQLSetConnectAttr( hdbc, SQL_ATTR_LOGIN_TIMEOUT, reinterpret_cast<SQLPOINTER>(600), SQL_IS_UINTEGER);
SQLDriverConnect( hdbc, NULL, pszConnection, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT); 
SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
SQLSetStmtAttr(hstmt, SQL_QUERY_TIMEOUT, (SQLPOINTER)12000, SQL_IS_UINTEGER);
SQLSetStmtAttr(hstmt, SQL_ATTR_QUERY_TIMEOUT, (SQLPOINTER)12000, SQL_IS_UINTEGER);

SQLINTEGER attr;

rc = SQLGetStmtAttr( hstmt, SQL_ATTR_QUERY_TIMEOUT, &attr, 0, NULL ) ;
rc = SQLGetStmtAttr( hstmt, SQL_QUERY_TIMEOUT, &attr, 0, NULL ) ;

rc = SQLGetConnectAttr(hdbc, SQL_ATTR_CONNECTION_TIMEOUT, &attr, 0, NULL);

rc = SQLExecDirect(hstmt, pszInsertStmt, SQL_NTS);

if (!SUCCESS(rc)) {
  if (hstmt) 
     PrintError(SQL_HANDLE_STMT, hstmt);
  if (hdbc)
     PrintError(SQL_HANDLE_DBC, hdbc);
  if(henv)
     PrintError(SQL_HANDLE_ENV, henv);
}

if (hstmt)
  SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
if (hdbc) {
  SQLDisconnect(hdbc);
  SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
}
if (henv)
  SQLFreeHandle(SQL_HANDLE_ENV, henv);

I have set query time in the code above. The connection time out is 0 (which I believe means no timeout). But no matter what I do, the stored procedure times out in 78 seconds. Does any one have any idea as to what I should do so that stored procedure can run indefinitely?

Please note if I run the stored procedure from SQL Server Management Studio directly, it works just fine..

Thanks in advance, -Neel.

如果有人感兴趣,解决方案是将“ SET NOCOUNT ON”作为存储过程的第一行。

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