Sends a SQL statement to the data source, where the statement is processed.
SQLEXEC(nStatementHandle [, cSQLCommand [, cCursorName[, aCountInfo]]])
- Specifies the statement handle to the data source returned by the SQLCONNECT(В ) function. For more information, see SQLCONNECT( ) Function.
- Specifies the SQL statement passed to the data source. The SQL statement can contain a parameterized WHERE clause, which creates a parameterized view. You must define all parameters in the WHERE clause before issuing SQLEXEC(В ). For example, if the parameters are variables, the variables must be created and initialized before SQLEXEC(В ) is issued. For more information about parameterized views, see How to: Create Parameterized Views. You can include expressions in the SQL statement. Visual FoxPro evaluates all expressions in the SQL statement that you pass before sending them to the data source. Visual FoxPro can evaluate memory variable names, function calls, and expressions enclosed in parentheses as parameter values.
- Specifies the name of the Visual FoxPro cursor to which the result set is sent. If you don't include a cursor name, Visual FoxPro uses the default name SQLRESULT. For multiple result sets, new cursor names are derived by appending an incremented number to the name of the first cursor.
Specifies the name of the array to populate with row count information. If the array doesnвЂ™t exist, it is created. The array has two columns: 1 вЂ“ Alias, 2 вЂ“Count.
Column Array contents Data type Description
Indicates that SQL command did not return any results. Either no records were returned or the SQL command failed before results could be returned. (final SQLMORERESULTS call) or execution failed before any result could be processed. Can be only on the first row. Count column for the row contains value -1.
Non-empty uppercase string
Alias of the cursor вЂ“ target for the record fetch operation. The Count column for the row contains the number of fetched records or -1 if fetch failed. If Count is -1, cursor may not have been created. During asynchronous execution, the fetch process for a cursor can be split between multiple SQLMORERESULTS or SQLEXEC calls; each call returns its own fetch count for the cursor.
Indicates that the SQL command (INSERT, UPDATE, or DELETE) did not return a result set.
Number of affected or fetched records.
Indicates the number of affected records as returned by the ODBC SQLRowCount function. Returns -1 if the number of records is unavailable.
Numeric data type. SQLEXEC(В ) returns the number of result sets if there is more than one. SQLEXEC(В ) returns 0 if it is still executing and returns 1 when it has finished executing. SQLEXEC(В ) returns вЂ“1 if a connection level error occurs.
If the SQL statement you want to pass is quite long, check to see if it exceeds the maximum length of a string literal in Visual FoxPro, which is 255 characters. Longer strings will cause a "Command contains unrecognized phrase/keyword" error. However, you can pass long SQL statements if you break them up into several concatenated literals. For example:
lnRetVal = SQLEXEC(lnHandle, "SELECT <long list of fields> " + ; "FROM <several tables> " + ; "WHERE <complex filter expression>")
If SQLEXEC(В ) is used to execute a SQL statement prepared with SQLPREPARE(В ), only the connection handle argument nStatementHandle is required. The cSQLCommand and CursorName arguments should be omitted. For more information, see SQLPREPARE( ) Function.
If the SQL statement generates one result set, SQLEXEC(В ) stores the result set to the specified Visual FoxPro cursor. If the SQL statement generates two or more result sets, you can name each result set by setting the connection's BatchMode property to False (.F.) using the SQLSETPROP(В ) function and changing the cursor name each time you call the SQLMORERESULTS(В ) function. Otherwise, SQLEXEC(В ) names each result set by appending sequential numbers to the name of the first one.
SQLEXEC(В ) is one of the four functions that you can execute either synchronously or asynchronously. The Asynchronous setting of SQLSETPROP(В ) determines whether these functions execute synchronously or asynchronously. In asynchronous mode, you must call SQLEXEC(В ) repeatedly until it returns a value other than 0 (still executing).
The following example shows various ways to use SQLEXEC(В ) to execute ad-hoc queries and to call or create stored procedures:
CLEAR LOCAL lnConn LOCAL lnPercent AS Int && Input parameters must be typed. LOCAL lnOutput lnPercent = 50 lnOutput = 0 * Make connection, assuming a local trusted connection. lnConn = SQLCONNECT('local') IF m.lnConn > 0 && Success. * Set the active database to PUBS. SQLEXEC(m.lnConn, 'use pubs') * Execute SELECT statement. SQLEXEC(m.lnConn, 'SELECT * FROM authors', 'PubAuthors') BROWSE * Execute INSERT statement, get value of identity field. SQLEXEC(m.lnConn, "INSERT INTO JOBS (job_desc, min_lvl, max_lvl); VALUES ('Developer',75,150)") SQLEXEC(m.lnConn, "SELECT SCOPE_IDENTITY()", "job_id") ? "ID for added Job is " + LTRIM(STR(job_id.exp)) * Execute DELETE statement. Get number of records affected. SQLEXEC(m.lnConn, "DELETE FROM JOBS WHERE job_desc ='Developer'") SQLEXEC(m.lnConn, "SELECT @@ROWCOUNT", 'rowcount') ? rowcount.exp, "record(s) deleted" * Call a stored procedure with no parameters. SQLEXEC(m.lnConn, 'sp_who', 'activeusers') BROWSE * Execute stored procedure with an INPUT parameter. SQLEXEC(m.lnConn, 'exec byroyalty ?lnPercent','HalfOffAuthors') * Create temp stored procedure with OUTPUT parameter and call it. SQLEXEC(m.lnConn, "CREATE PROCEDURE #MyProc @outparam int OUTPUT AS; SELECT @outparam=100") SQLEXEC(m.lnConn, "exec #myProc ?@lnOutput") ? m.lnOutput * Create a temp stored procedure with INPUT and OUTPUT parameters * and call it. SQLEXEC(m.lnConn, "CREATE PROCEDURE #MyProc2 " + ; "@inputparam INT, " + ; "@outparam int OUTPUT " + ; "AS SET @outparam=@inputparam*10") SQLEXEC(m.lnConn, "exec #myProc2 ?lnPercent, ?@lnOutput") ? m.lnOutput * Get version information. SQLEXEC(m.lnConn, 'SELECT @@VERSION','SQLVersion1') ? STRTRAN(SQLVersion1.Exp,CHR(0)) * Disconnect. SQLDISCONNECT(m.lnConn) ELSE ? "Unable to connect to SQL Server" ENDIF RETURN