Insert Data Into Temp Table with Query

后端 未结 8 1955
春和景丽
春和景丽 2020-12-12 12:43

I have an existing query that outputs current data, and I would like to insert it into a Temp table, but am having some issues doing so. Would anybody have some insight on h

相关标签:
8条回答
  • 2020-12-12 13:12
    SELECT * INTO #TempTable 
    FROM SampleTable
    WHERE...
    
    SELECT * FROM #TempTable
    DROP TABLE #TempTable
    
    0 讨论(0)
  • 2020-12-12 13:15

    Fastest way to do this is using "SELECT INTO" command e.g.

    SELECT * INTO #TempTableName
    FROM....
    

    This will create a new table, you don't have to create it in advance.

    0 讨论(0)
  • 2020-12-12 13:15

    Try this:

    SELECT *
    INTO #Temp
    FROM 
    (select * from tblorders where busidate ='2016-11-24' and locationID=12
    ) as X
    

    Please use alias with x so it will not failed the script and result.

    0 讨论(0)
  • 2020-12-12 13:16

    You can do that like this:

    INSERT INTO myTable (colum1, column2)
    SELECT column1, column2 FROM OtherTable;
    

    Just make sure the columns are matching, both in number as in datatype.

    0 讨论(0)
  • 2020-12-12 13:19
    SELECT *
    INTO #Temp
    FROM
    
      (SELECT
         Received,
         Total,
         Answer,
         (CASE WHEN application LIKE '%STUFF%' THEN 'MORESTUFF' END) AS application
       FROM
         FirstTable
       WHERE
         Recieved = 1 AND
         application = 'MORESTUFF'
       GROUP BY
         CASE WHEN application LIKE '%STUFF%' THEN 'MORESTUFF' END) data
    WHERE
      application LIKE
        isNull(
          '%MORESTUFF%',
          '%')
    
    0 讨论(0)
  • 2020-12-12 13:20

    SQL Server R2 2008 needs the AS clause as follows:

    SELECT * 
    INTO #temp
    FROM (
        SELECT col1, col2
        FROM table1
    ) AS x
    

    The query failed without the AS x at the end.


    EDIT

    It's also needed when using SS2016, had to add as t to the end.

     Select * into #result from (SELECT * FROM  #temp where [id] = @id) as t //<-- as t
    
    0 讨论(0)
提交回复
热议问题