240

After having created a temporary table and declaring the data types like so;

CREATE TABLE #TempTable(
ID int,
Date datetime,
Name char(20))

How do I then insert the relevant data which is already held on a physical table within the database?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
William
  • 6,332
  • 8
  • 38
  • 57

13 Answers13

290
INSERT INTO #TempTable (ID, Date, Name) 
SELECT id, date, name 
FROM physical_table
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
alexsuslin
  • 4,130
  • 1
  • 20
  • 30
138

To insert all data from all columns, just use this:

SELECT * INTO #TempTable
FROM OriginalTable

Don't forget to DROP the temporary table after you have finished with it and before you try creating it again:

DROP TABLE #TempTable
Sheridan
  • 68,826
  • 24
  • 143
  • 183
80
SELECT  ID , Date , Name into #temp from [TableName]
Abdul Saboor
  • 4,079
  • 2
  • 33
  • 25
49

My way of Insert in SQL Server. Also I usually check if a temporary table exists.

IF OBJECT_ID('tempdb..#MyTable') IS NOT NULL DROP Table #MyTable

SELECT b.Val as 'bVals'
  INTO #MyTable
FROM OtherTable as b
Beshoy Hanna
  • 611
  • 2
  • 9
  • 29
maxim
  • 561
  • 4
  • 13
23

I have provided two approaches to solve the same issue,

Solution 1: This approach includes 2 steps, first create a temporary table with specified data type, next insert the value from the existing data table.

CREATE TABLE #TempStudent(tempID  int, tempName  varchar(MAX) )
INSERT INTO #TempStudent(tempID, tempName) SELECT id, studName FROM students where id =1

SELECT * FROM #TempStudent

Solution 2: This approach is simple, where you can directly insert the values to temporary table, where automatically the system take care of creating the temp table with the same data type of original table.

SELECT id, studName  INTO #TempStudent FROM students where id =1

SELECT * FROM #TempStudent
BHUVANESH MOHANKUMAR
  • 2,747
  • 1
  • 33
  • 33
Ipsita Sethi
  • 462
  • 4
  • 15
  • While I recognize that this is a temp table, I still would never recommend someone use varchar(MAX). – bp_ Feb 08 '18 at 18:43
22
SELECT * 
INTO #TempTable
FROM table
Ravi Teja Koneru
  • 502
  • 7
  • 16
9

After you create the temp table you would just do a normal INSERT INTO () SELECT FROM

INSERT INTO #TempTable (id, Date, Name)
SELECT t.id, t.Date, t.Name
FROM yourTable t
Taryn
  • 242,637
  • 56
  • 362
  • 405
8

The right query:

drop table #tmp_table

select new_acc_no, count(new_acc_no) as count1
into #tmp_table
from table
where unit_id = '0007' 
group by unit_id, new_acc_no
having count(new_acc_no) > 1
Ahsan Ahmad
  • 129
  • 1
  • 2
  • 13
    This answer has nothing to do with the question. You took this information from somewhere else. Removing `new_acc_no`, `unit_id = '0007'`, `group by`, `having count(new_acc_no) > 1`, etc transforms the answer in an exact duplicate of: http://stackoverflow.com/a/15762663/1476885 – Zanon Aug 10 '15 at 20:00
6
insert into #temptable (col1, col2, col3)
select col1, col2, col3 from othertable

Note that this is considered poor practice:

insert into #temptable 
select col1, col2, col3 from othertable

If the definition of the temp table were to change, the code could fail at runtime.

Robbie Dee
  • 1,939
  • 16
  • 43
6

Basic operation of Temporary table is given below, modify and use as per your requirements,

-- CREATE A TEMP TABLE

CREATE TABLE #MyTempEmployeeTable(tempUserID  varchar(MAX), tempUserName  varchar(MAX) )

-- INSERT VALUE INTO A TEMP TABLE

INSERT INTO #MyTempEmployeeTable(tempUserID,tempUserName) SELECT userid,username FROM users where userid =21

-- QUERY A TEMP TABLE [This will work only in same session/Instance, not in other user session instance]

SELECT * FROM #MyTempEmployeeTable

-- DELETE VALUE IN TEMP TABLE

DELETE FROM #MyTempEmployeeTable

-- DROP A TEMP TABLE

DROP TABLE #MyTempEmployeeTable
BHUVANESH MOHANKUMAR
  • 2,747
  • 1
  • 33
  • 33
  • While I recognize that this is a temp table, I still would never recommend someone use varchar(MAX). – bp_ Feb 08 '18 at 18:41
  • @bp_ This is a generalized sample snippet which explains the user and User can specify the data type and its size based on their application requirements. – BHUVANESH MOHANKUMAR Feb 08 '18 at 21:10
4
INSERT INTO #TempTable(ID, Date, Name)
SELECT OtherID, OtherDate, OtherName FROM PhysicalTable
Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
2
insert #temptable
select idfield, datefield, namefield from yourrealtable
podiluska
  • 50,950
  • 7
  • 98
  • 104
2

All the above mentioned answers will almost fullfill the purpose. However, You need to drop the temp table after all the operation on it. You can follow-

INSERT INTO #TempTable (ID, Date, Name) 
SELECT id, date, name 
FROM physical_table;

IF OBJECT_ID('tempdb.dbo.#TempTable') IS NOT NULL 
    DROP TABLE #TempTable;
Tedd Hansen
  • 12,074
  • 14
  • 61
  • 97
Prabhat Maurya
  • 1,058
  • 16
  • 21