DF036: Consider using a table variable instead of a temporary table.

Last modified: December 25, 2024

The topic describes the DF036 T-SQL code analysis rule.

Category

BEST PRACTICE

Message

Consider using a table variable instead of a temporary table.

Description

It is recommended to use a table variable instead of a temporary table. Table variables are easier to work with, more secure, and cause fewer recompilations.

Additional information

Table variables are easier to work with, as they behave similarly to regular tables and do not require explicit dropping after use. They are also considered more secure, as their scope is limited to the current session, reducing the risk of conflicts or data leakage. Additionally, using table variables can lead to fewer recompilations compared to temporary tables, resulting in better query performance.

Noncompliant code example

CREATE TABLE #ids (id INT NOT NULL);

Compliant solution

DECLARE @ids TABLE (id INT NOT NULL);