Calling stored procedures in Python

To call a stored procedure from a Python application, use ibm_db.callproc function. The procedure that you call can include input parameters (IN), output parameters (OUT), and input and output parameters (INOUT).

Before you begin

Obtain a connection resource by calling one of the connection functions in the ibm_db API.

Procedure

Call the ibm_db.callproc function by passing the listed arguments:
connection
A valid database connection resource that is returned from the ibm_db.connect or ibm_db.pconnect function.
procname
A valid stored procedure name
parameters
A tuple of parameters that matches the parameters that are declared in the stored procedure.

Example

To call a stored procedure with the ibm_db.callproc function:

import ibm_db
        
conn = ibm_db.connect("sample", "username", "password")
if conn:
  name = "Peaches"
  second_name = "Rickety Ride"
  weight = 0

  print "Values of bound parameters _before_ CALL:"
  print "  1: %s 2: %s 3: %d\n" % (name, second_name, weight)

  stmt, name, second_name, weight = ibm_db.callproc(conn, 'match_animal', (name, second_name, weight))
  if stmt is not None:
    print "Values of bound parameters _after_ CALL:"
    print "  1: %s 2: %s 3: %d\n" % (name, second_name, weight)

What to do next

If the procedure call returns one or more result sets, you can begin fetching rows from the statement resource.