What's the difference between a temp table and table variable in SQL Server?

后端 未结 12 1123
抹茶落季
抹茶落季 2020-11-22 05:03

In SQL Server 2005, we can create temp tables one of two ways:

declare @tmp table (Col1 int, Col2 int);

or

create table #tm         


        
12条回答
  •  盖世英雄少女心
    2020-11-22 05:39

    Another difference:

    A table var can only be accessed from statements within the procedure that creates it, not from other procedures called by that procedure or nested dynamic SQL (via exec or sp_executesql).

    A temp table's scope, on the other hand, includes code in called procedures and nested dynamic SQL.

    If the table created by your procedure must be accessible from other called procedures or dynamic SQL, you must use a temp table. This can be very handy in complex situations.

提交回复
热议问题