我有一个带有以下值的临时表
@TempTable
ID
1
2
对于临时表中的每个ID,我想执行以下操作:
insert into ItemOrganisationSources
select ByLineID, <TempTable.ID>, SourceTypeID
from ItemOrganisationSources
where ItemOrganisationID in (
select ID from @TempTable)
答案 0 :(得分:1)
从您的代码中不清楚您是否有临时表(前缀为'#')或表变量(前缀为'@',就像您的示例一样)。但是,无论哪种方式,您都可以像任何其他表一样引用临时表/变量。
表变量:
insert into ItemOrganisationSources
select i.ByLineID, t.ID, i.SourceTypeID
from ItemOrganisationSources AS i
inner join @TempTable AS t
on i.ID = t.ID --Plug in the appropriate field to join on here
where i.ItemOrganisationID in (
select ID from @itemOrganisationIDsToBeDuplicated)
临时表:
insert into ItemOrganisationSources
select i.ByLineID, t.ID, i.SourceTypeID
from ItemOrganisationSources AS i
inner join #TempTable AS t
on i.ID = t.ID --Plug in the appropriate field to join on here
where i.ItemOrganisationID in (
select ID from @itemOrganisationIDsToBeDuplicated)
再次阅读你的问题后,我不完全确定你是否正在尝试将临时表加入其他数据,或者只是为临时表中的每个ID插入一次。如果是后者,这可能是CROSS JOIN
:
insert into ItemOrganisationSources
select i.ByLineID, t.ID, i.SourceTypeID
from ItemOrganisationSources AS i
cross join #TempTable AS t --cross join instead of inner join
where i.ItemOrganisationID in (
select ID from @itemOrganisationIDsToBeDuplicated)
答案 1 :(得分:0)
insert into ItemOrganisationSources
select ByLineID, TT.ID, SourceTypeID
from ItemOrganisationSources IO JOIN
@TempTable TT ON TT.ID = IO.ItemOrganisationID