单个insert
语句返回一个受影响的行数。您为“月”的每个值执行单独的插入,因此 12 个插入语句而不是 1 个。这将对性能产生一些影响。
或者,您可以加载要插入到临时表中的行并执行插入,然后报告事情,类似于以下内容:
create table #work
( month int not null ,
primary_key_of_real_table int not null ,
)
insert #work
select t.month , t.primary_key_column
from source_table t
where -- your filter criteria here
insert target_table
( ... column list ... )
select ... column list ...
from #work t
join source_table x on x.primary_key_column =t.primary_key_of_real_table
select m.month , cnt = sum(case when t.month is null then 1 else 0 end)
from ( select month = 1 union all
select month = 2 union all
...
select month = 12
) m
left join #work t on t.month = m.month
group by t.month
order by t.month