Extract a table with filters in Python via SAP RFC

256 views Asked by At

I am trying to extract a table (BSIS) in Python via SAP RFC_READ_TABLE with the following code:

class Main:
    def __init__(self):
        self.conn = Connection(ashost=ASHOST, sysnr=SYSNR, client=CLIENT, user=USER, passwd=PASSWD)

    def qry(self, Fields, SQLTable, Options, MaxRows, FromRow):
        if Fields[0] == '*':
            Fields = ''
        else:
            Fields = [{'FIELDNAME': str(x)} for x in Fields]
        if Options:
            options = [{'TEXT': 'AND'.join(Options)}]
        else:
        tables = self.conn.call("RFC_READ_TABLE", QUERY_TABLE=SQLTable, DELIMITER='|', FIELDS=Fields, 
                            OPTIONS=Options, ROWCOUNT=MaxRows, ROWSKIPS=FromRow)
    
        fields = []
        data_fields = tables["DATA"]
        data_names = tables["FIELDS"]
        headers = [x['FIELDNAME'] for x in data_names]
        long_fields = len(data_fields)
    

        for line in range(0, long_fields):
            fields.append(data_fields[line]["WA"].strip())

        fields = [x.strip().split('|') for x in fields]

        return fields, headers

s = Main()
table = 'BSIS'
fields = ['BUKRS','BELNR', 'GJAHR', 'BLDAT', 'BUDAT', 'MONAT']
lastmonth = '06'
actmonth = '07'
actualyear = '2023'
soc1 = 'BP01'
soc2 = 'BP02'
minlist = '0100000228'
maxlist = '1300018757'
where = [f" BELNR BETWEEN '{minlist}' AND '{maxlist}' AND  BUKRS IN ('BP01', 'BP02') AND
MONAT IN ( '{lastmonth}', '{actmonth}' ) AND GJAHR = '{actualyear}' "]
results, headers = s.qry(fields, table, where, maxrows, fromrow)

The filter does not work for all columns, I do not get an error but it only works for the columns BELNR and MONAT.

Notice that in the filter (where) I am using f-strings, I also tried with .format function but it did not work. I need all those filters.. I followed the idea of https://blogs.sap.com/2020/06/09/connecting-python-with-sap-step-by-step-guide/

I expect to have all the filters running simultaneously.

0

There are 0 answers