2012-06-20 50 views
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) 

回答

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