Calling Stored Procedures

Stored procedures can be called in several different ways with wwSQL. You can use either syntax that uses the AddParameter() method to add parameters to add named parameters to your SQL queries or you can use standard VFP SQL Passthrough syntax.

Using the AddSqlParameters() is the preferred mechanism however, since it results in calls that can be used with both wwSQL and wwADOSql consistently, while there are some differences with VFP SP syntax for the wwADOSql class since it doesn't use Sql Passthrough and only simulates it.

Let's take the following dummy stored procedure:

CREATE PROCEDURE test 
  @cName varchar(30),
  @iResult int OUTPUT
AS

select * from wws_customers where company like @cName

Set @iResult = 10
GO

This SP takes a couple of parameters one of which is an output parameter and it returns a result cursor.

The official way to do this is the following syntax:

* oSQL = CREATEOBJECT("wwSQL") && ODBC * oSQL.Connect("driver={sql server};server=(local);database=WebStore") oSQL = CREATEOBJECT("wwAdoSql") oSQL.nCodepage=65001 && UTF-8 oSQL.Connect("Provider=sqloledb;Data Source=(local);Initial Catalog=WebStore;Integrated Security=SSPI") *** Add named parameters oSQL.AddParameter("W%","cName") oSQL.AddParameter(0,"iResult","OUT") && Output parameter *** Make the SP call against the server IF !oSQL.ExecuteProcedure("test") ? oSQL.cErrorMsg RETURN ENDIF *** Retrieve a parameter lnResultValue = oSQL.oParameters["iResult"].Value *** Cursor returned from the SELECT BROWSE

The above is the most effective way and gives you the most control over parameter types. You can actually specify each parameters type and width, size etc.

Note:
If the Stored Procedure does not return a cursor but has any OUT parameters, you have to pass the llNoCursorReturned parameter to guarantee that the OUT parameters are picked up and stored back into the parameters collection.

Using traditional VFP SQL Passthrough Syntax:
We recommend you use the above syntax but for backwards compatibility you can also use Visual FoxPro SQL Passthrough syntax with Execute or ExecuteNonQuery when using wwSQL.

wwADOSQL does not work with the old PassThrough syntax if you use OUTPUT parameters. These will only be picked up with the new syntax above.

The following works in wwSQL but won't in wwAdoSql:

cName = "W%" iResult = 0 oSQL.Execute("execute Test ?cName,?@iResult") *** Retrieve parameters ? iResult ? o.Parameters["iResult"].Value

However, if the second parameter was not an OUTPUT parameter the call would work in wwADOSQL as well. To guarantee proper compatibility between wwSQL and wwADOSql use the AddParameter/ExecuteStoredProcedure combination.


  Last Updated: 4/29/2008 | © West Wind Technologies, 2008