How to: Use Rename and Refactoring to Make Changes to your Database Objects
The Refactor contextual menu in the Transact-SQL Editor allows you to rename or move an object to a different schema, and do a preview of all affected areas before committing the change. You can also use the Refactor menu to fully qualify all references to database objects, or expand any wildcard characters in SELECT
statements in your database project.
Warning
The following procedure uses entities created in previous procedures in the Project-Oriented Offline Database Development sections.
To rename a type
Right-click the Products table (Products.sql) in Solution Explorer and select View Code to open the script in Transact-SQL editor.
Right-click
[Products]
in the script, select Refactor, and Rename.In the New Name field, change it to Product. Leave the Preview Changes option checked and click OK.
In the next screen, you will be able to preview a list of scripts that are going to be affected by this rename operation. Specifically, all the places that refer to
Products
will be highlighted. This is very similar to the Find All References task in the previous procedure. Click anything on the top pane and view the actual change in the scripts (highlighted in green) in the bottom pane.Click Apply.
For script files that are already opened in Table Designer or Transact-SQL Editor, notice that the Transact-SQL Editor has highlighted locations where changes have taken place with a green bar on the left.
Notice the addition of TradeDev.refactorlog in Solution Explorer. Double-click to open it. It contains an XML representation of all the changes in this session.
Press F5 to build and deploy the project to the local database.
Right-click the TradeDev database under Local in SQL Server Object Explorer, and select Refresh.
Expand Tables, and notice that the Products table has been renamed.
Right-click Product and select View Data. Notice that existing data is kept intact regardless of the rename operation.
Warning
If a refactor log is deleted, the complete history of the refactoring will be deleted. When the project is published to a database where prior refactor operations haven't been applied, any refactor operations completed before the refactor file was deleted, will be published as drop and create operations. As a result, data loss may occur.
To expand wildcards
Expand the Functions node in Solution Explorer, and double-click GetProductsBySupplier.sql.
Place the cursor on the asterisk in this line and right-click. Select Refactor, and Expand Wildcards.
SELECT * from Product p
In the Preview Changes dialog box, click
SELECT * from Product p
on the top pane to highlight it.In the Preview Changes pane below, notice that
*
has been expanded to the following in the script.[Id], [Name], [ShelfLife], [SupplierId], [CustomerId]
Click the Apply button. Notice the line that contains changes brought forth by the expand operation is again highlighted with a green bar on the left.
To fully qualify database object names
Make sure GetProductsBySupplier.sql is still open in Transact-SQL Editor.
Place the cursor on
Product
in this line and right-click. Select Refactor, and Fully-Qualify Names.SELECT [Id], [Name], [ShelfLife], [SupplierId], [CustomerId] from Product p
Click the Apply button in the Preview Changes dialog box. Notice the all the object references have been updated to include the name of the object's schema and, if the object has a parent, the name of the parent.
SELECT [p].[Id], [p].[Name], [p].[ShelfLife], [p].[SupplierId], [p].[CustomerId] from [dbo].[Product] p
To move schema
Right-click the object that you want move. Select Refactor, and Move Schema.
In the New Schema list, click the name of the schema into which you want to move the object. Click OK.
If you selected the Preview changes check box, the Preview Changes dialog box appears. Otherwise, the object name is updated, and the object is moved to the new schema.