Use Caution Altering Tables with SQL Server Management Studio

Problem

Often times we use the SQL Server Management Studio GUI to perform simple tasks such as altering a column in a table. However, you should always check your scripts before blindly performing the actions recommended by SSMS, because they may not always be, and are often not, the best way to perform actions within SQL Server.

Solution

Let’s take a look at a simple task of resizing a column in the AdventureWorks database. If you need to change the NationalIDNumber in the HumanResources.Employee table, you can simply run the following script.


USE



AdventureWorks
GO

ALTER
TABLE HumanResources.Employee
ALTER
COLUMN NationalIDNumber NVARCHAR(20) NOT NULL

Now, let’s perform the same task using SSMS. You can right-click the HumanResources.Employee table and select Design from the context menu to display the Table Designer. If you change the column length and generate the change script, you will end up with a script that contains 443 lines of code and involves creating a temp table with the new definition, inserting all the data from the old table, dropping the old table, and then renaming the temp table. If this operation is performed on a large table, what would have been a quick change can now take several hours.

SQL Server 2008 Management Studio

SQL Server 2008 Management Studio has an option on by default that prevents operations that are required drop and create tables

Next Steps

Leave a Reply

Your email address will not be published. Required fields are marked *