Hi @Jonathan Brotto
The SELECT INTO statement creates a new table from the result set of a query. The columns of the new table inherit their names, data types, nullability, and IDENTITY property from the query’s result set.
SELECT INTO doesn’t copy constraints, indexes, or triggers from the query’s source. If you need the results in a table with the same indexes, constraints, and triggers as the source, you have to add them afterward.
Remember that the SELECT INTO statement both creates the target table (DDL) and populates it with the result set produced by the query (DML).
I often use SELECT INTO to create temporary tables for intermediate results. And sometimes use SELECT INTO to create backup copies of tables or to archive records.
For example,
SELECT * INTO Persons_backup FROM Persons
If you need a quick and dirty empty copy of some table, SELECT INTO allows you to obtain such a copy very simply. You don’t have to script the CREATE TABLE statement and change the table’s name—you just need to issue the following statement:
SELECT * INTO target_table FROM source_table WHERE 1 = 2;
Suppose you want to insert the result set of a stored procedure or a dynamic batch into a new table, but you don’t know what table structure you need to create. You can use a SELECT INTO statement, specifying OPENQUERY in the FROM clause, referring to your own server as if it were a linked server:
EXEC sp_serveroption <your_sever> , 'data access', true;
SELECT * INTO <target_table>
FROM OPENQUERY (<your_sever>, 'EXEC { <proc_name> | (<dynamic_batch>)}') AS O;
Best regards,
LiHong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.