Click on the banner to learn about and purchase my database training on Azure

Is your SQL Server up to date? Returning the latest version of Service Pack (SP) or Cumulative Update (CU) for your instance

Views: 1.948 views
Reading Time: 6 minutes

Speak guys!
In this article I want to bring you a solution I created yesterday to include in a checklist I'm developing, which allows me to identify the latest version of Service Pack (SP) and Cumulative Update (CU) for the version of SQL Server where I am running this code.

To identify the latest updates for each version, I will use the site, which is always up to date with the latest SQL Server updates (could also be the

To extract page data to SQL Server, I will use the OLE Automation, which has already published several articles explaining how to perform WebScraping of WEB pages and API's using this resource.

After retrieving the page's HTML source code, I need to identify the table that contains the data according to the version I'm looking for and also need to perform some treatment on the returned code to correct some inconsistencies:

  • Conversion of HTML entities (ex:)
  • Attributes of tags without quotes (eg class = sp instead of class = ”sp” = invalid HTML)
  • Open and not closed tags (ex: Column1 Column2 instead of Column1 Column2 )

This turns out to be the hardest part of this script, because if these code inconsistencies are not remedied, SQL Server returns me an error when trying to convert XHTML to XML.

After performing all of these treatments and converting the XHTML code to XML, I apply XQuery commands to identify the first row of the respective SQL Server version table and return to SP

Source code

After a brief summary of what I needed to do in order to accomplish this task, I will make available below the source code of the created solution, which can be used with a simple “F5”.

Examples of use

Now I will demonstrate some examples of how this Stored Procedure works in practice.

Retrieving the last update from an updated SQL Server 2017


Retrieving the last update from a SQL Server 2017


Retrieving the last update from a SQL Server 2008 R2


Retrieving the last update from a SQL Server 2019


I need to show ALL updates of a version of SQL Server

If you want to show all updates of the desired version of SQL Server and not just the last one, you can use the code below (after the 150 line):



Well guys, I hope you enjoyed this article. I am starting to use it in environments to do a quick check up and it turned out to be very practical. If you want to create monitoring for this in your environment by analyzing all instances, you can create this Stored Procedure on all instances and use the returned data to send automated emails as needed.

In this monitoring scenario, another very interesting solution is that of Reginaldo Silvawho made the post Get-SQLBuildVersion a Different Way to Stay Updated, where he shared a solution written in PowerShell, which he developed, where he passes the instance name as a parameter and the script already does this validation and parameters to email the result. Very practical to use in multiple instances.

Like Reginaldo, I will highlight the article Cumulative update, apply or not, that's the question! do Luciano Moreira (Luti), where he sets out his views on the constant application of SQL Server updates (I agree with him), since many DBA's are not in the habit of keeping SQL Server updated for fear of introducing a new bug after applying an update.

In my view, this doesn't make much sense these days, as these events are quite rare, and as you always apply updates, your environment is always up to date with new features, bug fixes, and security enhancements, as well as having a process more mature and common application updates in the company.

Ah, remember that from SQL Server 2017, we will have no more Service Packs, only Cumulative Updates, as documented in this article here.

That's it, guys.
I hope you enjoyed this article and even more!