Skip to content

Latest commit

 

History

History
46 lines (39 loc) · 2.32 KB

create-and-access-tables-in-tempdb-from-stored-procedures.md

File metadata and controls

46 lines (39 loc) · 2.32 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic monikerRange
Create & access tempdb tables from stored procedures
TempDB doesn't support creating and accessing tables from natively compiled stored procedures. Use memory-optimized tables, or table types and table variables.
MikeRayMSFT
mikeray
03/07/2017
sql
in-memory-oltp
conceptual
=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current

Create and Access Tables in TempDB from Stored Procedures

[!INCLUDE SQL Server Azure SQL Database Azure SQL Managed Instance]

Creating and accessing tables in TempDB from natively compiled stored procedures isn't supported. Instead, use either memory-optimized tables with DURABILITY=SCHEMA_ONLY or use table types and table variables.

For more information about memory-optimization of temp table and table variable scenarios, see: Faster temp table and table variable by using memory optimization.

The following example shows how the use of a temp table with three columns (ID, ProductID, Quantity) can be replaced using a table variable @OrderQuantityByProduct of type dbo.OrderQuantityByProduct:

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  

See Also

Migration Issues for Natively Compiled Stored Procedures
Transact-SQL Constructs Not Supported by In-Memory OLTP