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

 

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

Syntax Syntax

  1. execute (sqlParms = [])
End of the code.
execute Method: Attributes

Attribute

Description

sqlParms

Parameter list with input parameters

Result:

Example

Executing a SELECT statement

Syntax Syntax

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

Result

<SapDB_ResultSet object at ...>

Calling a Database Procedure (Stored Procedure)

You create the avg_price database procedure:

Syntax Syntax

  1. 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');""")
    
End of the code.

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

Syntax Syntax

  1. call = session.prepare ('call hotel.avg_price (?, ?)')
End of the code.

You specify a value for the input parameter:

Syntax Syntax

  1. in1 = 20005  # sets zip
End of the code.

You call the database procedure using the execute method.

Syntax Syntax

  1. out1 = call.execute ([in1])
End of the code.

You display the value of the output parameter:

Syntax Syntax

  1. print 'value avg_price:', out1
End of the code.
Result

value avg_price: (135.0)