DEV Community

Cover image for Efficient Temporary Storage in SQL: Ditching Cursors for Table Variables
Sean Drew
Sean Drew

Posted on • Edited on

Efficient Temporary Storage in SQL: Ditching Cursors for Table Variables

(A Limited-Scope Solution to Replace SQL Cursors)

Using a table variable is an excellent alternative to using a SQL cursor, especially when you need a temporary storage mechanism that has a limited scope. Table variables are defined using the DECLARE statement and can be useful for holding intermediate results in a manner similar to temporary tables. Here are some key points, advantages, and a sample use case involving table variables.

Key Differences Between Table Variables and Temporary Tables

Scope:
Table Variables: Their scope is limited to the batch, stored procedure, or function in which they are defined. Once the execution completes, the table variable is automatically dropped.

Temporary Tables: They can be scoped to a session or can be global. They exist until explicitly dropped or the session ends.

Performance:
Table Variables: They are often more lightweight than temporary tables and may result in less overhead for small datasets. However, they can be less efficient for larger datasets, as they don’t have statistics and may lead to less optimized execution plans.

Temporary Tables: They can hold larger datasets and have statistics, which helps SQL Server optimize the execution plan. They can also be indexed.

Transactions:
Table Variables: They are not affected by transaction rollbacks in the same way as temporary tables. If a batch fails, the changes made to a table variable remain.
Temporary Tables: They are affected by transactions, meaning changes can be rolled back if a transaction fails.

When to Use Table Variables

  • When the dataset is small (usually less than 1,000 rows).
  • When you need a temporary storage solution for a specific batch or scope.
  • When you want to avoid the overhead of creating and dropping a temporary table.

Sample Use Case with a Table Variable
Let's say we want to calculate a 10% salary increase for employees in a specific department and store the results in a table variable before updating the original Employees table.

Example Setup using Employees table
Image description

SQL Query Using Table Variable

declare @SalaryUpdates table
(
  employeeid int,
  newsalary decimal(10, 2)
)

-- insert the new salary calculations into the table variable
insert into @SalaryUpdates (EmployeeID, NewSalary)
select [employeeid], [salary] * 1.10
from [dbo].[employees]
where [departmentid] = 1 -- example condition to filter employees

-- update the original employees table using the table variable
update [emp]
set [salary] = [salary].[newsalary]
from [dbo].[employees] as [emp]
join @salaryupdates as [salary] on [emp].[employeeid] = [salary].[employeeid]

-- optionally, select to see the updated results
select * from [employees]
Enter fullscreen mode Exit fullscreen mode

Explanation
Table Variable Declaration:
The table variable @SalaryUpdates is declared with columns for EmployeeID and NewSalary.

Inserting Data:
The query inserts new salary values into the @SalaryUpdates table variable, calculating a 10% increase for employees in a specified department.

Updating the Original Table:
The UPDATE statement then uses a JOIN with the table variable to apply the new salaries back to the original Employees table.

Selecting Results:
Optionally, you can select from the Employees table to see the updated salaries.

Benefits of Using Table Variables
Simplicity: Table variables are simple to declare and use, making them suitable for quick calculations or small datasets.

Automatic Cleanup: No need to explicitly drop the variable; it automatically goes out of scope at the end of the batch.

Less Overhead: For small datasets, table variables may have less overhead compared to temporary tables.

Conclusion
Table variables provide a flexible and efficient way to handle temporary data within a limited scope, especially for small to moderate-sized datasets. They can help simplify code and reduce the need for explicit cleanup, making them a great alternative to cursors and temporary tables in many scenarios. However, for larger datasets or when advanced indexing and statistics are required, temporary tables may be the better choice.

Top comments (0)