Show TOC

execute Method (SapDB_Prepared Class)Locate this document in the navigation structure

Use

execute is a method of the SapDB_Prepared class. You can use this method to execute SQL statements with parameters, for example database procedures.

Features
execute (sqlParms = [])
			

Attribute

Description

sqlParms

Parameter list with input parameters

Result:

Example

Executing a SELECT statement

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

Result

<SapDB_ResultSet object at ...>

Calling a Database Procedure (Stored Procedure)

You create the avg_price database procedure:

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');""")
					
				

You create an object of the SapDBPrepared class using the prepare method:

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

You specify a value for the input parameter:

in1 = 20005  # sets zip
				

You call the database procedure using the execute method.

out1 = call.execute ([in1])
				

You display the value of the output parameter:

print 'value avg_price:', out1
				

Result

value avg_price: (135.0)