How to work with SQL aliases in dbForge Studio for SQL Server

dbForge Studio can automatically generate aliases for each table object (table, view, table-valued function, synonym) referenced in the FROM list of your SQL document.

However, users have the flexibility to customize the alias generation behavior according to their preferences.

  1. In the Tools menu, select Options.

  2. In the Options dialog, go to Text Editor > Code Completion > Aliases.

Automatic alias assignment

When the Generate alias on commit checkbox is selected, the Studio operates as follows:

  • Adds aliases to tables, views, table-valued functions, and synonyms: As the user selects a database object from the suggestion list, dbForge Studio automatically assigns an alias to it.

  • Adds table alias to column names: When the user chooses to select all columns using the asterisk (*) wildcard or individually picks specific columns from the suggestion list, dbForge Studio adds a table alias to each column name.

The following rules are used when creating aliases:

  • If possible, dbForge Studio generates aliases using the first letter of a table object name.
  • For names with underscores:
    for TBL_Address alias ta is assigned

  • For names with hyphens:
    for Tbl-address alias ta is assigned

  • For names with CamelCase:
    for TblAddress alias ta is assigned

  • For names with numbers:
    for 111 alias a is assigned

  • If a generated alias name matches a keyword, the alias name is wrapped in square brackets. For example, if the table object name is GeneralObjects, the alias name will be [go].

  • If a newly generated alias coincides with an existing one, dbForge Studio adds symbols 1, 2,…n to the newly generated alias to avoid ambiguity.

Custom alias mapping

dbForge Studio allows users to manually assign aliases to an identifier (table, view, table-valued function, synonym) referenced in an SQL document. Aliases can also be assigned to databases, servers, and linked servers.

To add a custom alias:

  1. In the Tools menu, click Options.

  2. In the Options window, navigate to Text Editor, Code Completion, Alias. The Alias page opens.

  3. On the Alias page in the Condition column, specify the database object name (you may use a mask) to which you want to assign an alias.

  4. In the Action column, specify the custom alias you prefer.

  5. Click OK.

Alias Refactoring

When a custom alias is created, the dbForge Studio assigns the alias name to an object that is referenced in an SQL statement according to a specified alias mask.

Note
Custom aliases have precedence over automatically created ones.

Alias refactoring

To rename an alias:

  1. Right-click the required alias and select the Rename command from the shortcut menu or select the alias and press F2 - the alias will be highlighted.

  2. Type a new name for the alias in the SQL editor window.
    As you type, a ToolTip appears instructing to Press F2 to preview changes or Enter/Tab to apply.

  3. Press F2 to open the Preview Changes - Rename dialog window and preview code changes; to apply changes press the Apply button.

  4. Alternatively, press Enter/Tab to apply changes in the code.

Alias Refactoring

Alias masks

dbForge Studio offers a number of alias masks for managing custom alias mapping. You can use the alias masks to specify conditions and actions when assigning aliases. For example, you can assign different aliases for same-named objects.

The following list demonstrates examples of the condition masks:

<product> - exact match;

<*product> - matches object names ending with “product”

<product*> - matches object names starting with “product”

<product> - matches object names including “product”

<*> - matches any valid object name

<schema>.<product> - exact match with the object name product using schema as a prefix

<schema*>.<product*> - matches object names starting with product with a prefix starting with schema

<database>.<schema>.<product> - matches the object name product with a database and schema in the prefix

The following list demonstrates examples of the action masks:

Id<1> - adds prefix Id to a generated alias

<1>.<2> - generates alias with a prefix

<[Dept]1> - excludes Dept from a generated alias

No Alias - when No Alias is specified in the Action column for a certain condition, no alias will be generated for that condition

Mask prioritization

User-defined (custom) masks have precedence over the automated alias mapping. Alias masks specified in the grid have top-down priority. For example, in the following table demonstrating an example custom alias mapping, the <Person> mask condition has a higher priority than <Product*>:

Condition Action
<Person> <alias1>
<Product*> <alias2>
<Sales>.<Product*> <alias2>

alias1 will be assigned to all objects including Person;

alias2 will be assigned to all objects starting with Product;

alias3 will be assigned to all objects starting with Product with parent Sales.

Helpful materials

SQL Aliases: Improving Query Efficiency and Clarity

Find out what SQL aliases are and how to use them to enhance the efficiency of SQL scripts.

Want to Find out More?

Overview

Overview

Take a quick tour to learn all about the key benefits delivered by dbForge Studio for SQL Server.
All Features

All features

Get acquainted with the rich features and capabilities of the Studio in less than 5 minutes.
Request a demo

Request a demo

If you consider employing the Studio for your business, request a demo to see it in action.
Ready to start using dbForge Studio for SQL Server?