Product articles SQL Prompt SQL Refactoring
Quickly rename variables all at once…

Quickly rename variables all at once with SQL Prompt

Grant Fritchey demonstrates a quick and easy way to rename all variables in a SQL script.

Despite your best efforts to enforce sensible SQL Server naming conventions, you’re going to run into situations where someone has made poor choices in naming objects, or maybe the business logic has changed and the object name no longer makes sense. Either way, easily and quickly taking care of renaming the objects within scripts can be painful and error prone.

Fixing poorly-named table variables

Let’s take this code as an example:

SQL Prompt renaming objects 1

The table variable @a is a poor choice, so I want to replace it. Yes, I can do a search and replace to try to find it and replace it within the code, but SQL Prompt offers an alternative that is both easier and, depending on if you want to review the code as you change it, faster.

I can highlight any use of the variable @a and press the function key F2. This then highlights every use of that variable:

Badly named variables in a SQL Script

If I start typing, I can rename the object, quickly and easily, and it renames everywhere at the same time:

Renaming all variables in one go using SQL Prompt

If I hit the Enter key, that gets committed to the code. However, if I hit the escape key, ESC, the changes are removed from script and it reverts back to what it was originally.

Summary

That’s it – a quick tip to help you make your code cleaner and more readable in a couple of clicks with SQL Prompt.

Take a look at my video below, or browse the rest of the handy #SuperSQLTips videos from other Data Platform MVPs and SQL Server experts, helping you get the most out of SQL Prompt:

If you’re new to SQL Prompt check it out for yourself with a free 28-day trial and see how much more productive you can be writing code every day.

Like this? Share it with other SQL professionals using the social sharing buttons below – and don’t forget to give me a shoutout @GFritchey. Thanks for reading.

 

Tools in this post

SQL Prompt

Write, format, and refactor SQL effortlessly

Find out more