Method execute

Use

execute is a method of the class SapDB_Prepared.

With this method you execute an SQL statement with parameters (for example a database procedure).

Features

execute (sqlParms = [])

sqlParms

Parameter list with the input parameters

Result:

?     Successful execution: see Possible SQL Results

Output parameters, if any

?     Error: exception of the class SQLError

Example

Executing an SQL statement

select = session.prepare ('SELECT * FROM hotel.customer WHERE cno = ?')
select.execute ([3000])

<SapDB_ResultSet object at …>

Calling a database procedure (stored procedure)

You create the database procedure avg_price:

session.sql ("""CREATE DBPROCEDURE avg_price (IN zip CHAR(5), OUT avg_price FIXED(6,2)) AS

    VAR sum FIXED(10,2); price FIXED(6,2); hotels INTEGER;

TRY

  SET sum = 0; SET hotels = 0;

  DECLARE dbproccursor CURSOR FOR

  SELECT price FROM hotel.room,hotel.hotel WHERE zip = :zip AND

  room.hno = hotel.hno AND type = 'single';

    WHILE $rc = 0 DO BEGIN

      FETCH dbproccursor INTO :price;

      SET sum = sum + price;

      SET hotels = hotels + 1;

    END;

CATCH

  IF $rc <> 100 THEN STOP ($rc, 'unexpected error');

CLOSE dbproccursor;

IF hotels > 0 THEN SET avg_price = sum / hotels

  ELSE STOP (100, 'no hotel found');""")

With the prepare method, you create an object of the class SapDB_Prepared:

call = session.prepare ('call hotel.avg_price (?, ?)')

You enter a value for the input parameter:

in1 = 20005    # sets zip

You call the database procedure with the execute method.

out1 = call.execute ([in1])

You display the value of the output parameter:

print 'value avg_price:', out1

value avg_price: (135.0)