Table variables

Table variables are similar to temporary tables and are declared using the table data type. This following book excerpt contains further explanations and examples for declaring table variables when working T-SQL programming constructs.

Table Variables Table variables are objects similar to temporary tables and were introduced in SQL Server 2000. A table variable is declared using the table data type. A statement declaring a table variable initializes the variable as an empty table with a specified structure. As a table definition, such a statement includes definitions of columns with their data type, size, precision, and optional constraints (primary key, identity,...

unique, and check constraints). All elements have to be defined during the declaration. It is not possible to alter or add them later. The following batch declares a table variable, inserts rows, and returns them to the user:

Declare @MyTableVar table
     (Id int primary key,
      Lookup varchar(15))

Insert @MyTableVar values (1, '1Q2000')
Insert @MyTableVar values (2, '2Q2000')
Insert @MyTableVar values (3, '3Q2000')

Select * from @MyTableVar
Go

Because of their nature, table variables have certain limitations:

  • Table variables can only be part of the Select, Update, Delete, Insert, and Declare Cursor statements.
  • Table variables can be used as a part of the Select statement everywhere tables are acceptable, except as the destination in a Select...Into statement:
    Select LookupId, Lookup
    Into @TableVariable       -- wrong
    From Lookup
    
  • Table variables can be used in Insert statements except when the Insert statement collects values from a stored procedure:
    Insert into @TableVariable    -- wrong
         Exec prMyProcedure
    
  • Unlike temporary tables, table variables always have a local scope. They can be used only in the batch, stored procedure, or function in which they are declared.
  • Table variables are considered to be nonpersistent objects, and therefore they will not be rolled back after a Rollback Transaction statement.

TIP
If possible, use table variables instead of temporary tables. Table variables have less locking overhead and therefore are faster.


Basic Transact-SQL Programming Constructs


The previous tip is from "15 tips in 15 minutes: Basic Transact-SQL Programming Constructs," excerpted from Chapter 3, of the book "Microsoft SQL Server 2005 Stored Procedure Programming in T-SQL & .NET" by Dejan Sunderic, courtesy of McGraw-Hill Publishing.

This was first published in February 2007

Dig deeper on SQL-Transact SQL (T-SQL)

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchBusinessAnalytics

SearchDataCenter

SearchDataManagement

SearchAWS

SearchOracle

SearchContentManagement

SearchWindowsServer

Close