How to Dump data into temp table from inserted magical table

Sudip Bhatt 2,281 Reputation points
2020-09-18T12:07:18.483+00:00
Select *
     From sourceTable st;

  Declare @outputTable Table (col1 int, col2 int, col3 int);

 ; WITH CTE AS (
    SELECT TOP (5) * FROM sourceTable WITH (READPAST)
    ORDER  BY col1
 )   
   Update Top (5)
          CTE
      Set readFlag = 1
    Output inserted.col1, inserted.col2, inserted.col3 Into @outputTable
    Where readFlag = 0

 SELECT * FROM @outputTable

How to dump data to #tmp data from inserted magical table ? i do not want to create temp table rather i want to use select * into tmp date from inserted magical table.....how is it possible ?

Thanks

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

Accepted answer
  1. Stefan Hoffmann 621 Reputation points
    2020-09-18T13:05:40.027+00:00

    You can use any table-like destination for the OUTPUT..INTO clause. It is not limited to table variables. E.g. using a table:

    USE tempdb;
    GO
    
    DROP TABLE IF EXISTS #Data;
    CREATE TABLE #Data (
        ID INT ,
        Payload INT
    );
    
    DROP TABLE IF EXISTS #Temp;
    CREATE TABLE #Temp (
        ID INT ,
        NewPayload INT ,
        OldPayload INT
    );
    
    INSERT INTO #Data ( ID ,
                        Payload )
    VALUES ( 1, 1 );
    
    UPDATE #Data
    SET    Payload += 1
    OUTPUT Inserted.ID ,
           Inserted.Payload ,
           Deleted.Payload
    INTO #Temp;
    
    SELECT *
    FROM   #Data D;
    
    SELECT *
    FROM   #Temp T;
    

0 additional answers

Sort by: Most helpful

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.