MS-SQL是否支持内存表?

Han*_*etz 16 mysql sql-server portability rdbms

最近,我开始将一些应用程序更改为支持MS SQL Server作为替代后端.

我遇到的一个兼容性问题是使用MySQL的CREATE TEMPORARY TABLE来创建内存表,这些表在会话期间保存数据以实现非常快速的访问,而无需永久存储.

MS SQL中的等价物是什么?

一个要求是我需要能够像使用其他临时表一样使用临时表,尤其是JOIN永久表.

Kei*_*ith 18

您可以创建表变量(在内存中)和两种不同类型的临时表:

--visible only to me, in memory (SQL 2000 and above only)
declare @test table (
    Field1 int,
    Field2 nvarchar(50)
);

--visible only to me, stored in tempDB
create table #test (
    Field1 int,
    Field2 nvarchar(50)
)

--visible to everyone, stored in tempDB
create table ##test (
    Field1 int,
    Field2 nvarchar(50)
)
Run Code Online (Sandbox Code Playgroud)

编辑:

根据反馈,我认为这需要一点澄清.

#table并且##table将永远是在tempdb.

@Table变量通常在内存中,但不保证是.SQL根据查询计划决定,并在需要时使用TempDB.


Man*_*anu 13

@Keith

这是一种常见的误解:表变量不一定存储在内存中.事实上,SQL Server决定是将变量保留在内存中还是将其溢出到TempDB.没有可靠的方法(至少在SQL Server 2005中)确保表数据保存在内存中.有关详细信息,请查看此处