How to perform OData Query and CRUD operation in OData


In previous articles, we have discussed What is an OData and how to create an OData in SAP. We have also shown how to test an OData if it is working fine or not. Now, we need to learn how to read a table using an OData Query and also perform operations like Insert, Update and Delete on that table. In this article we will also learn basic queries of reading data.

How it works

In case of OData, there are three phases of data transfer. These are:

1. Sending Data from Frontend

Data is sent from UI (Client) to OData (Server) in form of query

2.      Taking Data from Frontend

At OData we receive data from Frontend using data provider (Code implemented in CRUD Operation section)

io_data_provider->read_entry_data(IMPORTING es_data= ls_entity).

3.      Sending Data to Frontend

From OData we send data to UI using et_entityset or er_entity after data manipulation (Code implemented in CRUD Operation section)

The above steps are shown in the image below:

How to perform OData Query and CRUD operation in OData

Operations in ODATA

In this section, we will explore all the methods generated automatically, once you generate an OData. In last article, we have already instructed to write all your CRUD operations related code in DPC_EXT class. Hence, we will follow the same concept here.

SAP ABAP OData provides different methods for CRUD operations, these are:

MethodSQL OperationDescription
GET_ENTITYSelectThis method is used to read a single data based on table keys
GET_ENTITYSETSelectThis method is used to read entire data of a table
CREATE_ENTITYInsertThis method is used to create/insert a new data in table
UPDATE_ENTITYUpdate/ModifyThis method is used to update an existing data in table
DELETE_ENTITYDeleteThis method is used to delete an existing data in table


Query in SAP OData

In this section, we will explore all the Query we can perform while reading data from OData. In above section, we have learnt how to do read calls using GET_ENTITY and GET_ENTITYSET. Sometimes, we need to filter out data according to our requirement, or to get total number of data counts or to get data in specific order. These all SAP OData Queries are explained below:
Here, let us suppose <your service name>  =<OData Service Name>

$metadataIt gives the metadata detail of your service. By metadata we mean it will provide information about all the entity sets with their field names and their attributes<your service name>/$metadata
$FILTERIt is mainly used during Read Entity Set call. During Read, we can send some filter value in backend that can be later used in where condition of Select queries<your service name>/$FILTER
$top and $skipIt is mainly used to get limited data in the UI, in case you are reading all the data, and the table is having a very large data<your service name>/$top and $skip
$orderbyThis is used to order the data in ascending or descending order for a specific key<your service name>/$orderby
$format=jsonThis returns the data in form of JSON format. By default, the result is in XML format.<your service name>/$format=json
$inlinecountThis returns the number of data of a table that will appear in the UI<your service name>/$inlinecount
$expandThis is used to bind the association and navigation data together<your service name>/$expand
$valueThis is used to return the media data<your service name>/$value

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.