0

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.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • It's not an issue about the table columns, it's probably an issue about the parameter `OPTIONS` (i.e. the where clause), which is made of lines each of maximum 72 characters (as you can see in the blog post you refer to, in the screenshot below "*In the OPTIONS section we mention the condition FCURR = ‘USD’*"). Better define one column condition per line to avoid truncating. – Sandra Rossi Jul 22 '23 at 08:36
  • Thank you @SandraRossi. What do you mean when you said 'define one column condition per line'? Could you explain me please? – Camila Lopez Jul 24 '23 at 13:12
  • Line 1: `BELNR BETWEEN '{minlist}' AND '{maxlist}'`. Line 2: `AND BUKRS IN ('BP01', 'BP02')`. This way, no need to count the number of characters, lines would probably be less than 72 characters. – Sandra Rossi Jul 24 '23 at 14:01
  • Hello @SandraRossi I tried with where = [ line1: f" BELNR BETWEEN '{minlist}' AND '{maxlist}' AND " line2: f"MONAT IN ( '{mesanterior}', '{mesactual}' ) AND " line3: f"GJAHR = '{actualyear}' " ] But the filter for the GJAHR field does not work, in the extraction I obtain all the years. – Camila Lopez Jul 24 '23 at 15:30
  • You don't understand. `RFC_READ_TABLE` has a parameter `OPTIONS` which will receive the where clause. `OPTIONS` has to contain a table of lines of maximum 72 characters. Your Python code is incorrect, you are mixing `AND` several times, everything will be concatenated into one line. I hope you understand now. – Sandra Rossi Jul 24 '23 at 16:43
  • So sorry @SandraRossi, I am a begginer in Python and SAP. This was what I understood: options1 = [{ 'TEXT': f"GJAHR = '{actualyear}' "}] options2 = [{ 'TEXT': f"BUKRS IN ('{soc1}', '{soc2}') "}] and so on.. But I think that I have to change the parameters in qry(self, Fields, SQLTable, Options, MaxRows, FromRow) and s.qry(fields, table, options, maxrows, fromrow) right? – Camila Lopez Jul 24 '23 at 21:01
  • I don't understand what are `options1` and others. If you don't master Python then remove everything you don't master e.g. run `RFC_READ_TABLE` directly without any variable, without any function, you'll probably end up with something which works, and after that you add complexity. Good luck. – Sandra Rossi Jul 25 '23 at 04:53

0 Answers0