select into use case situations

Jonathan Brotto 1,076 Reputation points
2022-02-15T18:49:34.453+00:00

Was wondering where would I use a select into keywords as it seems to make a view or another temp like table to get data from based on my understanding.

Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. LiHong-MSFT 10,056 Reputation points
    2022-02-16T01:54:55+00:00

    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.


3 additional answers

Sort by: Most helpful
  1. Naomi Nosonovsky 8,431 Reputation points
    2022-02-15T22:38:19.53+00:00

    I use this quite often if I need to use intermediate results but think cte may perform poorly or need to use results of the query in a stored procedure, for example, but don't want to actually define the structure of the temp table. I also often use it in ad-hoc queries.


  2. Erland Sommarskog 121.5K Reputation points MVP Volunteer Moderator
    2022-02-15T22:59:26.177+00:00

    I may use SELECT INTO for ad-hoc operations to create temp tables for intermediate result. But in application code, I prefer to create temp tables explicitly with CREATE TABLE and populate them with INSERT.

    SELECT INTO can be faster than CREATE TABLE + INSERT because it can be minimally logged. But so can CREATE TABLE + INSERT as well, if you use the TABLOCK hint. And for temp tables you should not need that hint.

    0 comments No comments

  3. Naomi Nosonovsky 8,431 Reputation points
    2022-02-16T18:55:12.81+00:00

    This may be more that you asked for, but it should show you the concept of pivoting and case expressions. In your case you need non dynamic version.

    https://social.technet.microsoft.com/wiki/contents/articles/17510.t-sql-dynamic-pivot-on-multiple-columns.aspx

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.