How/where to specify Open SQL in PyRFC call to SAP RFC_READ_TABLE
Question: How do I specify the Open SQL WHERE clause in an RFC_READ_TABLE query using
PyRFC?

I'm trying to get started with PyRFC to have python do table extractions from
SAP (in the absence of a supportive/cooperative basis team). In this example
from <http://scn.sap.com/community/scripting-
languages/blog/2012/11/04/revisiting-python-and-sap-with-pyrfc>, they use:

    
    
    pyrfc.Connector.call("RFC_READ_TABLE", QUERY_TABLE=table, DELIMITER='|')
    

<http://saplsmw.com/node/101> says that a WHERE clause needs to be passed as
an OPTION to the RFC call. How do I do this in PyRFC? (OPTIONS is an exporting
variable of type table in RFC_READ_TABLE's function module declaration on the
SAP end).

EDIT: OK <http://scn.sap.com/community/scripting-
languages/blog/2014/05/05/python-for-basis> has an example of sending the
WHERE clause in OPTIONS:

    
    
    OPTIONS = [{'TEXT':source_where}])
    

So it looks like the syntax is an array (maps the SAP table type) of single
element dictionaries where the key is the SAP data type and the value is the
WHERE clause.

So next question is: How do I specify a PACKAGE SIZE to be sent to
RFC_READ_TABLE so that I can extract large tables without hitting internal
table limits?


Answer: RFC_READ_TABLE has a parameter 'ROWCOUNT' which specifies the maximum number
of rows to return in a single call.

Of course, if you say restrict it to 1000 rows at a time there may be other
rows that you never download if the table contains more than 1000 rows.

To solve this there is another parameter, 'ROWSKIPS' through which you specify
the starting row you want to return.

So, with the first call ROWCOUNT = 1000 ROWSKIPS = 0

The next call ROWCOUNT = 1000 ROWSKIPS = 1000

The next call ROWCOUNT = 1000 ROWSKIPS = 2000

and so on, incrementing ROWSKIPS each time as follows: ROWSKIPS = ROWSKIPS +
ROWCOUNT.

An example PyRFC call with both ROWCOUNT and ROWSKIPS defined, which reads
from TCURR in batches of 10 (and where FCURR is set to 'USD'):

    
    
    #!/usr/bin/env python
    from pyrfc import Connection, ABAPApplicationError, ABAPRuntimeError, LogonError, CommunicationError
    from ConfigParser import ConfigParser
    from pprint import PrettyPrinter
    
    def main():
    
        try:
    
            config = ConfigParser()
            config.read('sapnwrfc.cfg')
            params_connection = config._sections['connection']
            conn = Connection(**params_connection)
    
            options = [{ 'TEXT': "FCURR = 'USD'"}]
            pp = PrettyPrinter(indent=4)
            ROWS_AT_A_TIME = 10 
            rowskips = 0
    
            while True:
                print u"----Begin of Batch---"
                result = conn.call('RFC_READ_TABLE', \
                                    QUERY_TABLE = 'TCURR', \
                                    OPTIONS = options, \
                                    ROWSKIPS = rowskips, ROWCOUNT = ROWS_AT_A_TIME)
                pp.pprint(result['DATA'])
                rowskips += ROWS_AT_A_TIME
                if len(result['DATA']) < ROWS_AT_A_TIME:
                    break
    
        except CommunicationError:
            print u"Could not connect to server."
            raise
        except LogonError:
            print u"Could not log in. Wrong credentials?"
            raise
        except (ABAPApplicationError, ABAPRuntimeError):
            print u"An error occurred."
            raise
    
    if __name__ == '__main__':
        main()
    

