你是否有一个临时这不是从你的代码清晰表(以'#'为前缀)或表变量(以您的示例为前缀)。无论哪种方式,您都可以像引用其他表一样引用临时表/变量。
表变量:
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)
你可以通过临时表中的记录通过使用游标循环。 – 2013-04-11 12:41:13
你能举一个你想要什么样的最终结果的例子吗?我以为我有这个答案,但现在我想我可能误解了你的问题。 – 2013-04-11 12:44:41
@VijaysingGusinge你应该避免游标,除非绝对必要,并首先在sql中首选基于集合的操作。 – Ryan 2013-04-11 12:45:25