1

I am attempting to write a simple java utility that extracts data from SAP into a MySQL database, using JCo. I have understood the JCo documentation and tried out the relevant examples mentioned in SAP help portal, I am able to retrieve data from Table and insert into MySQL DB.

What I would like to have is a facility to filter data in following two ways :

  1. I would like to fetch only the required fields.
  2. I would like to fetch rows only if the value of the a particular field matches certain pattern.

After doing some research I didn't find any way to specify query parameters so that it retrieves only the filtered data, it basically queries all the fields from a Table, I think I will have to filter out the data that I don't want in my java-client layer. Please let me know if I am missing out something here.

Here is a code example :

public static void readTables() throws JCoException, IOException {

    final JCoDestination destination = JCoDestinationManager
            .getDestination(DESTINATION_NAME2);
    final JCoFunction function = destination.getRepository().getFunction(
            "RFC_READ_TABLE");

    function.getImportParameterList().setValue("QUERY_TABLE", "DD02L");
    function.getImportParameterList().setValue("DELIMITER", ",");

    if (function == null) {
        throw new RuntimeException("BAPI RFC_READ_TABLE not found in SAP.");
    }

    try {
        function.execute(destination);
    } catch (final AbapException e) {
        System.out.println(e.toString());
        return;
    }

    final JCoTable codes = function.getTableParameterList().getTable(
            "FIELDS");
    String header = "SN";
    for (int i = 0; i < codes.getNumRows(); i++) {
        codes.setRow(i);
        header += "," + codes.getString("FIELDNAME");
    }
    final FileWriter outFile = new FileWriter("out.csv");
    outFile.write(header + "\n");
    final JCoTable rows = function.getTableParameterList().getTable("DATA");

    for (int i = 0; i < rows.getNumRows(); i++) {
        rows.setRow(i);
        outFile.write(i + "," + rows.getString("WA") + "\n");
        outFile.flush();
    }
    outFile.close();

}

This method tries to read a table where SAP stores meta data or data dictionary and writes the output to a csv file. This works fine but takes 30-40 secs and returns around 4 hundred thousand records with 32 columns. My intention was to ask if there is a way I can restrict my query to return only a particular field, instead of reading all the fields and discarding them in the client layer.

Thanks.

3
  • What have you tried so far? Please provide a complete sample of the code that is not working.
    – vwegert
    Oct 19, 2013 at 8:59
  • I've tried examples mentioned in SAP help portal and explored the API details, where I didn't find a way to filter data. Oct 19, 2013 at 9:24
  • This is not a complete code example.
    – vwegert
    Oct 19, 2013 at 11:26

1 Answer 1

1

This works fine :

JCoTable table = function.getTableParameterList().getTable("FIELDS");
table.appendRow();
table.setValue("FIELDNAME", "TABNAME");
table.appendRow();
table.setValue("FIELDNAME", "TABCLASS");

Please check this Thread

Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.