我有一个包含以下值的临时表
@TempTable
ID
1
2
对于临时表中的每个 ID,我要执行以下操作:
insert into ItemOrganisationSources
select ByLineID, <TempTable.ID>, SourceTypeID
from ItemOrganisationSources
where ItemOrganisationID in (
select ID from @TempTable)
我有一个包含以下值的临时表
@TempTable
ID
1
2
对于临时表中的每个 ID,我要执行以下操作:
insert into ItemOrganisationSources
select ByLineID, <TempTable.ID>, SourceTypeID
from ItemOrganisationSources
where ItemOrganisationID in (
select ID from @TempTable)
从您的代码中不清楚您是否有临时表(以“#”为前缀)或表变量(以“@”为前缀,如您的示例)。不过,无论哪种方式,您都可以像引用任何其他表一样引用临时表/变量。
表变量:
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)
insert into ItemOrganisationSources
select ByLineID, TT.ID, SourceTypeID
from ItemOrganisationSources IO JOIN
@TempTable TT ON TT.ID = IO.ItemOrganisationID