Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk1 min

Using a Table Variable Name as a Column Prefix in SQL Server

To qualify a table variable’s columns in SQL Server, give it an alias in FROM and use that alias as the column prefix.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use an alias, not the table variable’s name, as the column prefix. Put the variable in the FROM clause, assign it an alias, and qualify columns with that alias—for example, m.EmployeeID.

Can you use the table variable name as a column prefix?

In Microsoft’s documented T-SQL syntax, qualify a table variable’s columns with an alias. Microsoft Learn states that outside a FROM clause, table variables must be referenced by using an alias. Its join example names the variable @MyTableVar, aliases it as m, and refers to columns as m.EmployeeID and m.DepartmentID. See Microsoft Learn’s table (Transact-SQL) documentation.

Example: alias the variable in FROM

DECLARE @MyTableVar TABLE
(
    EmployeeID int,
    DepartmentID int
);

SELECT m.EmployeeID,
       m.DepartmentID
FROM @MyTableVar AS m;

Here, @MyTableVar identifies the table variable as the source in FROM. The alias m is the qualifier used before each column name. Do not substitute the variable’s @-prefixed name for the documented alias qualifier.

When the alias matters

Use an alias when a query needs qualified column references, especially in joins. Microsoft’s example uses the alias in a join condition. A query selecting columns from a table variable can also use unqualified column names, as in Microsoft’s simple FROM example; when you need a prefix, use the alias.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Scope

This guidance is for Microsoft T-SQL documentation covering SQL Server, Azure SQL Database, Azure SQL Managed Instance, and SQL database in Microsoft Fabric. It does not establish equivalent syntax rules for other database systems. The MicrosoftDocs source file records an editorial date of April 5, 2023: table (Transact-SQL) source file.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.