在临时表中插入表中的几个值

时间:2012-06-20 19:45:40

标签: sql-server tsql sql-server-2000

我在名为#tempIQ的临时表中有几个值,我想使用相同的组标识符插入名为IQGroups的表中。假设每个人都有独特的智商:

create table #tempIQ
(
id int
)

declare @GroupIDas int
set @GroupID=1001    

select iq from #tempIQ

1,2,86,99,101,165,180,201

我想将临时表中的这些ID插入名为IQGroups的分组中,但很难找到简单的解决方案。

-- now try and insert all the iqs for a group into the IQGroups table from the #tempIQ table.
  insert into IQGroups (GroupID, IQ) values (@GroupID, #tempiQ.iq) 

3 个答案:

答案 0 :(得分:7)

试试这个:

 INSERT INTO IQGroups (GroupID, IQ)
   SELECT @GroupID, IQ
   FROM #tempIQ

答案 1 :(得分:3)

尝试使用SELECT语句。

INSERT INTO IQGroups (GroupID, IQ)
SELECT @GroupID, iq
FROM #tempIQ

这是选择多行的标准方法。

答案 2 :(得分:0)

这是另一种方法,

select id, 1001 as GroupID
into IQGroups 
from #tempIQ
相关问题