Home > Table variables
Tutorial:
EMAIL THIS

Table variables

07 Feb 2007 | McGraw-Hill

Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   

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

     Home: Introduction
     Tip 1: T-SQL identifiers
     Tip 2: Database object qualifiers
     Tip 3: Character string data types
     Tip 4: Date, time and number data types
     Tip 5: Special data types-Part 1
     Tip 6: Special data types-Part 2
     Tip 7: Local variables
     Tip 8: Global variables
     Tip9: Table variables
     Tip 10: Flow control statements
     Tip 11: Blocks, looping and conditional statements
     Tip 12: Unconditional and scheduled execution
     Tip 13: Transact SQL cursors
     Tip 14: Cursor related statements
     Tip 15: Problems and justified use of cursors

    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.




    Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



    RELATED CONTENT
    SQL/Transact SQL (T-SQL)
    Using DELETE and TRUNCATE TABLE statements to delete data in SQL Server
    SQL language crash course (just enough to be dangerous)
    Working with IntelliSense in SQL Server 2008 Management Studio
    SQL Server Mailbag: Stored procedures, triggers and SSRS reports
    Working with sparse columns in SQL Server 2008
    Determining the source of full transaction logs in SQL Server
    New GROUP BY option provides better data control in SQL Server 2008
    Using the OPENROWSET function in SQL Server
    Loading data files with SQL Server's BULK INSERT statement
    Importing and exporting bulk data with SQL Server's bcp utility
    SQL/Transact SQL (T-SQL) Research

    SQL Server Stored Procedures
    SQL Server Mailbag: CALs, witnesses and unwanted changes
    SQL Server Mailbag: Stored procedures, triggers and SSRS reports
    Top tips and tricks for SQL Server database development
    Top 10 SQL Server development tips of 2008
    SQL Server trigger vs. stored procedure to receive data notification
    SQL Server errors, failures and other problems fixed from the trenches
    SQL Server and data manipulation in T-SQL
    How to use SQL Server 2008 hierarchyid data type
    SQL Server stored procedures tutorial: Write, tune and get examples
    Check SQL Server database and log file size with this stored procedure

    XML in SQL Server
    Processing XML files with SQL Server functions
    Top 10 SQL Server development tips of 2008
    Manage traces in SQL Server 2005 Analysis Services with XMLA commands
    Create a computed column in SQL Server using XML data
    Create DDL table in SQL Server 2005 to audit DDL trigger activity
    Retrieve XML data values with XQuery in SQL Server 2005
    XML data type in SQL Server 2005 vs. VARCHAR (MAX)
    T-SQL commands vs. XML AUTO in SQL Server
    Basic Transact-SQL programming constructs: 15 tips, 15 minutes
    T-SQL identifiers

    RELATED GLOSSARY TERMS
    Terms from Whatis.com − the technology online dictionary
    ACID  (SearchSQLServer.com)
    Collaboration Data Objects  (SearchSQLServer.com)
    commit  (SearchSQLServer.com)
    container  (SearchSQLServer.com)
    DAO  (SearchSQLServer.com)
    fetch  (SearchSQLServer.com)
    OLE DB  (SearchSQLServer.com)
    query  (SearchSQLServer.com)
    SQL  (SearchSQLServer.com)
    T-SQL  (SearchSQLServer.com)

    RELATED RESOURCES
    2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
    Search Bitpipe.com for the latest white papers and business webcasts
    Whatis.com, the online computer dictionary




  • Secure SQL - Data Security for Your Database
    HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts