Category Archives: Tech Notes

T-SQL Rename Logical and Physical Database Files

I used to get really nervous about renaming database files, which I think is an appropriate response.  Changing the attributes of the database’s physical objects does sound a bit scary on the surface, but once you’ve done it a few times you’ll realize it’s easy to do, and easy to fix if you make a mistake.  This simple script template shows the basic steps of the process.


Can’t RDP to Server after Windows Patch (CredSSP)

I was preparing to remote into a virtual machine to do some maintenance when I encountered an error.

“An authentication error has occurred.
The function requested is not supported

Remote computer: [Computer Name]
This could be due to CredSSP encryption oracle remediation.
For more information, see”

Information from the link in the error:

Credential Security Support Provider protocol (CredSSP) is an authentication provider that processes authentication requests for other applications.

A remote code execution vulnerability exists in unpatched versions of CredSSP. An attacker who successfully exploits this vulnerability could relay user credentials to execute code on the target system. Any application that depends on CredSSP for authentication may be vulnerable to this type of attack.

This security update addresses the vulnerability by correcting how CredSSP validates requests during the authentication process.


The bottom line is that in order to RDP into the target server both computers need to have the update installed.  In my case, my local machine was updated, but the target machine was not yet updated.  This target machine did not have automatic updates turned on and was an Azure VM.  I could have contacted support, or turned on automatic updates from the portal, but the reality was I needed to get into the machine quickly to handle an emergency fix.  Due to the simple architecture (stand-alone VM) I could not push the patch via SCCM or GPO.

Work-around 1:

Continue reading Can’t RDP to Server after Windows Patch (CredSSP)

Power BI On-Premises Data Gateway Service Failures

Power BI On-Premises Data Gateway is a windows service (PBIEgwService) that moves data from on-premises to the cloud, and also is used by Power BI Report Server.  The data gateway has been failing nearly daily. I’ve changed the service settings to restart upon failure with a 3 minute buffer. If this does not resolve the issue I might set up a heart-beat process that checks the health of the service and if it’s ever not running it will start it.

Microsoft Released SQL Operations Studio (preview)

Microsoft recently announced at the 2017 SQL PASS event that they would be previewing a new tool for sql server that works across multiple operating systems.

“SQL Operations Studio (preview) is a free, light-weight tool, that runs on Windows, macOS, and Linux, for managing SQL Server, Azure SQL Database, and Azure SQL Data Warehouse; wherever they’re running.”

It’s now available for preview.

Read and Download:

More info (video) at TechNet:

Remove Numbers from Text SVF, Nested vs CTE

I need to remove numbers from a string while processing some data  as part of an ETL process in a data warehouse.  I have to do this for many columns and potentially many tables so I decided to put the logic into a Scalar-Valued Function (SVF), understanding there are performance detriments when using SVFs. Generally this will be used on relatively small data sets for cleaning “codes” that will be insert into a slowly changing dimension.  I was curious about how to do this.  My instinct was to do it using nested replace functions.  I did a quick google search to see what options the collective would recommend.  I came across a post on Stack Overflow that mentioned the nested replace functions, and then someone also mentioned using a recursive Common Table Expression (CTE), which I thought was a creative suggestion.

I have a soft spot in my heart for recursive CTEs so I thought I’d try both options and see which solution performs better.  Let’s start with building the functions:

Now that we have some functions we need some test data so let’s create a simple table and populate it with simi-random data by leveraging the newid() function.

Continue reading Remove Numbers from Text SVF, Nested vs CTE

Backup Transaction Logs to Nul for All Online Databases

First of all, never use this in a production environment!   This script is to backup your transaction logs to a “nul device” for all online databases which are not using simple recovery model.  In windows it is indeed spelled “nul” with one “L”.  The only reason you would want to do this is if you have a non production environment using full recovery model and this server architecturally mirrors your production environment.  For example, we have a “staging” server that is used for testing our code changes before they go into production.  We require the staging environment to be as close to production as possible and have scheduled scripts that sync them weekly.  In this scenario, we have many databases in the staging server that are using full recovery model but we do not want to backup the t-logs, we would rather just throw them away.

To learn more about NUL devices, here’s a link to the wikipedia page:

Resolve SQL Server Fatal error: cannot create ‘R_TempDir’

I was trying to test an install of R in SQL Server 2016 and when running a script I received this error: Fatal error: cannot create ‘R_TempDir’

Following the instructions here, I enabled external scripts, restarted the sql server service, and then tried to run the following test script:

This is when the fatal error occurred.  As the error suggests, R is having some issues creating a temporary directory.  After some internet searching and trial and error I got past the issue.

Enable  8dot3 File Names

R configuration uses the 8dot3 file name convention, also known as “short names”.  To enable this on windows 10, run the following command in CMD (command prompt):

For more options and information look here:

Give access to the working directory to R

Locate and open “rlauncher.config” file in a text editor.  This file will be under the “<sqlserver_instance>\binn” directory.  Take a look at the location of WORKING_DIRECTORY.  This should have a “short name” file path.  The path should be something like “<sqlserver_instance>\EXTENS~1”, and “\EXTENS~1” is equivalent to “\ExtensibilityData”.  We need to give access to R to this folder. I did this by changing the permissions to full control to everyone.  You may want to be more restrictive here, but in my case this did not matter.

  • Right click folder > Properties > Security tab > Advanced > Add
  • Select a principal (I entered “Everyone”)
  • Tick “Full control” under basic permissions and click “OK”
  • Tick “Replace all child object and permissions entries with inheritable permissions entries from this object” and click “OK”

Now if you rerun the script above you should get a result of “hello, 1”.

Calculate Weighted Average in Excel using SumProduct() Function

Weighted averaged, also known as weighted arithmetic mean, is similar to an ordinary average, except that instead of each of the data points contributing equally to the final average, each data point is “weighted” and thus contributes more or less depending on the given weight.  The weight would typically be some correlated data point that indicates significance of the value being averaged.

For example, let’s say we were tracking the progress of a project and its various tasks.  Our data set includes the task number, percent completed, and estimated hours to complete the task.  We want to calculate an overall percent completed for the project based on how complete the individual tasks are.  Take a look at the example below.



If we calculate a straight average of the percent complete column then we get 75% completed overall.  However, this could be deceiving because some tasks will take longer to complete than others, as indicated in the estimated hours column.  Let’s use this estimated hours column as the “weight” in our weighted average.

One way to do this would be to:

  • multiply the item (% Complete) by the weight (Estimated Hours) at the row level, shown in column D below.
  • sum up all of those products in column D, and then
  • divide by the sum of the weight for all records (column C)



The result of the weighted average is 49%, which is much different than the 75% from a straight average.  This is because there are items which have a high percent completed but a low estimated hours to complete, and also items with low percent completed and higher estimated hours to complete.  By including a weight to factor in the level of effort for each item you get a much more accurate result.

What’s the problem with this approach? If the number of tasks changes then it becomes a fairly manual task to adjust the rows and formulas accordingly.  Also, I don’t like the idea of having a “helper” or “work” column inserted into the data set.  There is a quicker and simpler way to calculate the weighted average than the method I just explained.

Continue reading Calculate Weighted Average in Excel using SumProduct() Function