A stored procedure can have zero or more parameters. It can also return a value. In OLE DB, parameters to a stored procedure can be passed by:
To support parameters, the ICommandWithParameters interface is exposed on the command object. To use parameters, the consumer first describes the parameters to the provider by calling the ICommandWithParameters::SetParameterInfo method (or optionally prepares a calling statement that calls the GetParameterInfo method). The consumer then creates an accessor that specifies the structure of a buffer and places parameter values in this buffer. Finally, it passes the handle of the accessor and a pointer to the buffer to Execute. On later calls to Execute, the consumer places new parameter values in the buffer and calls Execute with the accessor handle and buffer pointer.
A command that calls a temporary stored procedure using parameters must first call ICommandWithParameters::SetParameterInfo to define the parameter information, before the command can be successfully prepared. This is because the internal name for a temporary stored procedure differs from the external name used by a client and SQLOLEDB cannot query the system tables to determine the parameter information for a temporary stored procedure.
These are the steps in the parameter binding process:
An accessor is identified by its handle, which is of type HACCESSOR. This handle is returned by the CreateAccessor method. Whenever the consumer finishes using an accessor, the consumer must call the ReleaseAccessor method to release the memory it holds.
When the consumer calls a method, such as ICommand::Execute, it passes the handle to an accessor and a pointer to a buffer itself. The provider uses this accessor to determine how to transfer the data contained in the buffer.
When executing a stored procedure in SQL Server 2000, Microsoft OLE DB Provider for SQL Server (SQLOLEDB) supports the:
When the ODBC CALL syntax is used in calling a stored procedure, the provider calls a helper function to find the stored procedure parameter information. Therefore, you do not need to call the ICommandWithParameters::SetParameterInfo method to describe the parameters to the provider.
If you are not sure about the parameter information (parameter meta data), ODBC CALL syntax is recommended.
The general syntax for calling a procedure by using the ODBC CALL escape sequence is:
{[?=]call procedure_name[([parameter][,[parameter]]...)]}
For example:
{call SalesByCategory('Produce', '1995')}
The PRC escape sequence is similar to the ODBC CALL syntax of calling a stored procedure. The RPC escape sequence provides most optimal performance among the three methods of calling a stored procedure.
When the RPC escape sequence is used to execute a stored procedure, the provider does not call any helper function to determine the parameter information (as it does in the case of ODBC CALL syntax). This improves the performance. In this case, you need to provide the parameter information by executing ICommandWithParameters::SetParameterInfo.
The RPC escape sequence requires you to have a return value. If the stored procedure does not return a value, the server returns a 0 by default. In addition, you cannot open a SQL Server cursor on the stored procedure. The stored procedure is prepared implicitly and actual call the ICommandPrepare::Prepare will fail.
If you know all the parameter meta data, RPC escape sequence is the recommended way to execute stored procedures.
This is an example of RPC escape sequence for calling a stored procedure:
{rpc SalesByCategory}
The ODBC CALL escape sequence and the RPC escape sequence are the preferred methods for calling a stored procedure rather than the Transact-SQL EXECUTE statement. SQLOLEDB uses the remote procedure call (RPC) mechanism of SQL Server 2000 to optimize command processing. This RPC protocol increases performance by eliminating much of the parameter processing and statement parsing done on the server.
This is an example of the Transact-SQL EXECUTE statement:
EXECUTE SalesByCategory 'Produce', '1995'
Execute stored procedure using ODBC CALL syntax and process return code and output parameters
Execute stored procedure using RPC syntax and process return code and output parameters