DF001: The SELECT…INTO statement is used to create a table.

Last modified: December 25, 2024

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

Category

BEST PRACTICE

Message

The SELECT…INTO statement is used to create a table.

Description

It is not recommended to use the SELECT…INTO statement to create a table. Instead, create the table manually using the CREATE TABLE statement.

Additional information

The SELECT…INTO statement first creates a new table and then inserts rows into it. If the insertion operation fails or encounters an error, the inserted rows will be undone, and as a result, the new table will remain empty. If you want the entire operation to succeed or fail as a whole, consider using an explicit transaction.

Noncompliant code example

SELECT FirstName, LastName INTO #Customers FROM dbo.Customers

Compliant solution

CREATE TABLE #Customers(FirstName nvarchar(128), LastName nvarchar(128))
INSERT INTO #Customers (FirstName, LastName)
SELECT FirstName, LastName FROM dbo.Customers