Partilhar via


Criar e aceder a tabelas no TempDB a partir de Procedimentos Armazenados

Aplica-se a:SQL ServerBanco de Dados SQL do AzureInstância Gerenciada SQL do Azure

Criar e aceder a tabelas no TempDB a partir de procedimentos armazenados compilados nativamente não é suportado. Em vez disso, utilize tabelas otimizadas para memória, cujo atributo de DURABILIDADE está configurado como SCHEMA_ONLY; ou opte por usar tipos de tabela e variáveis de tabela.

Para mais informações sobre otimização de memória em cenários de tabelas temporárias e variáveis de tabela, veja: Tabela temporária mais rápida e variável de tabela usando otimização de memória.

O exemplo seguinte mostra como o uso de uma tabela temporária com três colunas (ID, ProductID, Quantity) pode ser substituído por uma variável de tabela @OrderQuantityByProduct do tipo dbo. OrdemQuantidadePorProduto:

CREATE TYPE dbo.OrderQuantityByProduct   
  AS TABLE   
   (id INT NOT NULL PRIMARY KEY NONCLUSTERED HASH WITH (BUCKET_COUNT=100000),   
    ProductID INT NOT NULL,   
    Quantity INT NOT NULL) WITH (MEMORY_OPTIMIZED=ON)  
GO  
CREATE PROCEDURE dbo.usp_OrderQuantityByProduct   
WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER  
AS BEGIN ATOMIC WITH   
(  
    TRANSACTION ISOLATION LEVEL = SNAPSHOT,  
    LANGUAGE = N'ENGLISH'  
)  
  -- declare table variables for the list of orders   
  DECLARE @OrderQuantityByProduct dbo.OrderQuantityByProduct  
  
  -- populate input  
  INSERT @OrderQuantityByProduct SELECT ProductID, Quantity FROM dbo.[Order Details]  
  end  

Ver também

Problemas de Migração para Procedimentos Armazenados Compilados Nativamente
Transact-SQL construções não suportadas pelo OLTP In-Memory