Installation guide
mxODBC - Python ODBC Database Interface
The ODBC syntax for calling a stored procedure is as follows:
{call procedure-name [([parameter][,[parameter]]...)]}
For stored functions or procedures with return status, the ODBC syntax is as
follows:
{? = call function-name [([parameter][,[parameter]]...)]}
Using the above syntax, you can call stored procedures through one of the
.execute*() calls, e.g.
results = cursor.execute("{call myprocedure(?,?)}", parameters)
will call the stored procedure myprocedure with the given input parameters.
It is also possible to use the
'named' parameter style, e.g.
results = cursor.execute("{call myprocedure(:a, :b)}", parameters)
Results can be retrieved through output parameter, input/output parameters, or
result sets. Depending on the database backend, it is also possible to combine
both.
Retrieving output parameters from stored procedures
When not providing the optional parametertypes parameter as in the above
examples, all parameters are considered to be input parameters, so
results will
be a tuple copy of
parameters.
6
If you want to use input/output or output parameters, you have to specify the
parametertypes parameter to define how to bind the variables to the procedure
parameters:
results = cursor.execute("{call myprocedure(?,?)}",
parameters, parametertypes)
Please see section 5.6.3 Input/Output and Output Parameters for more details on
how to use
parametertypes.
Values from input/output and output parameters will then be updated in the
results tuple copy as returned by the stored procedure.
Example:
from mx.ODBC.unixODBC import SQL
…
results = cursor.execute(
'{call sp_params(?,?)}',
[1, 0],
parametertypes=(SQL.PARAM_INPUT, SQL.PARAM_OUTPUT))
if results == [1, 3]:
print 'Works.'
For stored functions or procedures with return status, the syntax is similar (note
the prepended "
? =":
results = cursor.execute("{? = call myfunction(?,?)}",
6
Note that the results sequence is a tuple or list of tuples for the cursor.execute*()
methods, not a list as for
cursor.callproc(). This is due to the DB-API requiring a list for
cursor.callproc().
70