How to rename variables

The Rename functionality provides an easy way to replace all occurrences of variable names with new ones in order to improve code readability, and automatically finds and corrects all references to them.

An object can be renamed only in a script, not in a database.

The Rename refactoring is available for:

  • Local variables
  • SQLCMD variables

To rename a variable:

1. In the SQL code editor, place the cursor over the variable you want to rename and do one of the following:

  • Right-click the variable and select Rename.
  • Go to the SQL Complete menu and then select Rename.
  • Press F2.

The object will be highlighted.

2. Type a new name for the variable. As you type, a tooltip appears instructing you to Press F2 to preview changes or Enter/Tab to apply.

3. To preview the code changes, press F2. In the Preview Changes - Rename dialog that opens, view the code changes and click Apply to apply them.

4. To proceed without preview, press Enter/Tab to apply your changes in the code.

Note

If the variable and the column name are the same, the renaming of the variable does not affect the column name.

Let’s take the following procedure as an example:

CREATE OR ALTER PROCEDURE Person.EmailAddressID 
  @EmailAddress VARCHAR(30) = '%'
AS
  DECLARE @EmailAddressList VARCHAR(30)
  SELECT
    email.EmailAddressID AS EmailID
  FROM Person.EmailAddress email
  WHERE email.EmailAddress LIKE @EmailAddress;
GO
EXECUTE Person.EmailAddressID @EmailAddress = 'adriana%'
GO

Try to rename the variable and procedure parameter using the built-in SSMS Find & Replace functionality. As you can see, the name of the procedure will be changed as well.

Renaming variables

With the SQL Complete Refactoring feature, the renaming of variables and procedure parameters will not cause the renaming of the procedure itself.

Renaming variables

Note

You can undo any action by pressing Ctrl+Z.

Example: Rename a variable in the CREATE OR ALTER PROCEDURE statement