DF106: Temporary table is used before having rows inserted into it.

Last modified: December 25, 2024

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

Category

EXECUTION RULES

Message

Temporary table is used before having rows inserted into it.

Description

It is recommended to ensure that the table has rows inserted before referencing it as a source table.

Additional information

Referencing a temporary table before inserting rows may result in querying or joining against an empty table, leading to unexpected or inaccurate results. To avoid this, it’s recommended to ensure that a temporary table has rows inserted into it before referencing it as a data source in subsequent queries or operations.

Noncompliant code example

DECLARE @Result TABLE(Id INT)
 
IF EXISTS(SELECT * FROM @result) SELECT 'Data found'

Compliant solution

DECLARE @Result TABLE(Id INT)
INSERT INTO @Result(Id) SELECT Id FROM dbo.DemoTable WHERE Code = 'A'
IT EXISTS(SELECT * FROM @result) SELECT 'Data found'