<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>polybase Archives | ZappySys Blog</title>
	<atom:link href="https://zappysys.com/blog/tag/polybase/feed/" rel="self" type="application/rss+xml" />
	<link>https://zappysys.com/blog/tag/polybase/</link>
	<description>SSIS / ODBC Drivers / API Connectors for JSON, XML, Azure, Amazon AWS, Salesforce, MongoDB and more</description>
	<lastBuildDate>Tue, 23 Dec 2025 13:56:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.4</generator>

<image>
	<url>https://zappysys.com/blog/wp-content/uploads/2023/01/cropped-zappysys-symbol-large-32x32.png</url>
	<title>polybase Archives | ZappySys Blog</title>
	<link>https://zappysys.com/blog/tag/polybase/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to import REST API in SQL Server (Call JSON / XML SOAP Service)</title>
		<link>https://zappysys.com/blog/import-rest-api-json-sql-server/</link>
		
		<dc:creator><![CDATA[ZappySys Team]]></dc:creator>
		<pubDate>Fri, 09 Mar 2018 20:23:23 +0000</pubDate>
				<category><![CDATA[JSON File / REST API Driver]]></category>
		<category><![CDATA[ODBC Gateway]]></category>
		<category><![CDATA[ODBC PowerPack]]></category>
		<category><![CDATA[T-SQL (SQL Server)]]></category>
		<category><![CDATA[XML File / SOAP API Driver]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[linked server]]></category>
		<category><![CDATA[odbc]]></category>
		<category><![CDATA[polybase]]></category>
		<category><![CDATA[rest api]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[xml]]></category>
		<guid isPermaLink="false">https://zappysys.com/blog/?p=2921</guid>

					<description><![CDATA[<p>Introduction In this article, you will see few Examples to import REST API in SQL Server Table (Call JSON / XML SOAP API).  You will also learn how to load JSON Files into SQL Server Table using T-SQL Stored procedure code.  So let&#8217;s get ready for fun 🙂 JSON File format and REST API (i.e. RESTful [&#8230;]</p>
<p>The post <a href="https://zappysys.com/blog/import-rest-api-json-sql-server/">How to import REST API in SQL Server (Call JSON / XML SOAP Service)</a> appeared first on <a href="https://zappysys.com/blog">ZappySys Blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2>Introduction</h2>
<p><a href="https://zappysys.com/blog/wp-content/uploads/2018/03/json-to-sql-server-import-export.png"><img decoding="async" class="alignleft wp-image-2924 size-thumbnail" src="https://zappysys.com/blog/wp-content/uploads/2018/03/json-to-sql-server-import-export-150x150.png" alt="JSON to SQL Server" width="150" height="150" /></a>In this article, you will see few <strong>Examples to import REST API in SQL Server Table (Call JSON / XML SOAP API). </strong> You will also learn <strong>how to load JSON Files into SQL Server Table using T-SQL Stored procedure</strong> code.  So let&#8217;s get ready for fun 🙂</p>
<p>JSON File format and REST API (i.e. RESTful API ) is becoming more and more popular each day. You may already have some data integration scenarios to <strong>read/write From REST API services in SQL Server</strong> or maybe API integration in other Apps such as Power BI, Tableau, SSIS, Informatica.</p>
<p>REST API integration can be challenging without the right set of tools. Even for highly skilled programmer sometimes its difficult to achieve the desired result.  But not to worry at the end of this article you will find out how easy it is to <strong>pull data from JSON / XML based web service using familiar SQL Query Language.</strong>  In the next few sections, you will learn how to call REST API inside SQL Server using familiar T-SQL code.</p>
<h2>Steps to Import JSON / XML REST API in SQL Server</h2>
<p>Here is the summary of steps to query REST API / JSON File in SQL Server (Using T-SQL code). For detailed steps read the full article and watch the video tutorial.</p>
<ol>
<li>Install <strong>ZappySys ODBC PowerPack </strong> <a href="https://zappysys.com/products/odbc-powerpack/" target="_blank" rel="noopener">from here</a> (it contains drivers for JSON / XML API)</li>
<li>Create <strong>ODBC DSN</strong> for JSON / XML API Source</li>
<li>Configure <strong>ZappySys Data Gateway Service</strong> so SQL Server can use ZappySys Drivers in T-SQL code</li>
<li>Create and <strong>Configure Linked Server</strong> to connect to JSON / REST API Service</li>
<li>Write some <strong>T-SQL code</strong> in your stored procedure like below to import data from JSON / REST Service
<div id="attachment_3278" style="width: 866px" class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/03/example-import-json-rest-api-sql-server-tsql-stored-procedure.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-3278" class="size-full wp-image-3278" src="https://zappysys.com/blog/wp-content/uploads/2018/03/How-to-import-REST-API-in-SQL-Server-1.png" alt="Example - Import JSON Files / REST API Data inside SQL Server ( Call API using T-SQL Stored Procedure)" width="856" height="687" /></a><p id="caption-attachment-3278" class="wp-caption-text">Example &#8211; Import JSON / XMLFiles / REST API Data into SQL Server Table ( Call API using T-SQL Stored Procedure)</p></div></li>
</ol>
<p>&nbsp;</p>
<h2>Video Tutorial &#8211; Read from JSON / XML / REST API in SQL Server (T-SQL code)</h2>
<p>If you want to save time and go quick then check out following video tutorial on calling JSON / REST API Service in SQL Server code. Steps The following video will be useful to configure your Linked Server and query REST API. It shows how to configure ODBC DSN, ZappySys Data Gateway Server (Used for Linked Server). To see other use cases of ZappySys Data Gateway <a href="https://zappysys.com/blog/category/odbc-powerpack/odbc-gateway/" target="_blank" rel="noopener">click here</a></p>
[youtube url=&#8221;https://www.youtube.com/watch?v=fLx6_C39LOQ&#8221;]
<p>&nbsp;</p>
[youtube url=&#8221;https://www.youtube.com/watch?v=M_Pm02ZQT8A&#8221;]
<h2>Requirements</h2>
<ol>
<li>A first requirement will be to SQL Server Database Engine Installed</li>
<li>The second requirement will be SSMS installed</li>
<li>Make sure to have <a href="https://zappysys.com/products/odbc-powerpack/">ZappySys ODBC PowerPack</a> installed.</li>
<li> Another important requirement is the internet connection to connect to REST API information using a URL.</li>
</ol>
<h2>Step-By-Step : Load REST API in SQL Server</h2>
<p>In next few sections, you will see step-by-step approach to consume REST API / JSON data in SQL Server using T-SQL code.</p>
<h3>Configure ZappySys Data Gateway Service</h3>
<p>To call external Drivers (e.g. ZappySys JSON/REST API Driver) in SQL Server we have to use <a href="https://docs.microsoft.com/en-us/sql/relational-databases/linked-servers/linked-servers-database-engine?view=sql-server-2017" target="_blank" rel="noopener">Linked Server feature</a>. We will use <a href="https://zappysys.com/products/odbc-powerpack/data-gateway/" target="_blank" rel="noopener">ZappySys Data Gateway Service</a> along with SQL Linked Server to call ZappySys Drivers (e.g. JSON / XML API Drivers) using T-SQL Code in Stored Procedures.</p>
<div class="content_block" id="custom_post_widget-5282">Now let's look at how to configure <a href="https://zappysys.com/products/odbc-powerpack/data-gateway/" target="_blank" rel="noopener">ZappySys Data Gateway</a>. This feature acts as a bridge between Client App and ZappySys Drivers. Using data gateway you can use ZappySys Drivers inside applications / operating systems where ZappySys drivers may not be available directly for some reason (e.g. You don't have access to Server for Installation or System does not support ODBC drivers like JAVA programs). <a href="https://zappysys.com/blog/category/odbc-powerpack/odbc-gateway/">Click here to read more</a> on various use cases of Data Gateway.
<h4><span style="font-size: 14pt;">Configure Data Gateway User / Port</span></h4>
Now let's look at steps to configure Data Gateway after installation. We will also create a sample data source for ODATA API (i.e. JSON based REST API Service).
<ol>
 	<li>Assuming you have installed <a href="https://zappysys.com/products/odbc-powerpack/" target="_blank" rel="noopener">ZappySys ODBC PowerPack</a> using default options (Which also enables Data Gateway Service)</li>
 	<li>Search "Gateway" in your start menu and click ZappySys Data Gateway
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/11/start-menu-open-zappysys-data-gateway.png">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2018/11/start-menu-open-zappysys-data-gateway.png" alt="Open ZappySys Data Gateway" /></a>
<p class="wp-caption-text">Open ZappySys Data Gateway</p>

</div></li>
 	<li>First make sure Gateway Service is running (Verify Start icon is disabled)</li>
 	<li>Also verify Port on General Tab
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/11/odbc-configure-data-gateway-json-1.png">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2018/11/odbc-configure-data-gateway-json-1.png" alt="Port Number setting on ZappySys Data Gateway" /></a>
<p class="wp-caption-text">Port Number setting on ZappySys Data Gateway</p>

</div></li>
 	<li>Now go to Users tab. <strong>Click Add</strong> icon to add a new user. Check Is admin to give access to all data sources you add in future. If you don't check admin then you have to manually configure user permission for each data source.
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/11/odbc-configure-data-gateway-json-2.png">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2018/11/odbc-configure-data-gateway-json-2.png" alt="Add Data Gateway User" /></a>
<p class="wp-caption-text">Add Data Gateway User</p>

</div></li>
</ol>
&nbsp;
<h4><span style="font-size: 14pt;">Configure Data Source</span></h4>
<ol>
 	<li>After user is added, go to Data Sources tab. <strong>Click Add</strong> icon to create new data source. Select appropriate driver based on your API / File format. You can choose Generic ODBC option to read data from ODBC DSN or use Native Driver option.
<pre class=""><strong>NOTE:</strong> Whenever possible use native driver option for better performance / security and ease of use.</pre>
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/11/odbc-configure-data-gateway-json-3.png">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2018/11/odbc-configure-data-gateway-json-3.png" alt="Add Gateway Data Source (Native JSON Driver)" /></a>
<p class="wp-caption-text">Add Gateway Data Source (Native JSON Driver)</p>

</div></li>
 	<li>Click on "Edit" under Data source and configure as per your need (e.g. Url, Connection, Request Method, Content Type, Body, Pagination etc.). For this demo we are going to pick simple JSON REST API which doesn't need any authentication.  Enter following URL.
<pre class="">https://services.odata.org/V3/Northwind/Northwind.svc/Invoices?$format=json</pre>
</li>
 	<li>You can also view response structure and select default hierarchy (i.e. Filter) like below (Select Array Icon) for data extraction.
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/11/odbc-configure-data-gateway-json-4.png">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2018/11/odbc-configure-data-gateway-json-4.png" alt="Configure JSON API Data source" /></a>
<p class="wp-caption-text">Configure JSON API Data source</p>

</div></li>
</ol>
<h4><span style="font-size: 14pt;">Test SQL Query / Preview Data</span></h4>
<ol>
 	<li>Now go to Preview Tab. You can click Preview button to execute default query
OR
Select Table name from dropdown to generate SQL with column names.
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/11/odbc-configure-data-gateway-json-5.png">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2018/11/odbc-configure-data-gateway-json-5.png" alt="JSON / REST API Driver Query Preview / Query Examples (Read REST API or JSON Files)" /></a>
<p class="wp-caption-text">JSON / REST API Driver Query Preview / Query Examples (Read REST API or JSON Files)</p>

</div></li>
 	<li>You can also click Query Builder to generate SQL using different options in WITH clause. ANy setting you specify in WITH clause will override UI settings we applied in previous steps.
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/11/odbc-configure-data-gateway-json-6.png">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2018/11/odbc-configure-data-gateway-json-6.png" alt="Using SQL Query Builder (For Files or REST / SOAP API - JSON / XML / CSV Format)" /></a>
<p class="wp-caption-text">Using SQL Query Builder (For Files or REST / SOAP API - JSON / XML / CSV Format)</p>

</div></li>
 	<li>There is another useful option for code generation. Select your Language and quickly copy code snippet. See below Example of XML Driver Query to call SOAP API.
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/11/odbc-configure-data-gateway-json-7.png">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2018/11/odbc-configure-data-gateway-json-7.png" alt="Generate Example Code for ZappySys Driver" /></a>
<p class="wp-caption-text">Generate Example Code for ZappySys Driver</p>

</div></li>
 	<li><strong>Click OK</strong> to Close Data Source UI</li>
 	<li>Once data source is tested and configured you can <strong>click Save </strong>button in the Gateway UI toolbar and click <strong>Yes</strong> for <strong>Restart Service</strong>.</li>
</ol>
&nbsp;</div>
<h4>Why use ZappySys Data Gateway Service?</h4>
<p>Gateway Service is useful to call ZappySys driver from any other machine without installing ZappySys driver. For example your Unix server can consume API data by connecting to ZappySys Data Gateway (which can be running on Remote Windows OS). Many times you cannot access remote server but like to Query API data (<a href="https://zappysys.com/api/integration-hub/google-sheets-connector/sql-server?context=connector" target="_blank" rel="noopener">Google SpreadSheet Use case).</a> To learn more about other use cases <a href="https://zappysys.com/blog/category/odbc-powerpack/odbc-gateway/" target="_blank" rel="noopener">click here</a>.</p>
<div class="su-note"  style="border-color:#e5dd9d;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#FFF7B7;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">ZappySys has made API integration in SQL Server extremely easy, fast and secure by using very <strong>innovative approach</strong> for Microsoft SQL Server. Unlike other ODBC drivers available in the market we use very different approach. We use lightweight proxy service called <strong>ZappySys Data Gateway</strong> which runs outside the SQL Server Process / Memory Space. There are many advantages of this approach.</p>
<ul>
<li>Unlike other ODBC drivers, our drivers don&#8217;t affect SQL Server performance because it&#8217;s never loaded in the same SQL Server Process giving you total process isolation.</li>
<li>ZappySys Data Gateway Service acts like a small SQL Server itself. So any machine or any process can send SQL query to read/write data from API services or JSON files as long as communication happens using <a href="https://msdn.microsoft.com/en-us/library/dd304523.aspx" target="_blank" rel="noopener"><strong>TDS Protocol</strong></a>.</li>
<li>You can connect to ZappySys Data Gateway service using any  <a href="https://msdn.microsoft.com/en-us/library/dd304523.aspx" target="_blank" rel="noopener"><strong>TDS Protocol</strong></a> compatible driver (e.g. Microsoft native drivers for SQL Server such as ODBC, JDBC, OLEDB, ADO.net) ).</li>
<li>You can connect from any platform (e.g. <strong>Linux, Mac or Windows</strong>) using standard SQL Server Driver (many times already installed with OS).</li>
<li>You don&#8217;t have to install ZappySys Driver on the client machine with this approach.</li>
</ul>
</div></div>
<h3>Call REST API in SQL Server (T-SQL Code) using Linked Server</h3>
<div class="content_block" id="custom_post_widget-5289">Once you configured data source in Gateway, we can now setup Linked Server in SQL Server to query API data.
<ol style="margin-left: 10px;">
 	<li>Assuming you have installed SQL Server and SSMS. If not then get both for FREE from here: <a href="https://www.microsoft.com/en-us/sql-server/sql-server-editions-express">Get SQL Server Express</a> and  <a href="https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms" target="_blank" rel="noopener">Get SSMS</a></li>
 	<li>Open SSMS and connect to SQL Server.</li>
 	<li>Go to Root &gt; Server Objects &gt; Linked Servers node. Right click and click <strong>New Linked Server...
</strong>
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/03/create-new-linked-server-ssms.png">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2018/03/create-new-linked-server-ssms.png" alt="Add Linked Server in SQL Server" />
</a>
<p class="wp-caption-text">Add Linked Server in SQL Server</p>

</div></li>
 	<li> Now enter linked server name, select Provider as SQL Native Client</li>
 	<li>Enter data source as <strong><span class="lang:default decode:true crayon-inline">GatewayServerName,PORT_NUMBER</span></strong> 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 &gt; General tab incase its different.</li>
 	<li>Enter Catalog Name. This must match name from Data gateway Data sources grid &gt; Name column
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/11/ssms-sql-server-configure-linked-server-2.png">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2018/11/ssms-sql-server-configure-linked-server-2.png" alt="Configure Linked Server Provider, Catalog, Server, Port for ZappySys Data Gateway Connection" />
</a>
<p class="wp-caption-text">Configure Linked Server Provider, Catalog, Server, Port for ZappySys Data Gateway Connection</p>
</div>
<div style="color: #31708f;background-color: #d9edf7;border-color: #bce8f1;padding: 15px;margin-bottom: 20px;border: 1px solid transparent;border-radius: 4px;">
<strong>INFO:</strong><br/>
<ul>
    <li>
      For <strong>SQL Server 2012, 2014, 2016, 2017, and 2019</strong>, use the <em>SQL Server Native Client 11.0</em> as the Provider.
    </li>
    <li>
      For <strong>SQL Server 2022 or higher</strong>, use the <em>Microsoft OLE DB Driver for SQL Server</em> as the Provider.
    </li>
  </ul>
</div></li>
 	<li>Click on Security Tab and select last option "<strong>Be made using this security context</strong>". Enter your gateway user account here.</li>
<li>
        <p>Optional: Under the Server Options Tab, Enable <b>RPC</b> and <b>RPC Out</b> and Disable Promotion of Distributed Transactions<b>(MSDTC)</b>.</p>
		<div class="wp-caption alignnone">
			<img decoding="async" class="block margin-bottom-10 img-thumbnail" src="https://zappysys.com/blog/wp-content/uploads/2018/11/linked-server-options-rpc-msdtc.png" title="RPC and MSDTC Settings" alt="RPC and MSDTC Settings" />
			<p class="wp-caption-text">RPC and MSDTC Settings</p>
		</div>
        <hr />
        <p>
            You need to enable RPC Out if you plan to use <b><i>EXEC(...) AT [MY_LINKED_SERVER_NAME]</i></b> rather than OPENQUERY.
            <br />
            If don't enabled it, you will encounter the <i>'Server "MY_LINKED_SERVER_NAME" is not configured for RPC'</i> error.
        </p>
        <p>
            Query Example:
            <code class="sql">EXEC('Select * from Products') AT [MY_LINKED_SERVER_NAME]</code>
        </p>
        <hr />
        <p>
            If you plan to use <b><i>'INSERT INTO...EXEC(....) AT [MY_LINKED_SERVER_NAME]'</i></b> in that case you need to Disable Promotion of Distributed Transactions(MSDTC).
            <br />
            If don't disabled it, you will encounter the <i>'The operation could not be performed because OLE DB provider "SQLNCLI11/MSOLEDBSQL" for linked server "MY_LINKED_SERVER_NAME" was unable to begin a distributed transaction.'</i> error.
        </p>
        <p>
            Query Example:
<pre class="">Insert Into dbo.Products 
EXEC('Select * from Products') AT [MY_LINKED_SERVER_NAME]</pre>
        </p>
        <hr />
</li>
 	<li>Click OK to save Linked Server</li>
 	<li>In SSMS execute below SQL query to test your connectivity.
<pre class="">SELECT * FROM OPENQUERY( MY_LINKED_SERVER_NAME, 'SELECT * FROM $')</pre>
--OR--
<pre class="">SELECT * FROM OPENQUERY( MY_LINKED_SERVER_NAME, 
'SELECT * FROM $
 WITH (Src=''https://services.odata.org/V3/Northwind/Northwind.svc/Customers?$format=json''
 ,Filter=''$.value[*]''
 ,DataFormat=''OData''
)');</pre>
</li>
 	<li>Here is the preview after you run some REST API query in SQL Server. Notice that you can override default configuration by supplying <a href="https://zappysys.com/onlinehelp/odbc-powerpack/scr/json-odbc-driver-connectionstring.htm" target="_blank" rel="noopener">many parameters</a> in WITH clause (second query example in screenshot).
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/11/query-rest-api-sql-server-linked-server-openquery-zappysys-data-gateway.png">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2018/11/query-rest-api-sql-server-linked-server-openquery-zappysys-data-gateway.png" alt="SSMS Output - Query REST API via Linked Server OPENQUERY statement (Connect to ZappySys Data Gateway)" />
</a>
<p class="wp-caption-text">SSMS Output - Query REST API via Linked Server OPENQUERY statement (Connect to ZappySys Data Gateway)</p>

</div></li>
 	<li>You can wrap your queries inside View or wrap inside Stored procedure to parameterize. Here is an example of create view which calls REST API queries. Below View can be consumed like a normal table from any Tools or Programming Language which supports connectivity to SQL Server.
<pre class="lang:tsql decode:true ">CREATE VIEW dbo.vwApiInvoices 
AS 
/*Call REST API inside SQL Server View*/
SELECT * FROM OPENQUERY( LS , 
'SELECT * FROM $
WITH (Src=''https://services.odata.org/V3/Northwind/Northwind.svc/Invoices?$format=json''
	 ,Filter=''$.value[*]''
	 ,DataFormat=''OData''
)');

GO
</pre>
&nbsp;</li>
 	<li>Notice in above approach if you parameterize Stored Procedure then <a href="https://zappysys.com/blog/create-csv-list-sql-server-table-columns-datatypes/" target="_blank" rel="noopener">check this article to understand Dynamic Metadata</a>.</li>
 	<li>That's it. We are now ready to move forward with more interesting things in next section.</li>
</ol></div>
<p>Optionally, use these lines of code to create the linked server using T-SQL instead of using the UI:</p><pre class="crayon-plain-tag">/* Add Linked Server called RESTAPI-SV (change if needed) to access REST API via Data Gateway - JSON/REST Driver */
/* customer_api is just an example data source name - created on Data gateway UI */

EXEC master.dbo.sp_addlinkedserver 
	@server = N'RESTAPI-SV', 
	@srvproduct=N'', 
--- For MSSQL 2012, 2014, 2016, 2017, and 2019 use below (SQL Server Native Client 11.0)---
    @provider=N'SQLNCLI11',
--- For MSSQL 2022 or higher use below (Microsoft OLE DB Driver for SQL Server)---
--- @provider=N'MSOLEDBSQL',
	@datasrc=N'localhost,5000', 
	@catalog=N'customer_api'

/* Add login for Linked Server - This is same user account and password which you define on Users tab on ZappySys Data Gateway Config Tool */
EXEC master.dbo.sp_addlinkedsrvlogin 
	@rmtsrvname=N'RESTAPI-SV',
	@useself=N'False',
	@locallogin=NULL,
	@rmtuser=N'admin',
	@rmtpassword='adminP@ss!23'
GO
--//OTHER IMPORTANT OPTIONS
EXEC master.dbo.sp_serveroption @server=N'RESTAPI-SV', @optname=N'rpc', @optvalue=N'true'
GO

EXEC master.dbo.sp_serveroption @server=N'RESTAPI-SV', @optname=N'rpc out', @optvalue=N'true'
GO

--Increase default timeout for long running query (default is 600 sec)
--EXEC master.dbo.sp_serveroption @server=N'RESTAPI-SV', @optname=N'query timeout', @optvalue=N'1200'
GO

--Below needed to support EXEC + INSERT (dynamic query)
EXEC master.dbo.sp_serveroption @server=N'RESTAPI-SV', @optname=N'remote proc transaction promotion', @optvalue=N'false'
GO</pre><p>
&nbsp;</p>
<ol>
<li>Now it is time to create some queries. Let&#8217;s run a simple query to REST API:<br />
<pre class="crayon-plain-tag">select * from OPENQUERY([RESTAPI-SV] , 'SELECT * FROM value' )</pre>
</li>
<li>The query will show all the data from REST API:
<div id="attachment_2945" style="width: 684px" class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/03/query-resp-api-results.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-2945" class="size-full wp-image-2945" src="https://zappysys.com/blog/wp-content/uploads/2018/03/query-resp-api-results.png" alt="Linked server query results" width="674" height="214" srcset="https://zappysys.com/blog/wp-content/uploads/2018/03/query-resp-api-results.png 674w, https://zappysys.com/blog/wp-content/uploads/2018/03/query-resp-api-results-300x95.png 300w" sizes="(max-width: 674px) 100vw, 674px" /></a><p id="caption-attachment-2945" class="wp-caption-text">REST API QUERY IN SQL SERVER</p></div>
<p>&nbsp;</li>
<li>Also, the following query can be used when you need to use filters (Notice that we escaped single tick with double tick)</p><pre class="crayon-plain-tag">select * from OPENQUERY([RESTAPI-SV] , 'SELECT * FROM value where ContactTitle=''Owner''' )</pre><p>
</li>
<li>Store the data in a temporary table using the following query:</p><pre class="crayon-plain-tag">select * INTO #tmpcustomer from OPENQUERY([RESTAPI-SV] , 'SELECT * FROM value' )</pre><p>
</li>
<li>And now you can query and use the SQL table:</p><pre class="crayon-plain-tag">SELECT * FROM #tmpcustomer t</pre><p>
</li>
<li>You can also check the metadata, values and odata_next link:</p><pre class="crayon-plain-tag">select * from OPENQUERY([RESTAPI-SV] , 'SELECT * FROM $' )</pre><p>
&nbsp;</li>
</ol>
<h3>Call REST API in SQL Server (T-SQL Code) using Polybase (For SQL 2019 or higher)</h3>
<div class="content_block" id="custom_post_widget-8745"><div style="padding: 0px 0px 10px 0px;">

Once you configured data source in Gateway or in ODBC DSN Data Source, we can now Configure PolyBase to access external data in SQL Server.

</div>
<div style="padding: 0px 0px 10px 0px;">

PolyBase in SQL Server 2019 allows you to connect to ODBC-compatible data sources through the ODBC connector. For more details refer to <a href="https://docs.microsoft.com/en-us/sql/relational-databases/polybase/polybase-configure-odbc-generic?view=sql-server-ver15" target="_blank" rel="noopener">this link</a>.

</div>
<div style="padding: 0px 0px 10px 0px;">

If you haven't installed PolyBase, Please refer to this <a href="https://docs.microsoft.com/en-us/sql/relational-databases/polybase/polybase-installation?view=sql-server-ver15" target="_blank" rel="noopener" data-linktype="relative-path">PolyBase installation instruction</a>.

</div>
<ol>
 	<li>Assuming you have installed SQL Server, SSMS and Polybase. If not then get both SQL Server and SSMS for FREE from here: <a href="https://www.microsoft.com/en-us/sql-server/sql-server-editions-express">Get SQL Server Express</a> and  <a href="https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms" target="_blank" rel="noopener">Get SSMS.</a></li>
 	<li>Open SSMS and connect to SQL Server.</li>
 	<li>After installing Polybase make sure you have an SSIS Catalog created. If you don't, create it in SSMS:
<img loading="lazy" decoding="async" class="alignnone size-full wp-image-8843" src="https://zappysys.com/blog/wp-content/uploads/2020/02/call-rest-api-in-polybase-create-ssis-catalog-0.png" alt="" width="428" height="336" srcset="https://zappysys.com/blog/wp-content/uploads/2020/02/call-rest-api-in-polybase-create-ssis-catalog-0.png 428w, https://zappysys.com/blog/wp-content/uploads/2020/02/call-rest-api-in-polybase-create-ssis-catalog-0-300x236.png 300w" sizes="(max-width: 428px) 100vw, 428px" /></li>
 	<li>Configure the required settings:
<img loading="lazy" decoding="async" class="alignnone wp-image-8844 size-full" src="https://zappysys.com/blog/wp-content/uploads/2020/02/call-rest-api-in-polybase-create-ssis-catalog-e1583329508357.png" alt="" width="515" height="522" srcset="https://zappysys.com/blog/wp-content/uploads/2020/02/call-rest-api-in-polybase-create-ssis-catalog-e1583329508357.png 515w, https://zappysys.com/blog/wp-content/uploads/2020/02/call-rest-api-in-polybase-create-ssis-catalog-e1583329508357-296x300.png 296w" sizes="(max-width: 515px) 100vw, 515px" /></li>
 	<li>Also, make sure the SQL Server is configured to support TCP/IP protocol:
<img loading="lazy" decoding="async" class="alignnone size-full wp-image-8845" src="https://zappysys.com/blog/wp-content/uploads/2020/02/call-rest-api-in-polybase-enable-tcp-ip-in-sql-server.png" alt="" width="644" height="349" srcset="https://zappysys.com/blog/wp-content/uploads/2020/02/call-rest-api-in-polybase-enable-tcp-ip-in-sql-server.png 644w, https://zappysys.com/blog/wp-content/uploads/2020/02/call-rest-api-in-polybase-enable-tcp-ip-in-sql-server-300x163.png 300w" sizes="(max-width: 644px) 100vw, 644px" /></li>
 	<li><strong>(optional)</strong> If in later steps you get a <em>Login</em> error, try connecting to your database using <em><strong>SQL Server Authentication</strong></em> instead of <em>Windows Authentication</em>.</li>
 	<li>Now, in SSMS open the new query window and select the desired database in which you want to call an external ODBC data source.</li>
 	<li>First, we need to create the MASTER KEY in the database where you are trying to create the External Data Source
<pre class="">--1
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'My$tr0ngP@$$w0rd';</pre>
</li>
 	<li>Now we need to create a database scoped credential for accessing the ODBC source in the database.
<pre class="">--2
CREATE DATABASE SCOPED CREDENTIAL SqlServerCredentials WITH IDENTITY = 'username', Secret = 'password';</pre>
</li>
 	<li>Now go to our configured ZS Driver data source which one we created using Gateway or ODBC DSN Data Source and copy the connection string from there.
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/12/copy-connection-string-of-odbc-datasource-for-office-365-e1547488424263.png" target="_blank" rel="noopener">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2018/12/copy-connection-string-of-odbc-datasource-for-office-365-e1547488424263.png" alt="" />
</a>
<p class="wp-caption-text">Copy Connection String from the ZS Driver</p>

</div></li>
 	<li>Now Using that ZS Driver connection string and credentials we need to create the external data source.
<pre class=""><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">EXTERNAL</span> <span class="hljs-keyword">DATA</span> <span class="hljs-keyword">SOURCE</span> external_data_source_name
<span class="hljs-keyword">WITH</span> (LOCATION = odbc://&lt;ODBC <span class="hljs-keyword">server</span> address&gt;[:&lt;port&gt;],
CONNECTION_OPTIONS = <span class="hljs-string">'&lt;zs-driver-conection-string&gt;;
ServerNode = &lt;name of server  address&gt;:&lt;Port&gt;'</span>,
CREDENTIAL = &lt;credential_name&gt; );</pre>
So that our query looks like this, we need to use the same credentials name which one we created.
<pre class="">--3
CREATE EXTERNAL DATA SOURCE [ZappySys_Ext_CSV_Data_source] WITH (LOCATION = N'ODBC://127.0.0.1:5000', CREDENTIAL = [SqlServerCredentials],
CONNECTION_OPTIONS= 'DRIVER={ZappySys CSV Driver};DataPath='E:\zsTemp\Mydata.csv'')
</pre>
Or If you have created the ODBC System DSN Data Source
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2020/02/odbc-system-dsn-zs-csv-driver.png" target="_blank" rel="noopener">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2020/02/odbc-system-dsn-zs-csv-driver.png" alt="" />
</a>
<p class="wp-caption-text">ODBC System DSN : ZappySys CSV Driver</p>

</div>
We can pass the DSN Name also in the query instead of Zs driver connection string, in that case, we need to query like below to create the external data source
<pre class="">--3
CREATE EXTERNAL DATA SOURCE [ZappySys_Ext_CSV_Data_source] WITH (LOCATION = N'ODBC://127.0.0.1:5000', CREDENTIAL = [SqlServerCredentials],
CONNECTION_OPTIONS= 'DSN=zsDSN_MyCSVDriver')
</pre>
</li>
 	<li>Furthermore, we need to create the External Data Table for that external data source.
<pre class="">--4
CREATE EXTERNAL TABLE extTblPersons (
ID bigint NULL,
Name nvarchar(250) NULL
)
WITH (
LOCATION='_root_',
DATA_SOURCE = [ZappySys_Ext_CSV_Data_source] ,
)</pre>
For Location value, we need to pass the table name, from the ZS Driver Preview Tab we can download get the table name.
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2020/02/odbc-preview-tab-table-list.png" target="_blank" rel="noopener">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2020/02/odbc-preview-tab-table-list.png" alt="" />
</a>
<p class="wp-caption-text">ZS Driver Query Preview -Table List</p>

</div></li>
 	<li>That's it now execute the query using that external data table, it will retrieve the data from the external ODBC Data source. So here it will read the data from the Mydata.csv csv file and load the query data in the SQL Server.
<pre class="">--5
Select * From extTblPersons</pre>
<div class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2020/02/sql-server-query-result.png" target="_blank" rel="noopener">
<img decoding="async" src="https://zappysys.com/blog/wp-content/uploads/2020/02/sql-server-query-result.png" alt="" />
</a>
<p class="wp-caption-text">SQL Server Query Result</p>

</div></li>
</ol></div>
<h2>How to use Dynamic SQL with parameters</h2>
<p>So far we saw how to use OPENQUERY statement with a static SQL query. Now let&#8217;s look at the dynamic Query approach to Read REST API data and Load into SQL Table. For Dynamic SQL you have to use EXEC rather than OPENQUERY.</p><pre class="crayon-plain-tag">--MSDTC option must be off for INSERT + EXEC pattern (Find Linked Server Properties | Server Options | Enable Promotion of Distributed Transaction - [Set to false]
--OR-- run via code like below
--EXEC master.dbo.sp_serveroption 'MyLinkedServer', 'remote proc transaction promotion', false;

declare @tbl nvarchar(100) = 'value'
--Just output the query data (no insert)
EXEC('select col1,col2 from ' + @tbl) AT [MyLinkedServer]

--Load into table (dont output)
insert into _tmp_invoices(col1,col2) 
EXEC('select col1,col2 from ' + @tbl) AT [MyLinkedServer]

--We recommend using column names in the list rather than * (all) to avoid metdata issue
--Use Data Source &gt; Preview UI &gt; Table dropdown to generate column names</pre><p>
&nbsp;</p>
<h2>How to parameterize API calls in SQL Server</h2>
<p>Now you know how to write simple SQL queries to fetch data from API. In real world you will have a need to pass parameters to your API. You can pass parameters to your API following ways.  Notice that how we have escaped single tick. In this example we have passed Header, Body and URL using Dynamic approach. In your case you may need to pass only one way (e.g. Pass parameter via URL only)</p><pre class="crayon-plain-tag">declare @url varchar(100)='http://httpbin.org/post' 
declare @body varchar(100)='{id:1,notes:"Line1\r\nLine2"}'
declare @header varchar(100)='Content-Type:text/plain||x-hdr1:AAA'

declare @sql varchar(1000)
declare @sqlForLs  varchar(1000) = 
'SELECT * FROM $
WITH  (
SRC='''+ replace(@url,'''', '''''')  +''' ,
METHOD=''POST'' ,
HEADER='''+ replace(@header,'''', '''''')  +''' ,
BODY='''+ replace(@body,'''', '''''')  +''' 
)'
 
set @sql = 'SELECT * into #tmp from OPENQUERY(LS,''' + replace(@sqlForLs,'''', '''''') + ''')'
print @sql

EXECUTE(@sql);</pre><p>
&nbsp;</p>
<h2>How to Import JSON File to SQL Server (Single or Multiple)</h2>
<p>Not only you can query REST API in SQL Server but also you can query a JSON file. The steps are all the same except in one step. Instead of specifying a URL in the ZappySys ODBC driver (see Add the ZappySys driver in the Windows ODBC Administrator section, step 7) and specify the <strong>file path</strong> and set the format to <strong>Default:</strong></p>
<div id="attachment_2953" style="width: 621px" class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/03/How-to-import-REST-API-in-SQL-Server-2.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-2953" class="size-full wp-image-2953" src="https://zappysys.com/blog/wp-content/uploads/2018/03/How-to-import-REST-API-in-SQL-Server-2.png" alt="JSON file properties" width="611" height="595" /></a><p id="caption-attachment-2953" class="wp-caption-text">Add properties JSON file</p></div>
<p>If you dont want to hard code file name in the DSN then you can also use following Query syntax to specify URL or File Path inside SQL Query</p><pre class="crayon-plain-tag">select * into #tmp from OPENQUERY([JSON-SV], 'SELECT * FROM value WITH (SRC=''C:\test\cust2012.json'')' )</pre><p>
<h2>How to load multiple JSON files into SQL Server Table</h2>
<p>Let&#8217;s say that you have multiple JSON files in a folder (similar structure). The file names are customer_2012.json, customer_2013.json, customer_2014.json and they are all in the same folder. Is it possible to query all of them in SQL Server without any extra looping code?</p>
<p>The answer is yes. You just need to create the linked server to a single JSON file and then run this query (JSON-SV is the name of the linked server):</p><pre class="crayon-plain-tag">select * into #tmp from OPENQUERY([JSON-SV], 'SELECT * FROM value WITH (SRC=''C:\test\cust*.json'')' )</pre><p>
We are just using this code:</p><pre class="crayon-plain-tag">(SRC=''C:\test\cust*.json'')' )</pre><p>
SRC means source and it will search all the files with json extension starting with the word &#8220;cust&#8221;.</p>
<h2>How to read and parse JSON string in T-SQL</h2>
<p>In SQL Server 2016 and later, it is possible to read JSON data. However in older versions, it was not possible. The following example will show how to parse JSON data using a Linked Server named JSON-SV.</p>
<ol>
<li style="list-style-type: none;">
<ol>
<li>We will first store JSON values in a SQL Server table in a column named MyData:<br />
<pre class="crayon-plain-tag">create table #tmp(MyData nvarchar(max))

insert into #tmp values(
'{ 
rows:[ 
{id:1, name:"Sam''s Club"}, 
{id:2, name:"Walmart"}, 
{id:3, name: "Facebook"} 
]
}')</pre>
</li>
<li>We just created a temporary table named tmp and in the column MyData, we stored JSON values. In the next step, we will store the values of the temporary table in a variable named json and store and parse the values en the variable sql:<br />
<pre class="crayon-plain-tag">declare @json nvarchar(max)
declare @sql nvarchar(max)

select @json = MyData from #tmp


set @sql='select * from OPENQUERY([JSON-SV] , ''SELECT * FROM rows WITH
(DATA=@'''''+ Replace(@json ,'''','''''''''') +''''')'' );'
print(@sql)
execute(@sql)</pre>
</li>
<li>If everything is OK, you will be able to see the JSON values parsed in a nice table format:
<div id="attachment_2960" style="width: 200px" class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/03/parse-json-sql-server.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-2960" class="size-full wp-image-2960" src="https://zappysys.com/blog/wp-content/uploads/2018/03/parse-json-sql-server.png" alt="Parse JSON values" width="190" height="111" /></a><p id="caption-attachment-2960" class="wp-caption-text">JSON values parsed</p></div></li>
</ol>
</li>
</ol>
<h2>How to Call REST API from a SQL Stored Procedure</h2>
<p>How can we call REST API using a stored procedure? We need to use dynamic queries to pass parameters. The following example will run a query where the city is specified in a variable.</p>
<ol>
<li>We will create the stored procedure first:<br />
<pre class="crayon-plain-tag">CREATE PROCEDURE getcity 
@City NVARCHAR(50) 
AS 
DECLARE @sql NVARCHAR(500)='select * from OPENQUERY([RESTAPI-SV] , 
''SELECT * FROM value where City=''''' + @City + ''''''')' 
EXECUTE(@sql)</pre>
</li>
<li>The stored procedure name is getcity. We will call the RESTAPI-SV that calls the REST API and pass the parameter City in the query. We finally execute the query stored in the variable using the Execute statement.</li>
<li>Finally, we just invoke the stored procedure:<br />
<pre class="crayon-plain-tag">EXECUTE getcity @City = N'Berlin'</pre>
The query will display all the data with the city equal to Berlin.</li>
</ol>
<h2>How to POST data to REST API in SQL Server (Send data to URL)</h2>
<p>The following example shows how to POST data to API service where data is coming from an embedded string in SQL Server using a Linked Server. I am assuming that you already created a linked server following previews steps explained before.</p>
<h3>POST data to URL (embedded Body)</h3>
<p>In SSMS run this query:</p><pre class="crayon-plain-tag">select * from OPENQUERY([RESTAPI-SV] , 'SELECT * FROM $ 
WITH (
 METHOD=''POST''
,HEADER=''Content-Type: application/json || x-hdr1: AAA''
,SRC=''http://httpbin.org/post''
,BODY=''{ id:2,  notes2:"Line1\r\nLine2" }''
)' 
)</pre><p>
The query will invoke the Linked Server for REST API, created before and use the POST Method with the embedded strings.</p>
<h3>POST data to URL (read body from file)</h3>
<p>Here is an example to POST data to URL where Body is read from Local file. Notice two additional attributes in below.  We set IsMultiPart=true and also Body has @ symbol before file path.</p><pre class="crayon-plain-tag">select * from OPENQUERY([RESTAPI-SV] , 'SELECT * FROM $ 
WITH (
 METHOD=''POST''
,HEADER=''Content-Type: application/json || x-hdr1: AAA''
,SRC=''http://httpbin.org/post''
,BODY=''@c:\data\api_call_body.json''
,IsMultiPart='True'
)' 
)</pre><p>
<h3>Avoiding errors with POST / PUT / DELETE  requests to create / modify /delete data</h3>
<p>There will be a time you may get error when you issue POST requests because same request sent multiple times and duplicate records got created. To avoid such situation <a href="https://zappysys.com/blog/caching-metadata-odbc-drivers-performance/" target="_blank" rel="noopener">refer to this article</a> (Read Handling POST request section)</p>
<h2>Pagination Concepts for REST API / SOAP XML  in SQL Server</h2>
<div class="content_block" id="custom_post_widget-3892"><div style="margin-bottom: 1em;">Even we set up ODBC Data Source to get the data, it may not be enough. Usually, if you are getting a huge data set from API provider, it won't give it to you in one HTTP response. Instead, it gives back only a subset of data and provides a mechanism for data pagination. The good news is that <em>ZappySys ODBC Driver</em> includes many options to cover virtually any pagination method.</div>
<div><span style="font-size: 16px;">Below you will find a few examples of API pagination. If you need something more sophisticated check the below link (the article was written for SSIS PowerPack but UI options and concepts apply to ODBC Driver too):</span></div>
<div style="margin-bottom: 1em;"><a href="https://zappysys.com/blog/ssis-rest-api-looping-until-no-more-pages-found/" target="_blank" rel="noopener">https://zappysys.com/blog/ssis-rest-api-looping-until-no-more-pages-found/</a></div>
<h3>Paginate by Response Attribute</h3>
This example shows how to paginate API calls where you need to paginate until the last page detected. In this example, next page is indicated by some attribute called nextlink (found in response). If this attribute is missing or null then it stops fetching the next page.
<pre class="lang:tsql decode:true codeblock">SELECT * FROM $
WITH(
SRC=@'https://zappysys.com/downloads/files/test/pagination_nextlink_inarray_1.json'
,NextUrlAttributeOrExpr = '$.nextlink'  --keep reading until this attribute is missing. If attribute name contains dot then use brackets like this $.['my.attr.name']
)</pre>
<h3>Paginate by URL Parameter (Loop until certain StatusCode)</h3>
This example shows how to paginate API calls where you need to pass page number via URL. The driver keeps incrementing page number and calls next URL until the last page detected (401 error). There are few ways to indicate the last page (e.g. By status code, By row count, By response size). If you don't specify end detection then it will use the default (i.e. No records found).
<pre class="lang:tsql decode:true codeblock">SELECT * FROM $
WITH (
SRC=@'https://zappysys.com/downloads/files/test/page-xml.aspx?page=1&amp;mode=DetectBasedOnResponseStatusCode'
,PagingMode='ByUrlParameter'
,PagingByUrlAttributeName='page'
,PagingByUrlEndStrategy='DetectBasedOnResponseStatusCode'
,PagingByUrlCheckResponseStatusCode=401
,IncrementBy=1
)</pre>
<h3>Paginate by URL Path (Loop until no record)</h3>
This example shows how to paginate API calls where you need to pass page number via URL Path. The driver keeps incrementing page number and calls next URL until the last page is detected. There are few ways to indicate the last page (e.g. By status code, By row count, By response size). If you don't specify end detection then it will use the default (i.e. No records found).
<pre class="lang:tsql decode:true codeblock">SELECT * FROM $
WITH (
SRC=@'https://zappysys.com/downloads/files/test/cust-&lt;%page%&gt;.xml'
,PagingMode='ByUrlPath'
,PagingByUrlAttributeName='&lt;%page%&gt;'
,PagingByUrlEndStrategy='DetectBasedOnRecordCount'
,IncrementBy=1
)</pre>
<h3>Paginate by Header Link (RFC 5988)</h3>
API like GitHub / Wordpress use Next link in Headers (<a href="https://tools.ietf.org/html/rfc5988" target="_blank" rel="noopener">RFC 5988</a>)
<pre class="lang:default decode:true ">SELECT * FROM $
LIMIT 25
WITH(
	 Src='https://wordpress.org/news/wp-json/wp/v2/categories?per_page=10'
	,PagingMode='ByResponseHeaderRfc5988'
	,WaitTimeMs='200' --//wait 200 ms after each request
)</pre>
&nbsp;</div>
<h2>Error Handling for REST API / SOAP XML in SQL Server</h2>
<div class="content_block" id="custom_post_widget-3894">Sometimes errors occur... they just do and there is nothing you can do! Or can you? Actually, in ODBC PowerPack you can handle them in two ways.
<h3>METHOD 1 - Using Error Handling Options</h3>
<img loading="lazy" decoding="async" class="alignnone size-full wp-image-3949" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-api-error-handling-1.png" alt="" width="668" height="702" />
<h4>When to use?</h4>
You may want to use them when your source is a resource located on the Internet; e.g. a file on a website, a file on an FTP server or just a plain API HTTP response. By default, when a remote server returns an error, data retrieval is stopped, an error is raised and no data is given back to you. This might not be always desirable.
<h4>Scenario 1</h4>
Imagine a scenario, that there is a web server which each day at 12 AM releases a new JSON file with that day's date as filename, e.g. <span style="text-decoration: underline;"><em>http://www.some-server.com/data/2018-06-20.json</em></span>. And, of course, you want to download it and use it daily in your Power BI report. But you have a problem: Power BI report data sources are refreshed each hour and you may get <a href="https://en.wikipedia.org/wiki/HTTP_404" target="_blank" rel="noopener">HTTP 404 status code</a> (no file was found) when a file is not released yet. Which consequentially means other data sources won't be updated as well and you will see old and cached data on the report. That's where you could use <strong><span class="lang:default highlight:0 decode:true crayon-inline">Continue on any error</span></strong> or <strong><span class="lang:default highlight:0 decode:true crayon-inline">Continue when Url is invalid or missing (404 Errors)</span></strong> to avoid an error being raised and let other data sources to be updated.
<h4>Scenario 2</h4>
Another scenario is when you expect a web server to raise some kind of HTTP error when accessing a URL. You don't want ODBC Data Source to raise an error but instead, you want to get response data. That's where you can use <strong><span class="lang:default highlight:0 decode:true crayon-inline">Continue on any error</span></strong> or alike together with  <strong><span class="lang:default highlight:0 decode:true crayon-inline">Get response data on error</span></strong> to continue on an error and get the data:

<img loading="lazy" decoding="async" class="alignnone wp-image-3961 size-full" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-powerpack-get-response-data-on-error.png" alt="" width="547" height="235" srcset="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-powerpack-get-response-data-on-error.png 547w, https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-powerpack-get-response-data-on-error-300x129.png 300w" sizes="(max-width: 547px) 100vw, 547px" />
<h3>METHOD 2 - Using Connection [Retry Settings]</h3>
Another scenario you may run into is a buggy web server. You ask it to give you some file or data and it, like a snotty kid, just doesn't give it to you! You have to ask twice or thrice before it does its job. If that's the case, you have to retry HTTP requests using <em>Connection</em>:

<img loading="lazy" decoding="async" class="alignnone wp-image-3963 size-full" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-api-error-handling-3.png" alt="" width="671" height="572" /></div>
<h2>OAuth / HTTP Connection in SQL Server</h2>
<div class="content_block" id="custom_post_widget-3896"><div style="margin-bottom: 1em;">If you need to authenticate or authorize your user to access a web resource, you will need to use one of the <em>Connections:</em></div>
<ul>
 	<li>HTTP</li>
 	<li>OAuth</li>
</ul>
<img loading="lazy" decoding="async" class="wp-image-4078 size-full" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-powerpack-authentication-authorization-e1529337108252.png" alt="ZappySys XML Driver - HTTP and OAuth Connection Types" width="577" height="302" srcset="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-powerpack-authentication-authorization-e1529337108252.png 577w, https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-powerpack-authentication-authorization-e1529337108252-300x157.png 300w" sizes="(max-width: 577px) 100vw, 577px" />
<h3>HTTP Connection</h3>
<div style="margin-bottom: 1em;">Use <em>HTTP Connection</em> for simple Windows, Basic, NTLM or Kerberos authentication. Just fill in a username and a password and you are good to go!</div>
<div style="margin-bottom: 1em;">You can also use <em>HTTP Connection</em> for more sophisticated authentication like:</div>
<ul>
 	<li><strong>SOAP WSS</strong> (when accessing a SOAP WebService)</li>
 	<li><strong>Static Token / API Key</strong> (when need to pass an API key in HTTP header)</li>
 	<li><strong>Dynamic Token</strong> (same as Static Token method except that each time you need to log in and retrieve a fresh API key)</li>
 	<li><strong>JWT Token</strong> (As per RFC 7519)</li>
</ul>
<img loading="lazy" decoding="async" class="alignnone wp-image-4091 size-full" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-api-connection-type-1.png" alt="" width="622" height="570" />
<h3>OAuth</h3>
If you are trying to access REST API resource, it is a huge chance, you will need to use <em>OAuth Connection</em>. <a href="https://zappysys.com/blog/rest-api-authentication-with-oauth-2-0-using-ssis/" target="_blank" rel="noopener">Read this article</a> to understand how OAuth authentication and authorization works and how to use it (article originally was written for <a href="https://zappysys.com/products/ssis-powerpack/" target="_blank" rel="noopener">SSIS PowerPack</a>, but the concepts and UI stay the same): <br/>
<a href="https://zappysys.com/blog/rest-api-authentication-with-oauth-2-0-using-ssis/" target="_blank" rel="noopener">https://zappysys.com/blog/rest-api-authentication-with-oauth-2-0-using-ssis/</a>
<img loading="lazy" decoding="async" class="alignnone size-full" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-api-connection-type-2.png" width="721" height="708" /></div>
<h2>Performance Tips for REST API / XML SOAP Calls</h2>
<div class="content_block" id="custom_post_widget-4455">While calling APIs you may face some performance issues. There are a few tips you can consider to speed up things.
<h4><span style="font-size: 14pt;"><strong>Use Server-side filtering if possible in URL or Body Parameters</strong></span></h4>
Many API supports filtering your data by URL parameters or via Body. Whenever possible try to use such features.  Here is an example of <a href="http://www.odata.org/getting-started/basic-tutorial/" target="_blank" rel="noopener">odata API</a>, In the below query the first query is faster than the second query because in the first query we filter at the server.
<pre class="lang:tsql decode:true">SELECT * FROM value
WITH(
	 Src='https://services.odata.org/V3/Northwind/Northwind.svc/Customers?$format=json&amp;$filter=Country eq ''USA'''
	,DataFormat='Odata'
)

-- Slow query - Client-side filtering
SELECT * FROM value
WHERE Country ='USA'
WITH(
	 Src='https://services.odata.org/V3/Northwind/Northwind.svc/Customers?$format=json'
	,DataFormat='Odata'
)</pre>
<h4><span style="font-size: 14pt;"><strong>Avoid Special features in SQL Query (e.g. WHERE, Group By, Order By)</strong></span></h4>
ZappySys API engine triggers client-side processing if special features are used in Query. Following SQL Features will trigger Client-Side processing which is several times slower than server-side processing. So always try to use simple query (Select col1, col2 .... from mytable )
<ul>
 	<li>WHERE Clause</li>
 	<li>GROUP BY Clause</li>
 	<li>HAVING Clause</li>
 	<li>ORDER BY</li>
 	<li>FUNCTIONS (e.g. Math, String, DateTime, Regex... )</li>
</ul>
LIMIT clause does not trigger client-side processing.
<h4><span style="font-size: 14pt;"><strong>Consider using pre-generated Metadata / Cache File</strong></span></h4>
Use META option in WITH Clause to use static metadata (Pre-Generated)There are two more options to speedup query processing time. Check <a href="https://zappysys.com/blog/caching-metadata-odbc-drivers-performance/" target="_blank" rel="noopener">this article</a> for details.
<ol>
 	<li>
<pre class="lang:default decode:true">select * from value WITH( meta='c:\temp\meta.txt' )
--OR--
select * from value WITH( meta='my-meta-name' )
--OR--
select * from value WITH( meta='[ {"Name": "col1",&amp;nbsp;"Type": "String", Length: 100},&amp;nbsp;{"Name": "col2",&amp;nbsp;"Type": "Int32"} ...... ]' )</pre>
</li>
 	<li>Enable Data Caching Options (Found on <strong>Property Grid</strong> &gt; <strong>Advanced</strong> Mode Only )</li>
</ol>
<h4><span style="font-size: 14pt;"><strong>Consider using Metadata / Data Caching Option</strong></span></h4>
ZappySys API drivers support Caching Metadata and Data rows to speed up query processing. If your data doesn't change often then you can enable this option to speed up processing significantly.

Check <a href="https://zappysys.com/blog/caching-metadata-odbc-drivers-performance/" target="_blank" rel="noopener">this article</a> for details how to enable Data cache / metadata cache feature for datasource level or query level.

To define cache option at query level you can use like below.
<pre class="">SELECT * FROM $
WITH 
(  SRC='https://myhost.com/some-api'
  ,CachingMode='All'  --cache metadata and data rows both
  ,CacheStorage='File' --or Memory
  ,CacheFileLocation='c:\temp\myquery.cache'
  ,CacheEntryTtl=300 --cache for 300 seconds
)
</pre>
&nbsp;

&nbsp;
<h4><strong><span style="font-size: 14pt;">Use --FAST Option to enable Stream Mode</span></strong></h4>
ZappySys JSON / XML drivers support <strong>--FAST</strong> suffix for Filter. By using this suffix after Filter driver enables Stream Mode, <a href="https://zappysys.com/blog/caching-metadata-odbc-drivers-performance/#Reading_Large_Files_Streaming_Mode_for_XML_JSON" target="_blank" rel="noopener">Read this article</a> to understand how this works.
<pre class="lang:default decode:true">SELECT * FROM $ 
LIMIT 10 --//add this just to test how fast you can get 10 rows
WITH(
  Filter='$.LargeArray[*]--FAST' --//Adding --FAST option turn on STREAM mode (large files)
 ,SRC='https://zappysys.com/downloads/files/test/large_file_100k_largearray_prop.json.gz'
 --,SRC='c:\data\large_file.json.gz'
 ,IncludeParentColumns='False'  --//This Must be OFF for STREAM mode (read very large files)
 ,FileCompressionType='GZip' --Zip or None (Zip format only available for Local files)
)</pre>
&nbsp;</div>
<h2>Other Considerations for Calling Web API in SQL Server</h2>
<div class="content_block" id="custom_post_widget-3901">There are few settings you can coder while calling Web API
<h3><strong>API Limit / Throttling</strong></h3>
While calling public API or other external web services one important aspect you have to check,  how many requests are allowed by your API. Especially when you use API pagination options to pull many records you have to slow down based on API limits. For example, your API may allow you only 5 requests per second. Use Throttling Tab on Driver UI to set delay after each request.
<h3><strong>2D Array Transformation</strong></h3>
If you are using JSON or XML API Driver then possible you may have to transform your data using 2D array transformation feature. <a href="https://zappysys.com/blog/parse-multi-dimensional-json-array-ssis/" target="_blank" rel="noopener">Check this link</a> for more information.

&nbsp;</div>
<h2>Calling XML SOAP Web Service in SQL Server</h2>
<p>So far we have looked at examples to consume data using JSON driver. Now let&#8217;s look at an example, to call XML SOAP Web Service.</p>
<div class="content_block" id="custom_post_widget-3870">To call SOAP API you need to know Request XML Body Structure. If you are not sure how to create SOAP Request body then no worries. <a href="https://zappysys.com/blog/calling-soap-web-service-in-ssis-xml-source/" target="_blank" rel="noopener">Check this article</a> to learn how to generate SOAP Request body using the Free tool <a href="https://www.soapui.org/downloads/latest-release.html" target="_blank" rel="noopener">SoapUI</a>. Basically, you have to use SoapUI to generate Request XML and after that, you can replace parameters as needed in the generated body.
<h3>What is SOAP Web Service?</h3>
If you are new to SOAP Web Service sometimes referred as XML Web Service then please read some concept about SOAP Web service standard <a href="https://msdn.microsoft.com/en-us/library/ms996507.aspx?f=255&amp;MSPPError=-2147217396" target="_blank" rel="noopener">from this link</a>

There are two important aspects in SOAP Web service.
<ol>
 	<li>Getting WSDL file or URL</li>
 	<li>Knowing exact Web Service URL</li>
</ol>
<h3>What is WSDL</h3>
In very simple term WSDL (often pronounced as whiz-dull) is nothing but a document which describes Service metadata (e.g. Functions you can call, Request parameters, response structure etc). Some service simply give you WSDL as xml file you can download on local machine and then analyze or sometimes you may get direct URL (e.g. http://api.mycompany.com/hr-soap-service/?wsdl )
<h3>Example SQL Query for SOAP API call using ZappySys XML Driver</h3>
Here is an example SQL query you can write to call SOAP API. If you not sure about many details then check next few sections on how to use XML Driver User Interface to build desired SQL query to POST data to XML SOAP Web Service without any coding.
<pre class="lang:tsql decode:true">SELECT * FROM $
WITH(
	 Src='http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx'
	,DataConnectionType='HTTP'
	,CredentialType='Basic' --OR SoapWss
	,SoapWssPasswordType='PasswordText'
	,UserName='myuser'
	,Password='pass$$w123'
	,Filter='$.soap:Envelope.soap:Body.GetHolidaysAvailableResponse.GetHolidaysAvailableResult.HolidayCode[*]'
	,ElementsToTreatAsArray='HolidayCode'	
	,RequestMethod='POST'	
	,Header='Content-Type: text/xml;charset=UTF-8 || SOAPAction: "http://www.holidaywebservice.com/HolidayService_v2/GetHolidaysAvailable"'
	,RequestData='
&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hol="http://www.holidaywebservice.com/HolidayService_v2/"&gt;
   &lt;soapenv:Header/&gt;
   &lt;soapenv:Body&gt;
      &lt;hol:GetHolidaysAvailable&gt;
         &lt;!--type: Country - enumeration: [Canada,GreatBritain,IrelandNorthern,IrelandRepublicOf,Scotland,UnitedStates]--&gt;
         &lt;hol:countryCode&gt;UnitedStates&lt;/hol:countryCode&gt;
      &lt;/hol:GetHolidaysAvailable&gt;
   &lt;/soapenv:Body&gt;
&lt;/soapenv:Envelope&gt;'
)</pre>
Now let's look at steps to create SQL query to call SOAP API. Later we will see how to generate code for your desired programming language (e.g. C# or SQL Server)
<h3>Video Tutorial - Introduction to SOAP Web Service and SoapUI tool</h3>
Before we dive into details about calling SOAP API using ZappySys XML Driver, lets first understand what is SOAP API and how to create SOAP requests using SoapUI tool. You will learn more about this process in the later section. The video contains some fragment about using SOAP API in SSIS but just ignore that part because we will be calling Soap API using ZappySys ODBC Driver rather than SSIS Components.

&nbsp;

<iframe loading="lazy" width="560" height="315" src="https://www.youtube.com/embed/d_x5bgGjg0Y?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen="allowfullscreen" data-mce-fragment="1"></iframe>
<h3>Using SoapUI to test SOAP API call / Create Request Body XML</h3>
Assuming you have downloaded and installed <a href="https://www.soapui.org/downloads/latest-release.html" target="_blank" rel="noopener">SoapUI from here</a>, now we are ready to use WSDL for your SOAP Web Service Calls. If you do not have WSDL file or URL handy then contact your API provider (sometimes you just have to add <strong>?wsdl </strong>at the end of your Service URL to get WSDL so try that. Example: http://mycompany/myservice?wsdl ).

If you don't know what is WSDL then in short, WSDL is <strong>Web service Description Language</strong> (i.e. XML file which describes your SOAP Service). WSDL helps to craft SOAP API request Body for ZappySys XML Driver. So Let's get started.
<ol>
 	<li>Open SoapUI and click SOAP button to create new SOAP Project</li>
 	<li>Enter WSDL URL or File Path of WSDLFor example WSDL for our sample service can be accessed via this URL
<pre class="lang:default highlight:0 decode:true">http://www.dneonline.com/calculator.asmx?wsdl</pre>
<a href="https://zappysys.com/blog/wp-content/uploads/2018/06/calling-soap-api-import-wsdl-new-soapui-project.png"><img loading="lazy" decoding="async" class="size-full wp-image-3871" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-call-soap-api-14.png" alt="Create new SOAP API Project in SoapUI tool for SOAP API Testing" width="486" height="349" /></a>
<div style="margin-bottom: 1em;">Create new SOAP API Project in SoapUI tool for SOAP API Testing</div></li>
 	<li>Once WSDL is loaded you will see possible operations you can call for your SOAP Web Service.</li>
 	<li>If your web service requires credentials then you have to configure it. There are two common credential types for public services (<strong>SOAP WSS</strong> or <strong>BASIC</strong> )
<ol>
 	<li>
<div style="margin-bottom: 1em;">To use <strong>SOAP WSS Credentials</strong> select request node and enter UserId, Password, and <strong>WSS-PasswordType</strong> (PasswordText or PasswordHash)</div>
<a href="https://zappysys.com/blog/wp-content/uploads/2018/06/calling-soap-api-pass-soap-wss-credentials-userid-password.png"><img loading="lazy" decoding="async" class="size-full wp-image-3872 alignnone" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-call-soap-api-2.png" alt="Configure SOAP WSS Credentials for SoapUI (SOAP API Testing Tool)" width="294" height="544" /></a>
<div style="display: block;">Configure SOAP WSS Credentials for SoapUI (SOAP API Testing Tool)</div></li>
 	<li>To use <strong>BASIC Auth</strong> Credentials select request node and double-click it. At the bottom click on Auth (Basic) and From Authorization dropdown click Add New and Select Basic.<a href="https://zappysys.com/blog/wp-content/uploads/2018/06/calling-soap-api-pass-basic-authentication-userid-password.png"><img loading="lazy" decoding="async" class="size-full wp-image-3873" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-call-soap-api-2.png" alt="Configure Basic Authorization for SoapUI (SOAP API Testing Tool)" width="616" height="653" /></a>
<div style="margin-bottom: 1em;">Configure Basic Authorization for SoapUI (SOAP API Testing Tool)</div></li>
</ol>
</li>
 	<li>Now you can test your request first Double-click on the request node to open request editor.</li>
 	<li>Change necessary parameters, remove optional or unwanted parameters. If you want to regenerate request you can click on <strong>Recreate default request toolbar icon</strong>.
<a href="https://zappysys.com/blog/wp-content/uploads/2016/06/create-soap-request-with-optional-parameters-soapui.png"><img loading="lazy" decoding="async" class="size-full wp-image-2812" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-call-soap-api-4.png" alt="Create SOAP Request XML (With Optional Parameters)" width="807" height="315" /></a>
<div style="margin-bottom: 1em;">Create SOAP Request XML (With Optional Parameters)</div></li>
 	<li>Once your SOAP Request XML is ready, <strong>Click the Play button</strong> in the toolbar to execute SOAP API Request and Response will appear in Right side panel.
<a href="https://zappysys.com/blog/wp-content/uploads/2018/06/soapui-test-soap-api-request-response-edit-xml-body.png"><img loading="lazy" decoding="async" class="size-full wp-image-3874" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-call-soap-api-5.png" alt="Test SOAP API using SoapUI Tool (Change Default XML Body / Parameters, Execute and See Response)" width="1216" height="511" /></a>
Test SOAP API using SoapUI Tool (Change Default XML Body / Parameters, Execute and See Response)</li>
</ol>
<h3>Create DSN using ZappySys XML Driver to call SOAP API</h3>
Once you have tested your SOAP API in SoapUI tool, we are ready to use ZappySys XML driver to call SOAP API in your preferred BI tool or Programming language.
<ol>
 	<li>First open <strong>ODBC Data Sources</strong> (search ODBC in your start menu or go under ZappySys &gt; ODBC PowerPack &gt; <strong>ODBC 64 bit</strong>)</li>
 	<li>Goto <strong>System DSN</strong> Tab (or User DSN which is not used by Service account)</li>
 	<li>Click <strong>Add</strong> and Select ZappySys XML Driver
<a href="https://zappysys.com/blog/wp-content/uploads/2018/06/zappysys-odbc-xml-soap-api-driver.png"><img loading="lazy" decoding="async" class="size-full wp-image-3875" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-call-soap-api-6.png" alt="ZappySys ODBC Driver for XML / SOAP API" width="593" height="459" /></a>
ZappySys ODBC Driver for XML / SOAP API</li>
 	<li>Configure API URL, Request Method and Request Body as below
<a href="https://zappysys.com/blog/wp-content/uploads/2018/06/calling-soap-web-service-zappysys-xml-driver.png"><img loading="lazy" decoding="async" class="size-full wp-image-3876" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-call-soap-api-7.png" alt="ZappySys XML Driver - Calling SOAP API - Configure URL, Method, Body" width="916" height="874" /></a>
ZappySys XML Driver - Calling SOAP API - Configure URL, Method, Body</li>
 	<li><strong>(This step is Optional)</strong> If your SOAP API requires credentials then Select Connection Type to HTTP and configure as below.
<a href="https://zappysys.com/blog/wp-content/uploads/2018/06/soap-api-call-credential-basic-soap-wss-zappysys-xml-driver.png"><img loading="lazy" decoding="async" class="size-full wp-image-3877" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-call-soap-api-8.png" alt="ZappySys XML Driver - Configure SOAP WSS Credentials or Basic Authorization (Userid, Password)" width="564" height="483" /></a>
<div style="display: block;">ZappySys XML Driver - Configure SOAP WSS Credentials or Basic Authorization (Userid, Password)</div></li>
 	<li>Configure-Request Headers as below (You can get it from Request &gt; Raw tab from SoapUI after you test the request by clicking the Play button)
<a href="https://zappysys.com/blog/wp-content/uploads/2018/06/set-soap-api-request-headers-zappysys-xml-driver.png"><img loading="lazy" decoding="async" class="size-full wp-image-3881" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-call-soap-api-9.png" alt="Configure SOAP API Request Headers - ZappySys XML Driver" width="1009" height="747" /></a>
Configure SOAP API Request Headers - ZappySys XML Driver</li>
 	<li>Once credentials entered you can select Filter to extract data from the desired node. Make sure to select array node (see special icon) or select the node which contains all necessary columns if you don't have array node.
<a href="https://zappysys.com/blog/wp-content/uploads/2018/06/soap-api-query-select-filter-zappysys-xml-driver.png"><img loading="lazy" decoding="async" class="size-full wp-image-3882" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-call-soap-api-10.png" alt="Select Filter - Extract data from nested XML / SOAP API Response (Denormalize Hierarchy)" width="809" height="594" /></a>
Select Filter - Extract data from nested XML / SOAP API Response (Denormalize Hierarchy)</li>
 	<li>If prompted select yes to treat selected node as Array (This is helpful when you expect one or more record for selected node)
<a href="https://zappysys.com/blog/wp-content/uploads/2018/06/xml-api-array-handling-zappysys-xml-driver.png"><img loading="lazy" decoding="async" class="size-full wp-image-3883" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-call-soap-api-11.png" alt="Treat selected node as XML Array Option for SOAP API Response XML" width="655" height="572" /></a>
Treat selected node as XML Array Option for SOAP API Response XML</li>
</ol>
<h3>Preview SOAP API Response / Generate SQL Code for SOAP API Call</h3>
Once you configure settings for XML Driver now you can preview data or generate example code for desired language (e.g. C#, Python, Java, SQL Server).

Go to Preview tab and you will see default query generated based on settings you entered in previous sections. Attributes listed in WITH clause are optional. If you omit attribute in WITH clause it will use it from Properties tab.
<h3>Preview Data</h3>
<a href="https://zappysys.com/blog/wp-content/uploads/2018/06/calling-soap-web-service-zappysys-xml-api-driver.png"><img loading="lazy" decoding="async" class="size-full wp-image-3884" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-call-soap-api-12.png" alt="Preview SOAP API Response in ZappySys XML Driver" width="808" height="780" /></a>
Preview SOAP API Response in ZappySys XML Driver
<h3>Generate Code Option</h3>
<a href="https://zappysys.com/blog/wp-content/uploads/2018/06/zappysys-driver-code-generator.png"><img loading="lazy" decoding="async" class="size-full wp-image-3885" src="https://zappysys.com/blog/wp-content/uploads/2018/06/odbc-call-soap-api-13.png" alt="Generate Example Code for ZappySys Driver" width="572" height="618" /></a>
<div style="display: block;">Generate Example Code for ZappySys Driver</div></div>
<h2>OPENQUERY vs EXEC AT (Handling Larger SQL Text)</h2>
<p>So far we have seen examples of using OPENQUERY. It allows us to send pass-through query at remote server. The biggest limitation of OPENQUERY is it doesnt allow you to use variables inside SQL so often we have to use unpleasant looking dynamic SQL (Lots of tick, tick &#8230;.  and escape  hell). Well there is good news. With SQL 2005 and later you can use <pre class="crayon-plain-tag">EXEC( your_sql ) AT your_linked_server syntax</pre> .</p>
<p>Disadvantage of EXEC AT is you cannot do SELECT INTO like OPENQUERY. Also you cannot perform JOIN like below in EXEC AT</p><pre class="crayon-plain-tag">SELECT a.* FROM OPENQUERY(ls_json,'select * from $') a 
JOIN  OPENQUERY(ls_json,'select * from $') b ON a.id=b.id</pre><p>
However you can always do INSERT INTO MyTable EXEC(&#8230;) AT LINKEDSRV. So table must exists when you do that way.</p>
<p>Here is how to use it. To use EXEC AT you must turn on <strong>RPC OUT</strong> option. Notice how we used variable in SQL to make it dynamic. This is much cleaner than previous approach we saw.</p><pre class="crayon-plain-tag">USE [master]
GO

EXEC master.dbo.sp_addlinkedserver @server = N'ls_Json', @srvproduct=N'', @provider=N'SQLNCLI', @datasrc=N'localhost,5000', @provstr=N'Network Library=DBMSSOCN;', @catalog=N'JsonApi'
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'ls_Json',@useself=N'False',@locallogin=NULL,@rmtuser=N'tdsuser',@rmtpassword='########'
GO

EXEC sp_serveroption 'ls_Json', 'rpc out', true;  
go

declare @tbl varchar(100)='$'
EXEC('select * from ' + @tbl ) AT ls_Json</pre><p>
Here is the difference between OPENQUERY vs EXEC approaches.</p>
<div id="attachment_7968" style="width: 721px" class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/03/openquery-vs-exec-sql-server-linked-server-text-limit.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-7968" class="size-full wp-image-7968" src="https://zappysys.com/blog/wp-content/uploads/2018/03/openquery-vs-exec-sql-server-linked-server-text-limit.png" alt="SQL Server OPENQUERY vs EXEC for Linked Server - Handling Larger SQL Text (More than 8000 chars)" width="711" height="485" srcset="https://zappysys.com/blog/wp-content/uploads/2018/03/openquery-vs-exec-sql-server-linked-server-text-limit.png 711w, https://zappysys.com/blog/wp-content/uploads/2018/03/openquery-vs-exec-sql-server-linked-server-text-limit-300x205.png 300w" sizes="(max-width: 711px) 100vw, 711px" /></a><p id="caption-attachment-7968" class="wp-caption-text">SQL Server OPENQUERY vs EXEC for Linked Server &#8211; Handling Larger SQL Text (More than 8000 chars)</p></div>
<p>&nbsp;</p>
<p>If you decide to use EXEC instead of OPENQUERY then make sure below setting is ON else you will get an error.</p>
<p>Under the Linked Server Options, Enable <b>RPC</b> and <b>RPC Out</b> and Disable Promotion of Distributed Transactions<b>(MSDTC)</b>.</p>
<div id="attachment_10949" style="width: 700px" class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/03/linked-server-options-rpc-msdtc.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-10949" class="size-full wp-image-10949" src="https://zappysys.com/blog/wp-content/uploads/2018/03/linked-server-options-rpc-msdtc.png" alt="linked-server-options-rpc-msdtc" width="690" height="625" srcset="https://zappysys.com/blog/wp-content/uploads/2018/03/linked-server-options-rpc-msdtc.png 690w, https://zappysys.com/blog/wp-content/uploads/2018/03/linked-server-options-rpc-msdtc-300x272.png 300w" sizes="(max-width: 690px) 100vw, 690px" /></a><p id="caption-attachment-10949" class="wp-caption-text">RPC OUT setting for EXEC AT statement in SQL Server Linked Server</p></div>
<p>&nbsp;</p>
<h2>Configure Firewall Settings</h2>
<p>So far we have assumed that Gateway is running on the same machine as SQL Server. However there will be a case when ZappySys ODBC PowerPack is installed on a different machine than SQL Server.  In such case you may have to perform additional Firewall configurations. On most computers firewall settings wont allow outside traffic to ZappySys Data Gateway. In such case perform following steps to allow other machines to connect to Gateway.</p>
<ol>
<li>Search for Windows Firewall Advanced Security in start menu.</li>
<li>Under Inbound Rules &gt; Right click and click [New Rule] &gt;&gt; Click Next</li>
<li>Select Port on Rule Type &gt;&gt; Click Next</li>
<li>Click on TCP and enter port number under specified local port as 5000 (use different one if you changed Default port) &gt;&gt; Click Next</li>
<li>Select Profile (i.e. Private, Public) &gt;&gt; Click Next</li>
<li>Enter Rule name [i.e. ZappySys Data Gateway &#8211; Allow Inbound ] &gt;&gt; Click Next</li>
<li>Click OK to save the rule</li>
</ol>
<div id="attachment_7250" style="width: 826px" class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2018/03/zappysys-data-gateway-firewall-rule-allow-traffic.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-7250" class="size-full wp-image-7250" src="https://zappysys.com/blog/wp-content/uploads/2018/03/zappysys-data-gateway-firewall-rule-allow-traffic.png" alt="ZappySys Datagateway - Allow Inbound Traffic on Port 5000 Firewall Rule" width="816" height="319" srcset="https://zappysys.com/blog/wp-content/uploads/2018/03/zappysys-data-gateway-firewall-rule-allow-traffic.png 816w, https://zappysys.com/blog/wp-content/uploads/2018/03/zappysys-data-gateway-firewall-rule-allow-traffic-300x117.png 300w, https://zappysys.com/blog/wp-content/uploads/2018/03/zappysys-data-gateway-firewall-rule-allow-traffic-768x300.png 768w" sizes="(max-width: 816px) 100vw, 816px" /></a><p id="caption-attachment-7250" class="wp-caption-text">ZappySys Datagateway &#8211; Allow Inbound Traffic on Port 5000 Firewall Rule</p></div>
<h2>Known Issues</h2>
<p>In this section we will discuss some known issues when using OPENQUERY / Data Gateway Connectivity</p>
<h3>Error: The data is invalid</h3>
<p>There will be a time when you start facing odd errors such as below. Possible</p><pre class="crayon-plain-tag">OLE DB provider "SQLNCLI11/MSOLEDBSQL" for linked server "Zs_Csv" returned message "Deferred prepare could not be completed.".
OLE DB provider "SQLNCLI11/MSOLEDBSQL" for linked server "Zs_Csv" returned message "Communication link failure".
Msg 13, Level 16, State 1, Line 0

Session Provider: The data is invalid.</pre><p>
<strong>Possible Cause:</strong></p>
<p>There are few reasons for such error but below are two main reasons</p>
<ul>
<li>If query length is more than 2000 Characters long such as below then you might get such error<br />
<pre class="crayon-plain-tag">SELECT * FROM OPENQUERY(LS, '--some really long text more than 2000 chars--')</pre>
</li>
<li>If query contains multiple OPENQUERY statements for JOIN / UNION like below then also it might fail (Gateway doesnt support parallel queries on a single connection &#8211; MARS compatibility issue).<br />
<pre class="crayon-plain-tag">select a.id, b.name from OPENQUERY(LS, 'select * from tbl1') a join OPENQUERY(LS, 'select * from tbl2') b on a.id=b.id</pre>
</li>
</ul>
<p><strong>Possible Fix:</strong></p>
<p>There are few ways to fix above error based on reason why you getting this error (i.e. Query Length issue OR JOIN/UNION in the same statement)</p>
<ul>
<li>If your query has long SQL (more than 2000 chars ) then reduce SQL length using different techniques
<ul>
<li>e.g. use SELECT * FROM MyTable rather than SELECT col1,col2&#8230; FROM MyTable</li>
<li>Use Meta Option in WITH clause if you must use column name. (e.g. SELECT * FROM MyTable WITH(META=&#8217;c:\meta.txt&#8217;) this way you can define column in Meta file rather than SELECT query. <a href="https://zappysys.com/blog/caching-metadata-odbc-drivers-performance/" target="_blank" rel="noopener">Check this article</a></li>
<li>Consider using EXECT (&#8230;.) AT [Linked_Server_name] option rather than OPENQUERY so you can use very long SQL (See next section on EXEC..AT usecase)</li>
<li>Consider <a href="https://zappysys.com/blog/odbc-drivers-custom-objects-feature/" target="_blank" rel="noopener">using Virtual Table / Stored Proc</a> to wrap long SQL so your call is very small (where usp_GetOrdersByYear is custom proc created on ZappySys Driver UI)<br />
<pre class="crayon-plain-tag">SELECT * FROM OPENQUERY(LS, 'EXEC usp_GetOrdersByYear 2021')</pre>
</li>
</ul>
</li>
<li>If your query uses JOIN  / UNION with multiple OPENQUERY in same SQL then use multiple Linked servers (one for each OPENQUERY clause) as below.<br />
<pre class="crayon-plain-tag">select a.id, b.name from OPENQUERY(LS_1, 'select * from tbl1') a join OPENQUERY(LS_2, 'select * from tbl2') b on a.id=b.id</pre>
</li>
</ul>
<h3>Error: Unable to begin a distributed transaction (When INSERT + EXEC used)</h3>
<p>If you try to use EXEC statement and try to insert data into table (like below) you might get below error unless MSDTC option is turned off.</p><pre class="crayon-plain-tag">INSERT INTO MyTable EXEC('select * from tbl') AT MyLinkedServer</pre><p>
</p><pre class="crayon-plain-tag">"Protocol error in TDS stream"
The operation could not be performed because OLE DB provider "SQLNCLI11" for linked server "ls_Json2" was unable to begin a distributed transaction.
--OR--
The operation could not be performed because OLE DB provider "MSOLEDBSQL" for linked server "ls_Json" was unable to begin a distributed transaction.</pre><p>
<b>Solution:</b><br />
<strong>Method-1:</strong> Go to linked server properties | Server Options | <strong>Enable Promotion of Distributed Transaction</strong> | Change to false (Default is true)<br />
Now your try your INSERT with EXEC AT and it should work</p>
<p><strong>Method-2: </strong>Run the below command if you dont want to use UI</p><pre class="crayon-plain-tag">EXEC master.dbo.sp_serveroption @server=N'My_Linked_Server', @optname=N'remote proc transaction promotion', @optvalue=N'false'</pre><p>
<h3>Error: Cannot use OPENQUERY with JOIN / UNION</h3>
<p>When you perform JOIN  / UNION ALL on the same Linked Server it may fail to process sometimes because Datagateway doesnt support parallel query requests on the same connection. Workaround for that would be create multiple linked servers for the same datasource. See above section for the same workaround.</p>
<h3>Error: Truncation errors due to data length mismatch</h3>
<p>Many time you may receive truncation errors if Table column is length is less than actual column size from query column. To solve this issue use New version of Data gateway ( check &#8211; Use nvarchar(max) for string options &#8211; Found on General Tab)</p>
<h2>Performance Tips</h2>
<p>Now lets look at few performance tips</p>
<h3>Use INSERT INTO rather than SELECT INTO to avoid extra META request</h3>
<p>We discussed some Pros and Cons of <strong>OPENQUERY</strong> vs <strong>EXEC (&#8230;) AT</strong> in previous section. One obvious advantage of EXEC (&#8230;.) AT is it reduces number of requests to driver (It sends pass through query). With EXEC you cannot load data dynamically like SELECT INTO tmp FROM OPENQUERY. Table must exist before hand if you use EXEC.</p><pre class="crayon-plain-tag">INSERT INTO tmp_API_Report_Load(col1,col2)
EXEC('select col1,col2 from some_api_table') AT [API-LINKED-SERVER]
--OR--
INSERT INTO tmp_API_Report_Load(col1,col2)
select col1,col2 from OPENQUERY([API-LINKED-SERVER], 'select col1,col2 from some_api_table')</pre><p>
Advantage of this method is your Query speed will increase because system calls API only once when you call EXEC AT. In OPENROWSET it needs to call above query twice (Once to obtain metadata and once to get data).</p>
<h3>Use Cached Metadata if possible</h3>
<p>By default, most of the SQL sent to Data gateway need to invoke two phases. First, get metadata and second fetch data. You can bypass meta API call by supplying static metadata. Use META property in WITH clause as per <a href="https://zappysys.com/blog/caching-metadata-odbc-drivers-performance/" target="_blank" rel="noopener">this article</a> to speed up your SQL.</p>
<p>&nbsp;</p>
<h2>Conclusion</h2>
<p>To conclude, we can say that using ZappySys JSON / XML Drivers we can query JSON / XML Files and call REST API or SOAP Web Service using familiar SQL Language. All you need to do is install ZappySys ODBC Drivers, configure it in the ODBC DSN and use ZappySys Gateway as a Proxy. Finally, you add a Linked Server in SQL Server to connect to ZappySys Data Gateway. Feel free to download  <a href="https://zappysys.com/products/odbc-powerpack/">ZappySys Drivers here</a>. You can contact ZappySys Team <a href="https://zappysys.com/support/" target="_blank" rel="noopener">here</a> if you need any API integration help.</p>
<h2>References</h2>
<ul>
<li><a href="https://zappysys.com/onlinehelp/odbc-powerpack/">ZappySys ODBC PowerPack online help</a></li>
<li><a href="https://docs.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server">JSON data in SQL Server</a></li>
<li><a href="https://docs.microsoft.com/en-us/sql/relational-databases/linked-servers/linked-servers-database-engine">Linked Servers (Database Engine)</a></li>
</ul>
<p>Keywords: Call REST API, SQL SERVER, JSON</p>
<p>The post <a href="https://zappysys.com/blog/import-rest-api-json-sql-server/">How to import REST API in SQL Server (Call JSON / XML SOAP Service)</a> appeared first on <a href="https://zappysys.com/blog">ZappySys Blog</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
