在临时表中插入多个值到一个表中

3

我有一个临时表叫做#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个回答

7

试试这个:

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

3

尝试使用SELECT语句。

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

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

0

这是另一种做法,

select id, 1001 as GroupID
into IQGroups 
from #tempIQ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接