How to create a SQL CLR stored procedure to get data from a web service and insert results into a SQL Server table

前提是你 提交于 2019-12-08 22:11:34

SQLCLR should be ok in this scenario, as long as you are careful and aware of the various nuances of working in the highly restricted environment that is SQL Server's CLR host (i.e. SQLCLR), namely:

  1. You cannot use native web service code. Instead, you need to use HttpWebRequest and HttpWebResponse. And, that means that you need to generate the request XML by hand, and then parse the XML response yourself (as opposed to getting back a .NET object).
  2. In order to avoid the performance bottle neck that most people refer to when dealing with networking calls, you need to increase the URI Connection Limit via ServicePointManager for the URI you are connecting to. The default limit is 2 and any calls above that will wait until one of those 2 connections completes. I am working on a blog post that will explain this in detail, with examples, and I will post the link here once it is published.
  3. The Assembly will need to be set to EXTERNAL_ACCESS. In order to do that, do not set the Database to TRUSTWORTHY ON. Instead, sign the Assembly, create an Asymmetric Key in master from that Assembly, create a Login from that Asymmetric Key, and finally grant that Login the EXTERNAL ACCESS ASSEMBLY permission.

For more information on working with SQLCLR in general, please see the series I am writing on this topic on SQL Server Central (free registration is required to read their content): Stairway to SQLCLR.

Also, while not a free option, if you want the ability to make these web calls without having to deal with the coding, figuring out best practices, scripting, etc, then take a look at the Full version of SQL# (which I wrote). The Full version has INET_GetWebPages which allows for making web requests. And, it has INET_GetConnectionLimitForURI, INET_GetCurrentConnectionCountForURI, and INET_SetConnectionLimitForURI which allow you to manage that URI connection limit and reduce/avoid that performance bottleneck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!