Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The Refactor contextual menu in the Transact-SQL Editor allows you to rename or move an object to a different schema and preview 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 SQL database projects sections.
Rename a type
Right-click the
Productstable (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 select OK.In the next screen, you can preview a list of scripts that this rename operation is going to affect. Specifically, all the places that refer to
Productsare highlighted. This process is similar to the Find All References task in the previous procedure. Select anything on the top pane and view the actual change in the scripts (highlighted in green) in the bottom pane.Select Apply.
For script files that are already opened in Table Designer or Transact-SQL Editor, the Transact-SQL Editor highlights the locations where changes took place with a green bar on the left.
Notice the addition of
TradeDev.refactorlogin 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
TradeDevdatabase under Local in SQL Server Object Explorer, and select Refresh.Expand Tables, and you see that the
Productstable was renamed.Right-click
Productand select View Data. Existing data is kept intact regardless of the rename operation.
Warning
If a refactor log is deleted, the complete history of the refactoring is also deleted. When the project is published to a database where previous refactor operations aren't applied, any refactor operations completed before the refactor file was deleted, are published as drop and create operations. As a result, data loss can occur.
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 AS p;In the Preview Changes dialog box, select
SELECT * from Product pon the top pane to highlight it.In the Preview Changes pane below, notice that
*expands to the following columns in the script.[Id], [Name], [ShelfLife], [SupplierId], [CustomerId]Select 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.
Fully qualify database object names
Make sure
GetProductsBySupplier.sqlis still open in Transact-SQL Editor.Place the cursor on
Productin this line and right-click. Select Refactor, and Fully-Qualify Names.SELECT [Id], [Name], [ShelfLife], [SupplierId], [CustomerId] FROM Product AS p;Select the Apply button in the Preview Changes dialog box. All the object references are 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] AS p;
Move schema
Right-click the object that you want move. Select Refactor, and Move Schema.
In the New Schema list, select the name of the schema into which you want to move the object. Select OK.
If you select 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.