declare @projectid int
set @projectid=1
declare @testcenter varchar(20),@nn int,@kk int
DECLARE countp_cursor CURSOR FOR select testcenter,count(*) as nn from pinfo where projectid =@projectid group by testcenter
open countp_cursor
FETCH NEXT FROM countp_cursor INTO @testcenter,@nn
WHILE @@FETCH_STATUS = 0
BEGIN
begin
set @kk=(select count(*) from year_Statistic where testcenter =@testcenter)
end
if @kk=1 --year_Statistic表中是否已经有该中心记录
begin
update year_Statistic set papercount =@nn where testcenter=@testcenter
end
if @kk=0
begin
INSERT INTO year_Statistic (testcenter,papercount) VALUES (@testcenter,@nn)
end
FETCH NEXT FROM countp_cursor INTO @testcenter,@nn
END
CLOSE countp_cursor
DEALLOCATE countp_cursor
--请将sql语句换成你自己的,重新定义参数名称等,就可以