Using Temporary Tables in Stored Procedures

Temporary tables are commonly used in stored procedures when intermediate results need to be stored in a work table for additional processing. Local temporary tables created in a stored procedure are automatically dropped when the stored procedure exits. Global temporary tables created in a stored procedure still exist after the stored procedure exits until they are explicitly dropped (see Listing 46.13) or the user session in which they were created disconnects from SQL Server.

LISTING 46.13 Using Local and Global Temporary Tables in Stored Procedures

use AdventureWorks2012goset nocount ongoif object_id('temp_test2') is not null    drop proc temp_test2goif exists (select ...

Get Microsoft® SQL Server 2012 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.