0% found this document useful (0 votes)
227 views

Difference Between Table Variable and Temporary Table

Table variables and temporary tables differ in scope, creation, and capabilities. Table variables are visible only within the current batch or stored procedure while temporary tables are visible within the current session and nested stored procedures. Table variables have limitations on data types, column names, indexes, and cannot be passed between procedures. Temporary tables have fewer limitations but can be impacted by other sessions.

Uploaded by

samsonawane09
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
227 views

Difference Between Table Variable and Temporary Table

Table variables and temporary tables differ in scope, creation, and capabilities. Table variables are visible only within the current batch or stored procedure while temporary tables are visible within the current session and nested stored procedures. Table variables have limitations on data types, column names, indexes, and cannot be passed between procedures. Temporary tables have fewer limitations but can be impacted by other sessions.

Uploaded by

samsonawane09
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Difference between Table Variable and temporary table

Feature Scope Table Variables Current batch Temporary Tables Current session, nested stored procedures. Global: all sessions. Stored Procedures, Triggers, Batches. CREATE TABLE statement. SELECT INTO statement. Maximum 116 characters. User-defined data types and XML collections must be in tempdb to use. String columns inherit collation from tempdb database. Indexes can be added after the table has been created.

Usage Creation

UDFs, Stored Procedures, Triggers, Batches. DECLARE statement only.

Table name Column data types

Maximum 128 characters. Can use user-defined data types. Can use XML collections. String columns inherit collation from current database. Can only have indexes that are automatically created with PRIMARY KEY & UNIQUE constraints as part of the DECLARE statement. PRIMARY KEY, UNIQUE, NULL, CHECK, but they must be incorporated with the creation of the table in the DECLARE statement. FOREIGN KEY not allowed. Statements are not allowed. INSERT statement (SQL 2000: cannot use INSERT/EXEC).

Collation

Indexes

Constraints

Post-creation DDL (indexes, columns) Data insertion

PRIMARY KEY, UNIQUE, NULL, CHECK. Can be part of the CREATE TABLE statement, or can be added after the table has been created. FOREIGN KEY not allowed. Statements are allowed. INSERT statement, including INSERT/EXEC. SELECT INTO statement. The SET IDENTITY_INSERT statement is supported.

Insert explicit values into identity columns (SET IDENTITY_INSERT). Truncate table Destruction

The SET IDENTITY_INSERT statement is not supported. Not allowed. Allowed. Automatically at the end of the Explicitly with DROP TABLE batch. statement. Automatically when session ends. (Global: also when other sessions have no

statements using table.) Last only for length of update Last for the length of the against the table variable. Uses transaction. Uses more than less than temporary tables. table variables. Stored procedure Not applicable. Creating temp table and data recompilations inserts cause procedure recompilations. Rollbacks Not affected (Data not rolled Affected (Data is rolled back). back). Statistics Optimizer cannot create any Optimizer can create statistics statistics on columns, so it on columns. Uses actual row treats table variable has having count for generation execution 1 record when creating plan. execution plans. Pass to stored procedures SQL 2008 only, with Not allowed to pass, but they predefined user-defined table are still in scope to nested type. procedures. Explicitly named objects Not allowed. Allowed, but be aware of (indexes, constraints). multi-user issues. Dynamic SQL Must declare table variable Can use temporary tables inside the dynamic SQL. created prior to calling the dynamic sql. Transactions

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy