Problem
In my previous project, I was asked to call Web Services from SQL Server stored procedures.
It was done using SQL CLR. By using CLR, we can run and manage the code inside the SQL Server.
Code that runs within CLR is referred to as a managed code.
We can create the stored procedures, triggers, user defined types and user-defined aggregates in the managed code. We can achieve significant performance increases because the managed code compiles to the native code prior to the execution. We can use SQL CLR in in SQL Server 2005 and later.
Why SQL CLR in SQL Server?
In some cases, some tasks are not possible by T-SQL as per my requirement. We can go with SQL CLR.
The tools used in this post are.
- Visual Studio 2015
- SQL Server 2014
In Action
- Create SQL Server Database project in VS 2015.

- Add SQL CLR C# stored procedure.

Name the stored procedure As CallWebService.
- Add C# codes to Call Webservice. I am using the Web Service, mentioned below to do the test.
http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry
I am simply writing a response to a text file. You can do it as per your desire.- HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.webserviceX.NET//globalweather.asmx//GetCitiesByCountry?CountryName=Sri Lanka");
- request.Method = "GET";
- request.ContentLength = 0;
- request.Credentials = CredentialCache.DefaultCredentials;
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- Stream receiveStream = response.GetResponseStream();
- // Pipes the stream to a higher level stream reader with the required encoding format.
- StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
- Console.WriteLine("Response stream received.");
- System.IO.File.WriteAllText("d://response.txt", readStream.ReadToEnd());
- response.Close();
- readStream.Close();
- Enable CLR and set trust worthy on the database. I am using AdventureWorks database.
- sp_configure 'show advanced options', 1;
- GO
- RECONFIGURE;
- GO
- sp_configure 'clr enabled', 1;
- GO
- RECONFIGURE;
- GO
- alter database [AdventureDatabase] set trustworthy on;
- Build Visual Studio Project.
It will generate DLL in bin folder.
- Register assembly in the database.
Go to AdventureWorks > Programmability > Assemblies
Right click on Assemblies and click new Assembly.

Set Permission to External access and browse for our DLL. It is in the bin folder of your project.
Once you add it, we can see the assembly registered in side Assemblies, as mentioned below.

- Create stored procedures to call assembly’s stored procedure.

Once you created a stored procedure, you can see locked stored procedure.

- Hence, we have finished executing the stored procedure. You can see the text file is generated in the drive.


Sixta ZerlauthPosted Sep 11, 2023, 6:40 PM
I added ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; to achieve a secure channel. But inside SQL-Server I got the message "cannot establish a secure channel" Outside SQL-Server the assembly works fine.
Paulo PintoPosted Sep 7, 2023, 4:28 PM
Add these to the usings section of the file: using System.IO; using System.Net; using System.Text;
Manoj vaishPosted Mar 31, 2020, 7:01 AM
Msg 6522, Level 16, State 1, Procedure CallCLRSP, Line 0 [Batch Start Line 4]A .NET Framework error occurred during execution of user-defined routine or aggregate "CallCLRSP": System.Security.HostProtectionException: Attempted to perform an operation that was forbidden by the CLR host. The protected resources (only available with full trust) were: All The demanded resources were: UI System.Security.HostProtectionException: at StoredProcedures.CallWebService() Error is coming
Manoj vaishPosted Mar 31, 2020, 7:01 AM
The solution you have provided is not working.
jf DiazPosted Aug 19, 2019, 5:37 PM
Take a look to these repository. It could help you out https://github.com/geral2/SQL-APIConsumer
Melissa PereiraPosted Apr 4, 2019, 5:32 AM
Thank you Jeevan for this. I am working on a project where i need to consume data from a web service that is in JSON format and insert into SQL table, could you help me with an example how I can call the web service URL and the insertion is fairly explained in this post. Thank you
akansha aggarwalPosted Nov 13, 2018, 3:57 AM
I want to pass file as a parameter in my post request.
Sacchin GuptaPosted Jun 6, 2018, 5:50 AM
Very useful article. Thanks for sharing.
Gagan SharmaPosted Nov 8, 2016, 11:58 PM
Thanks for sharing....nice
Bhuvan PandeyPosted Oct 25, 2016, 7:32 AM
Good one.
Janith ChampikaPosted Oct 24, 2016, 7:23 AM
Good Article
Sathira UdayangaPosted Oct 24, 2016, 6:53 AM
Thanks for sharing.