Site icon Ryadel

SQL Server - How to ALTER one or more Table Columns avoiding a Command Timeout in T-SQL

SQL Server - Recuperare il Product Key da una installazione esistente

If you've stumbled upon this post it most likely means that you need to change the type and/or format of one or more table column of a SQL Server using a standard ALTER query avoiding a Command Timeout and - most importantly without having to restart the whole Database.

The Problem

Let's take for example the following Attachments SQL table:

And let's say we want to change the Description column from nvarchar(250) to nvarchar(MAX). Needless to say, the table is live on a production server and contains hundreds of thousands records, therefore any attempt to change that data type using the following ALTER query:

Would likely hit a Connection Timeout.

What can we do?

The Fix

The best workaround I found to overcome such scenario is to take advantage of  SP_CONFIGURE, a built-in Stored Procedure which can be used to display or change global configuration settings for the SQL server. More specifically, the setting that we want to change is "remote query timeout", which defaults to 600 (seconds).

The plan is to bring it to 0, execute our query (without risking a timeout anymore) and then setting back to 600 immediately after. Here's how we can do that:

However, before doing that, it might be the case of executing SP_CONFIGURE in display mode in order to be 100% sure that the default value to restore is 600:

If that's not the case, just change the relevant value in the previous query accordingly.

 

Exit mobile version