Search code examples
pythonpyrfcsap-r3

Number of entries in SAP R/3 table using Pyrfc


How do you use the Pyrfc Python library to query the number of entries in an SAP R/3 database table?


Solution

  • I know of three methods to do this using Pyrfc. Modify the following example with your SAP R/3 server connection settings and desired table name:

    from pyrfc import Connection
    
    params = dict(
        ashost="1.1.1.1",
        sysnr="1",
        client="100",
        user="username",
        passwd="password",
    )
    table = "MKAL"
    with Connection(**params) as conn:
        # Method 1
        result = conn.call("RFC_GET_TABLE_ENTRIES", TABLE_NAME=table, MAX_ENTRIES=1)
        entries = result["NUMBER_OF_ENTRIES"]
    
        # Method 2
        result = conn.call("EM_GET_NUMBER_OF_ENTRIES", IT_TABLES=[{"TABNAME": table}])
        entries = result["IT_TABLES"][0]["TABROWS"]
    
        # Method 3
        short_field = "MANDT"  # table field with short data length
        result = conn.call(
            "RFC_READ_TABLE",
            QUERY_TABLE=table,
            ROWCOUNT=0,
            FIELDS=short_field,
        )
        entries = len(result)