I would like to know how to use a web service from a SQL Server stored procedure. I have a table in which the columns specify the city and state of the user. I want to pass this address as a parameter to Google Geocoding API web service and get the latitude and longitude.
I have successfully made use of this geocoding api in c#. But now, I want to use it in a stored procedure.
Can any one please suggest how to get this done? Or please provide me any links?
Any help will be appreciated!!
Thanks.
Omar Frometa has an example on how to do this. go this link http://www.codeproject.com/Articles/428200/How-to-invoke-a-Web-Service-from-a-Stored-Procedur
In summary, use these extended stored procedures: SP_OACreate and SP_OAMethod to access objects such as MSXML2.ServerXMLHttp.
For something like this, you don't need a full web service implementation. You can use SQLCLR (SQL Server's .NET Integration) to submit the request to the URL, get the response back in XML (unless you can find a JSON library that will work without being set to UNSAFE), and then parse that response.
Look at the following MSDN pages:
According to the Google Geocoding API documentation, the API URI should be formatted similarly to the following:
Just submit that with those 2 variables substituted with their proper values via
HttpWebRequest
, then callHttpWebRequest.GetResponse
, then callHttpWebResponse.GetResponseStream
. And do not forget to call theClose
andDispose
methods ofHttpWebResponse
(or instantiate it in ausing
block)!!Additional notes:
TRUSTWORTHY ON
. Just sign the assembly with a password, then create an asymmetric key in themaster
database by pointing to your signed DLL, then create a login from that asymmetric key, and finally grant that login theUNSAFE ASSEMBLY
permission. Then you can set the assemblyWITH PERMISSION_SET = EXTERNAL_ACCESS
.