Rename objects in SSDT

Aim: Rename objects in SQL Server Data Tools (SSDT) without breaking dependencies.

You can rename SSDT objects in their definition (.sql) files however this does not update the name of the object throughout the project. Renaming an object in this way will break any references to the object in dependent objects (e.g. views or stored procedures).

For example, in the following project, renaming a table from “testTable” to “newTestTable” results in Build errors due to the view that references the object’s old name.

01_rename_via_solution_explorer

However, the SQL Server Object Explorer from the View menu gives us more useful options to refactor objects. In the SQL Server Object Explorer, expand the Project folder & the relevant object subfolder. Right-click the object (e.g. testTable), select Refactor & Rename.

02_sql_server_object_explorer

In the pop-up window, change the object name, tick “Preview Changes” & click OK.

03_rename

The Preview shows each dependent object that will be changed as part of the refactor. Click Apply to confirm the change.

04_preview

Following completion of the refactor, the build will succeed as the dependent objects no longer reference the object’s old name. However, object’s definition file names will not be updated so you may need to align them to the new object names by updating them in the Solution Explorer.

05_successful_build

In the Solution Explorer, you will also see that the project now contains a .refactorlog file containing details of all refactors applied to the project.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Kevin Kline

Career and Technical Advice for the IT Professional

TroubleshootingSQL

Explaining the bits and bytes of SQL Server and Azure

SQL Authority with Pinal Dave

SQL Server Performance Tuning Expert

Powershellshocked

A blog about PowerShell and general Windows sysadmin stuff

Simon Learning SQL Server

I'm trying to become "better" at SQL Server and data - here's how I'm doing it!

%d bloggers like this: