cancel
Showing results for 
Search instead for 
Did you mean: 

Filtering Records while querying data using SAP Java Connector

Former Member
0 Kudos

Hi

I am new to the SAP community Network, and I have a question related to filtering records while querying data using SAP Java connector. Here is my code :

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 is 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 in advance.

Tarun

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi, Tarun

Before exectuting bapi you can define fields you want like below

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

Thanks

Former Member
0 Kudos

It works, Thanks a lot.

Former Member
0 Kudos

Also wanted to check if there is a way to filter records based on the value of a field.

Thanks

Tarun

Former Member
0 Kudos

Hi, Tarun

You can define search condition like sql where clause.

    JCoTable table = function.getTableParameterList().getTable("OPTIONS");

    table.appendRow();

    table.setValue("TEXT", "TABCLASS = 'TRANSP'");

    table.appendRow();

    table.setValue("TEXT", "AND AS4USER = 'DDIC'");

Thanks

Former Member
0 Kudos

Thanks!

Answers (0)