How to rename a variable in SSMS with dbForge SQL Complete

Without a doubt, SSMS is the go-to IDE when it comes to SQL Server database management, but it's no secret that its capabilities are somewhat limited. That's why we made it possible to ehnance them with dbForge SQL Complete, a top-tier add-in focused on advanced code completion, formatting, refactoring, and debugging.

In this brief tutorial, we will show you the easiest way of renaming local and SQLCMD variables in order to improve code readability, and it automatically finds and corrects all references to them. Note that an object can be renamed only in a script, not in a database.

Now let's see how it works in general. First of all, open SSMS and make sure you are connected to the required database.

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

  • Right-click the variable and select Rename
  • Navigate to the SQL Complete menu and select Rename
  • Press F2

The variable will be highlighted.

2. Enter 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 changes you have introduced, press F2. This will open the Preview Changes - Rename dialog, where you can make sure everything is correct and click Apply afterwards.

4. If you want to proceed without previewing, press Enter/Tab to apply your changes. Note that if the names of the variable and the column are identical, the renaming of the variable will not affect the column name.

Example

Now let's take a look at a specific example. For instance, we have the following procedure:

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

Next, let's rename the variable and procedure parameter using the integrated Find & Replace feature of SSMS. Take note that the name of the procedure will be changed as well.

Rename a variable

As you can see, if you rename variables and procedure parameters with SQL Complete, the procedure itself will not be renamed.

Rename a variable

Note that you can undo any action using Ctrl+Z.

Conclusion

We hope that this tutorial will be useful for your future endeavors. And now we'd like to unveil some more capabilities of dbForge SQL Complete that you will most likely find useful in your daily work. Here is what you can do with its help:

  • Double your coding speed with context-aware SQL autocompletion and relevant object suggestions
  • Get quick information on database objects
  • Reduce manual coding with an expandable library of predefined and custom snippets
  • Boost the quality of your code with T-SQL Debugger
  • Beautify your code and introduce coding standards with SQL formatting
  • Perform effective refactoring of tables, columns, stored procedures, and functions with automatic correction of references to the renamed objects
  • Visualize, search, copy, and aggregate data directly from the SSMS results grid
  • Get your tasks done even faster with a set of productivity enhancements

Sounds interesting? Then we gladly invite you to download SQL Complete for a free 14-day trial and see it in action.

Note that SQL Complete comes as part of a bundle called dbForge SQL Tools, which contains 15 standalone apps and SSMS add-ins designed to empower you with a comprehennsive set of features for SQL Server development, management, and administration.