在创建临时表并像这样声明数据类型之后;

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

然后,我如何插入已经在数据库中的物理表上持有的相关数据?


当前回答

创建临时表后,只需执行普通的INSERT INTO () SELECT FROM

INSERT INTO #TempTable (id, Date, Name)
SELECT t.id, t.Date, t.Name
FROM yourTable t

其他回答

我在SQL Server中插入的方法。此外,我通常检查是否存在临时表。

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

SELECT b.Val as 'bVals'
  INTO #MyTable
FROM OtherTable as b
INSERT INTO #TempTable(ID, Date, Name)
SELECT OtherID, OtherDate, OtherName FROM PhysicalTable
SELECT * 
INTO #TempTable
FROM table

正确的查询:

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
insert #temptable
select idfield, datefield, namefield from yourrealtable