2014-02-22 70 views
0

我試圖運行使用下面的存儲過程ODBC:更改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 

運行存儲過程的C++代碼:

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

我已經設置查詢時間在上面的代碼。連接超時是0(我相信這意味着沒有超時)。但是無論我做什麼,存儲過程都會在78秒內超時。有沒有人知道我應該怎麼做,以便存儲過程可以無限期地運行?

請注意,如果我從SQL Server Management Studio中直接運行存儲過程,它工作得很好..提前

感謝, -Neel。

+0

你應該看/問[這裏](http://stackoverflow.com/questions/16041991/sql-attr-query-timeout-not-timing-out),我想 – Alexander

回答

0

如果有人感興趣,解決方案是將「SET NOCOUNT ON」作爲存儲過程的第一行。