Microsoft KB Archive/247370

= How to pass parameters to a SQL Server stored procedure =

Article ID: 247370

Article Last Modified on 1/24/2005

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0b Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 7.0 Professional Edition
 * Microsoft Visual FoxPro 8.0 Professional Edition
 * Microsoft Visual FoxPro 9.0 Professional Edition

-



This article was previously published under Q247370



SUMMARY
There are two ways to pass parameters to a stored procedure using SQLExec. One way, which works across all versions of Visual FoxPro, is to build the SQL command as a string variable. The advantage of this method is that you can check the string and see exactly which SQL command you are passing to the back end.

The other way is to pass the Foxpro variables preceded with question marks, as in a parameterized view. In Visual FoxPro version 5.0 and later versions, this allows you to obtain values from the stored procedure that are being returned as output parameters.



MORE INFORMATION
Follow these steps:   Create two stored procedures in SQL Server (see Books Online for the exact steps). Mysp_ObjectList merely takes the SysObjects table and returns the value you pass it once for every record in the table. In mysp_GetVersion, we elaborate a bit on the normal procedure for finding the version of the server. SELECT @@VERSION normally returns the SQL Server version as a record in a cursor. Here, we assign that result to an output parameter of the stored procedure. CREATE PROCEDURE mysp_GetVersion @tcVersion Char(200) Output AS SELECT @tcVersion = @@VERSION CREATE PROCEDURE mysp_ObjectList @tcParm1 CHAR(10) AS SELECT @tcParm1, name FROM sysobjects  Create a DSN called SPParmTest in the ODBC Administrator which links to the database where you created the above procedures.  Run the following code in Visual FoxPro: *!* Error-checking is omitted for the purposes of this sample: lnConn = SQLCONNECT("SPParmTest") lcParm1 = "ReturnThis" lcParm2 = "Then This"
 * !* you should always check the return values from SQL Passthrough calls.

lcCommand = "exec mysp_ObjectList '" + lcParm1 + "'" =SQLEXEC(lnConn, lcCommand) BROWSE USE
 * !* This is the first way, involving building a string
 * !* containing the parameters.

lcCommand = "exec mysp_ObjectList ?lcParm2" =SQLEXEC(lnConn, lcCommand) BROWSE USE
 * !* This is the second way, passing the FoxPro variables directly to
 * !* the SQL command. This will work in 3.0.

lcVersion = SPACE(200) lcCommand = "exec mysp_GetVersion ?@lcVersion" && Note the pass by reference. =SQLEXEC(lnConn, lcCommand) ?lcVersion
 * !* To get a value back from a stored procedure, initialize the
 * !* output variable first. This won't work under 3.0.

=SQLDISCONNECT(lnConn) && clean up. 

