You may receive a return of -1 from SQL Server ODBC Driver version 3.70.0623 in Visual FoxPro for Windows (240137)
The information in this article applies to:
- Microsoft Visual FoxPro for Windows 3.0
- Microsoft Visual FoxPro for Windows 3.0b
- Microsoft Visual FoxPro for Windows 5.0
- Microsoft Visual FoxPro for Windows 5.0a
- Microsoft Visual FoxPro for Windows 6.0
- Microsoft ODBC Driver for SQL Server 3.7
This article was previously published under Q240137 SYMPTOMS
When executing a SQL Server stored procedure and passing parameters through SQL pass through, a return of -1 (error) might be received even though the stored procedure is successfully executed. This occurs when writing data from a FoxPro memo field to a SQL text column.
CAUSE
SQL Server Transact-SQL supports two methods of building SQL statements at run time in scripts, stored procedures, and triggers:
- The EXECUTE statement, which does not support parameter substitution in the executed string.
- The stored procedure sp_executesql, which does support parameter substitution in the executing string.
When parameter place holders are detected in the SQL statement passed to the SQL Server ODBC driver version 3.70, sp_executesql is called and the statement to be executed is passed as a parameter.
The SQL statement, passed as a parameter to sp_executesql, is not compiled until the sp_executesql statement is executed. The contents of the statement are then compiled and executed as an execution plan separate from the execution plan of the batch that called sp_executesql. The sp_executesql batch cannot reference variables declared in the batch calling sp_executesql. Local cursors or variables in the sp_executesql batch are not visible to the batch calling sp_executesql, and changes in database context last only until the end of the sp_executesql statement.
The SQL Server driver version 3.70 uses sp_executesql for parameterized queries and there is no way to turn off the SQLPrepare logic of the driver. It tries to generate a temporary table but the temp table releases outside of the context of the stored procedure.
RESOLUTION
There are four ways to address this issue:
- Use version 3.6 or version 2000.85.1117.00 of the SQL Server ODBC driver.
- Define the field in the FoxPro table that is to be written to the SQL TEXT column as character 254, rather than MEMO.
- Add a "SELECT 1" to the SQL stored procedure.
- Use ASYNCHRONOUS processing.
Of these, the use of ASYNCHRONOUS processing is most favorable because:
- Keeping the SQL driver at an earlier version may prove impossible as new products are installed that may update it.
- Developers might need to write data from memo fields.
- Adding a "SELECT 1" to the SQL stored procedure forces it to return a 1 (success) each time it is called, regardless of whether it actually works.
Note that because of the way ASYNCHRONOUS processing works (FoxPro does not wait for an SQLEXEC to finish before continuing processing), code that follows an SQLEXEC() command might fail if the code is dependant on the return of that call. A loop, checking for success of the SQLEXEC call, might be necessary.
STATUSMicrosoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.
Modification Type: | Major | Last Reviewed: | 3/17/2005 |
---|
Keywords: | kbCodeSnippet kbpending kbprb kbSQLProg KB240137 |
---|
|