新建一个存储过程
ALTER PROCEDURE [dbo].[SP_TEST]
AS
BEGIN
DECLARE @id varchar(128)
DECLARE @totalLast bigint
DECLARE @countNum bigint
DECLARE @tmpValue DECIMAL
set @tmpValue = 0
DECLARE cur CURSOR
FOR(select ID,sum(LAST) as totalLast, count(ID) as countNum from test1 where CREATED_TIME > convert(varchar(10),getdate(),120) GROUP BY RESOURCE_ID)
OPEN cur
FETCH NEXT FROM cur INTO @id,@totalLast,@countNum
WHILE @@fetch_status=0
BEGIN
select @tmpValue = [VALUE] from [test2] where ID= @id and TERMS = ‘condition1’
if @tmpValue != 0
update [test2] set [VALUE] = @tmpValue+@totalLast,CREATED_TIME = CONVERT(varchar(100), GETDATE(), 120) where RESOURCE_ID = @id and TERMS = ‘condition1’
else
insert into [test2] values(@id,‘conditon1’,@totalLast,CONVERT(varchar(100), GETDATE(), 12