Introduction
In our previous blog we saw how to read JIRA data in SQL Server. Now let’s learn how to Import SAP S/4HANA OData Service Data Into Sql Server. SAP S/4HANA provides OData REST API interface to access data in your application using HTTP Protocol. We will use ODBC XML Driver to read SAP data and load into SQL Server.
So, let’s get started.
Requirements
- A first requirement will be to SQL Server Database Engine Installed
- The second requirement will be SSMS installed
- Finally, make sure to have ZappySys ODBC PowerPack installed.
About SAP HANA / OData REST API Service
You can expose your SAP Data using ODATA REST API Service. Here is a good article which shows how to expose data as OData Service. For more information on SAP OData Service feature check this SAP help page. If you are new to OData Standard then read here to know more how OData can facilitate data extraction using HTTP REST API. If you need Sample XML based OData Service then use below test URLs.
1 2 3 4 5 |
https://services.odata.org/Northwind/Northwind.svc/ https://services.odata.org/Northwind/Northwind.svc/Customers https://services.odata.org/Northwind/Northwind.svc/Orders https://services.odata.org/Northwind/Northwind.svc/Invoices https://services.odata.org/Northwind/Northwind.svc/Products |
Now let’s look at how to read SAP Data using ODBC Driver. At this point we assume you have exposed your data as OData Endpoint.
Read SAP S/4HANA data using XML Driver
First let’s look at steps to configure XML Driver. We will read data from SAP S/4HANA OData Service and then in next section we will look at how to load data into SQL Server or other target.
- To do this, first of all, we will open the ZappySys Data Gateway Configuration:
- Add the Native – ZappySys XML Driver Data source.
- Now edit that XML data source to configure it. Enter your OData Service URL its typically like below. Replace 3 parts with your own value (i.e. replace {MY-INSTANCE}, {MY-PROJECT}, {MY-TABLE})
1https://{MY-INSTANCE}/sap/opu/odata/sap/{MY-PROJECT}/{MY-TABLE}
1https://myXXXXXX-api.s4hana.ondemand.com/sap/opu/odata/sap/MyTestProject/PurchaseOrders - Now go to Data Format / Compression (Zip/GZip) tab and select Data Format as OData to get all the records.
- Finally, now using Query Builder and Code Generator we will generate the query.
- That’s it we are ready to load SAP S/4HANA OData Service data to SQL Server.
Load SAP S/4HANA OData Service in MS SQL Server
- Assuming you have installed SQL Server and SSMS. If not then get both for FREE from here: Get SQL Server Express and Get SSMS
- Open SSMS and connect to SQL Server.
- Go to Root > Server Objects > Linked Servers node. Right click and click New Linked Server...
- Now enter the linked server name, select Provider as SQL Native Client
- Enter data source as GatewayServerName, PORT_NUMBER where server name is where ZappySys Gateway is running (Can be same as SQL Server machine or remote machine). Default PORT_NUMBER is 5000 but confirm on Data gateway > General tab in case its different.
- Enter Catalog Name. This must match name from Data gateway Data sources grid > Name column
- Click on Security Tab and select last option "Be made using this security context". Enter your gateway user account here.
-
Optional: Under the Server Options Tab, Enable RPC and RPC Out and Disable Promotion of Distributed Transactions(MSDTC).
RPC and MSDTC Settings
You need to enable RPC Out if you plan to use EXEC(...) AT [MY_LINKED_SERVER_NAME] rather than OPENQUERY.
If don't enabled it, you will encounter the 'Server "MY_LINKED_SERVER_NAME" is not configured for RPC' error.Query Example:
EXEC('Select * from Products') AT [MY_LINKED_SERVER_NAME]
If you plan to use 'INSERT INTO...EXEC(....) AT [MY_LINKED_SERVER_NAME]' in that case you need to Disable Promotion of Distributed Transactions(MSDTC).
If don't disabled it, you will encounter the 'The operation could not be performed because OLE DB provider "SQLNCLI11" for linked server "MY_LINKED_SERVER_NAME" was unable to begin a distributed transaction.' error.Query Example:
Insert Into dbo.Products EXEC('Select * from Products') AT [MY_LINKED_SERVER_NAME]
- Click OK to save Linked Server
- In SSMS execute below SQL query to test your connectivity.
SELECT * FROM OPENQUERY( MY_LINKED_SERVER_NAME, 'SELECT * FROM $')
- Here is the preview after you run some REST API query in SQL Server. Notice that you can override default configuration by supplying many parameters in WITH clause (second query example in the screenshot).
- You can wrap your queries inside View or wrap inside Stored procedure to parameterize. Here is an example of creating the view which calls REST API queries.
CREATE VIEW dbo.vw_MyAPICall_View AS /*Call REST API inside SQL Server View*/ SELECT * FROM OPENQUERY( MY_LINKED_SERVER_NAME , 'SELECT * FROM $'); GO
- Notice in above approach if you parameterize Stored Procedure then check this article to understand Dynamic Metadata.
- Now let's insert API data into the new data table "tblMyAPiData" in the SQL server database. For that, we need to execute below SQL query.
Select * into tblMyAPiData FROM OPENQUERY( MY_LINKED_SERVER_NAME , 'SELECT * FROM $')
- Let's insert and update records into the already created table "tblMyAPiData"
- To do that first we need to insert the new API data into the "#temp_tblMyAPiData" temporary database table.
- Now let's delete the old records from the database table which are updated.
- At the last insert all new API data into the database table.
- To do that, we need to query like below SQL queries.
SELECT * into #temp_tblMyAPiData FROM OPENQUERY(MY_LINKED_SERVER_NAME, 'SELECT * FROM $') DELETE FROM [dbo].[tblMyAPiData] WHERE id in (SELECT id FROM #temp_tblMyAPiData) INSERT INTO tblMyAPiData SELECT * FROM #temp_tblMyAPiData
- In the upper step, we see how to insert and update new records. Now if you want to update particular fields records only then you need to query like this.
Update dbtbl_1 set dbtbl_1.field1 = dbtbl_2.field1, dbtbl_1.field2 = dbtbl_2.field2 FROM tblMyAPiData dbtbl_1 JOIN (SELECT * FROM OPENQUERY(MY_LINKED_SERVER_NAME, 'SELECT * FROM $')) dbtbl_2 on dbtbl_1.id=dbtbl_2.id
*NOTE: If you are getting error like : "Cannot resolve the collation conflict between “Latin1_General_CI_AI” and “SQL_Latin1_General_CP1_CI_AS” in the equal to operation" then you need to query like this :
Update dbtbl_1 set dbtbl_1.field1 = dbtbl_2.field1, dbtbl_1.field2 = dbtbl_2.field2 FROM tblMyAPiData dbtbl_1 JOIN (SELECT * FROM OPENQUERY(MY_LINKED_SERVER_NAME, 'SELECT * FROM $')) dbtbl_2 on dbtbl_1.id=dbtbl_2.id COLLATE SQL_Latin1_General_CP1_CI_AS
SAP S/4HANA Integration with Other BI Tools (Power BI, Excel, SSRS, MS Access…)

BI / Reporting Tools Integration |
ETL Tools Integration | Programming Languages Integration |
|
|
|
Conclusion
So in this blog, we learned how to Import SAP S/4HANA OData Service Data in MS SQL Server using ODBC XML / SOAP API Driver in a very simple way. You can achieve many more functionalities with this tool. Check our blogs/articles on XML File / REST API Driver https://zappysys.com/blog/category/odbc-powerpack/odbc-drivers/xml-soap-api-driver/ to find out what this tool is capable of more.
References
Finally, you can use the following links for more information about the use of SAP S/4HANA OData Service with our tools:
- SAP API Business Hub
- Landing Page for ODBC XML / SOAP API Driver, you can also find Tutorial Video here.
- Help File: Documentation of XML Driver.
- Blog/articles link: https://zappysys.com/blog/category/odbc-powerpack/odbc-drivers/xml-soap-api-driver/