DECLARE drecordcursor CURSOR for
select [DR_ID], [C_ID],[D_ID],[StartTime],[EndTime] from [DriverRecord_tbl] where [IsEnd]=1 and [IsStatistics]=0 and [DR_ID]=4
open drecordcursor
declare @id int
declare @c_id int
declare @d_id int
declare @startime datetime
declare @endtime datetime
fetch next from drecordcursor into @id,@c_id,@d_id,@startime,@endtime
while @@FETCH_STATUS =0
begin
declare @sql nvarchar(max)
set @sql=''
declare @K_DriverTravelTime [decimal](18, 2)
declare @K_Oil [decimal](18, 2)
declare @K_Mileage [decimal](18, 2)
declare @K_Action_20 [decimal](18, 2)
declare @K_Action_21 [decimal](18, 2)
declare @K_Action_0 int
declare @K_Action_1 int
declare @K_Action_2 int
declare @K_Action_3 int
declare @K_Action_6 int
declare @K_Action_7 int
declare @K_Action_9 int
declare @K_Alarm_13 int
declare @K_Alarm_14 int
declare @K_Alarm_15 int
declare @K_Alarm_16 int
declare @datatime datetime
--分割datetime
declare @t1 datetime
declare @t2 datetime
set @t1 =@startime
set @t2 =convert(datetime, convert(varchar(10),dateadd(day,1,@startime) ,120) )
while @t2<@endtime
begin
--行程
select @K_DriverTravelTime=sum([Totaltraveltime]), @K_Mileage=sum([TotalMileage]),@K_Oil=sum([CumulativeOil])
,@K_Action_20=sum([HotCarLength]), @K_Action_21=sum([SuspendedIdling]) from [CarTrackRecording_tbl]
where C_ID =@c_id and [EndDatetime] between @t1 and @t2
--报警
select @K_Alarm_13=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =13 and createtime between @t1 and @t2 group by C_id, asr_typeid
select @K_Alarm_14=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =14 and createtime between @t1 and @t2 group by C_id, asr_typeid
select @K_Alarm_15=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =15 and createtime between @t1 and @t2 group by C_id, asr_typeid
select @K_Alarm_16=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =16 and createtime between @t1 and @t2 group by C_id, asr_typeid
--动作
select @K_Action_0=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =0 and [DHR_Datetim] between @t1 and @t2 group by c_id ,[DA_ID]
select @K_Action_1=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =1 and [DHR_Datetim] between @t1 and @t2 group by c_id ,[DA_ID]
select @K_Action_2=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =2 and [DHR_Datetim] between @t1 and @t2 group by c_id ,[DA_ID]
select @K_Action_3=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =3 and [DHR_Datetim] between @t1 and @t2 group by c_id ,[DA_ID]
select @K_Action_6=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =6 and [DHR_Datetim] between @t1 and @t2 group by c_id ,[DA_ID]
select @K_Action_7=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =7 and [DHR_Datetim] between @t1 and @t2 group by c_id ,[DA_ID]
select @K_Action_9=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =9 and [DHR_Datetim] between @t1 and @t2 group by c_id ,[DA_ID]
set @datatime=convert(datetime, convert(varchar(10),@t1, 120) )
--创建sql 语句
set @sql = @sql + ' if exists (select 1 from DriverKpiData_tbl where D_ID='+@d_id + ' K_DateTime = '''+ @datatime +''') '
+ ' begin '
+ ' update DriverKpiData_tbl '
+ ' set K_DriverTravelTime = K_DriverTravelTime +' + @K_DriverTravelTime
+ ' set K_Oil=K_Oil+' +@K_Oil
+ ' set K_Mileage=K_Mileage+' +@K_Mileage
+ ' set K_Action_0=K_Action_0+' +@K_Action_0
+ ' set K_Action_1=K_Action_1+' +@K_Action_1
+ ' set K_Action_2=K_Action_2+' +@K_Action_2
+ ' set K_Action_3=K_Action_3+' +@K_Action_3
+ ' set K_Action_6=K_Action_6+' +@K_Action_6
+ ' set K_Action_7=K_Action_7+' +@K_Action_7
+ ' set K_Action_9=K_Action_9+' +@K_Action_9
+ ' set K_Alarm_13=K_Alarm_13+' +@K_Alarm_13
+ ' set K_Alarm_14=K_Alarm_14+' +@K_Alarm_14
+ ' set K_Alarm_15=K_Alarm_15+' +@K_Alarm_15
+ ' set K_Alarm_16=K_Alarm_16+' +@K_Alarm_16
+ ' where D_ID='+@d_id + ' and K_DateTime='''+ @datatime + ''''
+ ' end '
+ ' else '
+ ' begin '
+ ' insert into DriverKpiData_tbl(C_ID,K_DriverTravelTime,K_Oil,K_Mileage,K_Action_0,K_Action_1,K_Action_2,K_Action_3,K_Action_6,K_Action_7,K_Action_9,K_Alarm_13,K_Alarm_14,K_Alarm_15,K_Alarm_16,K_DateTime) '
+ ' VALUES('+@c_id+','+@K_DriverTravelTime+','+@K_Oil+','+@K_Mileage+','+@K_Action_0+','+@K_Action_1+','+@K_Action_2+','+@K_Action_3+','+@K_Action_6+','+@K_Action_7+','+@K_Action_9+','+@K_Alarm_13+','+@K_Alarm_14+','+@K_Alarm_15+','+@K_Alarm_16+','''+@datatime+''')'
+ ' end '
set @t1 = @t2
set @t2 = dateadd(day,1,@t1)
end
--补充末尾的
declare @t datetime
set @t= convert(datetime, convert(varchar(10),@endtime ,120) )
--行程
select @K_DriverTravelTime=sum([Totaltraveltime]), @K_Mileage=sum([TotalMileage]),@K_Oil=sum([CumulativeOil])
,@K_Action_20=sum([HotCarLength]), @K_Action_21=sum([SuspendedIdling]) from [CarTrackRecording_tbl]
where C_ID =@c_id and [EndDatetime] between @t and @endtime
--报警
select @K_Alarm_13=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =13 and createtime between @t and @endtime group by C_id, asr_typeid
select @K_Alarm_14=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =14 and createtime between @t and @endtime group by C_id, asr_typeid
select @K_Alarm_15=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =15 and createtime between @t and @endtime group by C_id, asr_typeid
select @K_Alarm_16=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =16 and createtime between @t and @endtime group by C_id, asr_typeid
--动作
select @K_Action_0=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =0 and [DHR_Datetim] between @t and @endtime group by c_id ,[DA_ID]
select @K_Action_1=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =1 and [DHR_Datetim] between @t and @endtime group by c_id ,[DA_ID]
select @K_Action_2=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =2 and [DHR_Datetim] between @t and @endtime group by c_id ,[DA_ID]
select @K_Action_3=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =3 and [DHR_Datetim] between @t and @endtime group by c_id ,[DA_ID]
select @K_Action_6=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =6 and [DHR_Datetim] between @t and @endtime group by c_id ,[DA_ID]
select @K_Action_7=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =7 and [DHR_Datetim] between @t and @endtime group by c_id ,[DA_ID]
select @K_Action_9=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =9 and [DHR_Datetim] between @t and @endtime group by c_id ,[DA_ID]
set @datatime=@t
--创建sql 语句
set @sql = @sql + ' if exists (select 1 from DriverKpiData_tbl where D_ID='+@d_id + ' K_DateTime = '''+ @datatime +''') '
+ ' begin '
+ ' update DriverKpiData_tbl '
+ ' set K_DriverTravelTime = K_DriverTravelTime +' + @K_DriverTravelTime
+ ' set K_Oil=K_Oil+' +@K_Oil
+ ' set K_Mileage=K_Mileage+' +@K_Mileage
+ ' set K_Action_0=K_Action_0+' +@K_Action_0
+ ' set K_Action_1=K_Action_1+' +@K_Action_1
+ ' set K_Action_2=K_Action_2+' +@K_Action_2
+ ' set K_Action_3=K_Action_3+' +@K_Action_3
+ ' set K_Action_6=K_Action_6+' +@K_Action_6
+ ' set K_Action_7=K_Action_7+' +@K_Action_7
+ ' set K_Action_9=K_Action_9+' +@K_Action_9
+ ' set K_Alarm_13=K_Alarm_13+' +@K_Alarm_13
+ ' set K_Alarm_14=K_Alarm_14+' +@K_Alarm_14
+ ' set K_Alarm_15=K_Alarm_15+' +@K_Alarm_15
+ ' set K_Alarm_16=K_Alarm_16+' +@K_Alarm_16
+ ' where D_ID='+@d_id + ' and K_DateTime='''+ @datatime + ''''
+ ' end '
+ ' else '
+ ' begin '
+ ' insert into DriverKpiData_tbl(C_ID,K_DriverTravelTime,K_Oil,K_Mileage,K_Action_0,K_Action_1,K_Action_2,K_Action_3,K_Action_6,K_Action_7,K_Action_9,K_Alarm_13,K_Alarm_14,K_Alarm_15,K_Alarm_16,K_DateTime) '
+ ' VALUES('+@c_id+','+@K_DriverTravelTime+','+@K_Oil+','+@K_Mileage+','+@K_Action_0+','+@K_Action_1+','+@K_Action_2+','+@K_Action_3+','+@K_Action_6+','+@K_Action_7+','+@K_Action_9+','+@K_Alarm_13+','+@K_Alarm_14+','+@K_Alarm_15+','+@K_Alarm_16+','''+@datatime+''')'
+ ' end '
--事务添加
BEGIN TRY
BEGIN TRANSACTION
--/插入需要事务执行的代码
exec @sql
update [DriverRecord_tbl] set IsStatistics=1 where DR_ID=@ID
COMMIT TRANSACTION
END TRY
BEGIN CATCH
--SET @error=ERROR_PROCEDURE()
--RAISERROR(@error,16,1)
ROLLBACK TRANSACTION
END CATCH
print '----------------------------'
fetch next from drecordcursor into @id,@c_id,@d_id,@startime,@endtime
print @id
end
close drecordcursor
DEALLOCATE drecordcursor
DECLARE drecordcursor CURSOR for
select [DR_ID], [C_ID],[D_ID],[StartTime],[EndTime] from [DriverRecord_tbl] where [IsEnd]=1 and [IsStatistics]=0 and [DR_ID]=4
open drecordcursor
declare @id int
declare @c_id int
declare @d_id int
declare @startime datetime
declare @endtime datetime
fetch next from drecordcursor into @id,@c_id,@d_id,@startime,@endtime
while @@FETCH_STATUS =0
begin
declare @sql nvarchar(max)
set @sql=''
declare @K_DriverTravelTime [decimal](18, 2)
declare @K_Oil [decimal](18, 2)
declare @K_Mileage [decimal](18, 2)
declare @K_Action_20 [decimal](18, 2)
declare @K_Action_21 [decimal](18, 2)
declare @K_Action_0 int
declare @K_Action_1 int
declare @K_Action_2 int
declare @K_Action_3 int
declare @K_Action_6 int
declare @K_Action_7 int
declare @K_Action_9 int
declare @K_Alarm_13 int
declare @K_Alarm_14 int
declare @K_Alarm_15 int
declare @K_Alarm_16 int
declare @datatime datetime
--分割datetime
declare @t1 datetime
declare @t2 datetime
set @t1 =@startime
set @t2 =convert(datetime, convert(varchar(10),dateadd(day,1,@startime) ,120) )
while @t2<@endtime
begin
--行程
select @K_DriverTravelTime=sum([Totaltraveltime]), @K_Mileage=sum([TotalMileage]),@K_Oil=sum([CumulativeOil])
,@K_Action_20=sum([HotCarLength]), @K_Action_21=sum([SuspendedIdling]) from [CarTrackRecording_tbl]
where C_ID =@c_id and [EndDatetime] between @t1 and @t2
--报警
select @K_Alarm_13=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =13 and createtime between @t1 and @t2 group by C_id, asr_typeid
select @K_Alarm_14=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =14 and createtime between @t1 and @t2 group by C_id, asr_typeid
select @K_Alarm_15=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =15 and createtime between @t1 and @t2 group by C_id, asr_typeid
select @K_Alarm_16=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =16 and createtime between @t1 and @t2 group by C_id, asr_typeid
--动作
select @K_Action_0=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =0 and [DHR_Datetim] between @t1 and @t2 group by c_id ,[DA_ID]
select @K_Action_1=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =1 and [DHR_Datetim] between @t1 and @t2 group by c_id ,[DA_ID]
select @K_Action_2=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =2 and [DHR_Datetim] between @t1 and @t2 group by c_id ,[DA_ID]
select @K_Action_3=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =3 and [DHR_Datetim] between @t1 and @t2 group by c_id ,[DA_ID]
select @K_Action_6=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =6 and [DHR_Datetim] between @t1 and @t2 group by c_id ,[DA_ID]
select @K_Action_7=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =7 and [DHR_Datetim] between @t1 and @t2 group by c_id ,[DA_ID]
select @K_Action_9=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =9 and [DHR_Datetim] between @t1 and @t2 group by c_id ,[DA_ID]
set @datatime=convert(datetime, convert(varchar(10),@t1, 120) )
--创建sql 语句
set @sql = @sql + ' if exists (select 1 from DriverKpiData_tbl where D_ID='+@d_id + ' K_DateTime = '''+ @datatime +''') '
+ ' begin '
+ ' update DriverKpiData_tbl '
+ ' set K_DriverTravelTime = K_DriverTravelTime +' + @K_DriverTravelTime
+ ' set K_Oil=K_Oil+' +@K_Oil
+ ' set K_Mileage=K_Mileage+' +@K_Mileage
+ ' set K_Action_0=K_Action_0+' +@K_Action_0
+ ' set K_Action_1=K_Action_1+' +@K_Action_1
+ ' set K_Action_2=K_Action_2+' +@K_Action_2
+ ' set K_Action_3=K_Action_3+' +@K_Action_3
+ ' set K_Action_6=K_Action_6+' +@K_Action_6
+ ' set K_Action_7=K_Action_7+' +@K_Action_7
+ ' set K_Action_9=K_Action_9+' +@K_Action_9
+ ' set K_Alarm_13=K_Alarm_13+' +@K_Alarm_13
+ ' set K_Alarm_14=K_Alarm_14+' +@K_Alarm_14
+ ' set K_Alarm_15=K_Alarm_15+' +@K_Alarm_15
+ ' set K_Alarm_16=K_Alarm_16+' +@K_Alarm_16
+ ' where D_ID='+@d_id + ' and K_DateTime='''+ @datatime + ''''
+ ' end '
+ ' else '
+ ' begin '
+ ' insert into DriverKpiData_tbl(C_ID,K_DriverTravelTime,K_Oil,K_Mileage,K_Action_0,K_Action_1,K_Action_2,K_Action_3,K_Action_6,K_Action_7,K_Action_9,K_Alarm_13,K_Alarm_14,K_Alarm_15,K_Alarm_16,K_DateTime) '
+ ' VALUES('+@c_id+','+@K_DriverTravelTime+','+@K_Oil+','+@K_Mileage+','+@K_Action_0+','+@K_Action_1+','+@K_Action_2+','+@K_Action_3+','+@K_Action_6+','+@K_Action_7+','+@K_Action_9+','+@K_Alarm_13+','+@K_Alarm_14+','+@K_Alarm_15+','+@K_Alarm_16+','''+@datatime+''')'
+ ' end '
set @t1 = @t2
set @t2 = dateadd(day,1,@t1)
end
--补充末尾的
declare @t datetime
set @t= convert(datetime, convert(varchar(10),@endtime ,120) )
--行程
select @K_DriverTravelTime=sum([Totaltraveltime]), @K_Mileage=sum([TotalMileage]),@K_Oil=sum([CumulativeOil])
,@K_Action_20=sum([HotCarLength]), @K_Action_21=sum([SuspendedIdling]) from [CarTrackRecording_tbl]
where C_ID =@c_id and [EndDatetime] between @t and @endtime
--报警
select @K_Alarm_13=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =13 and createtime between @t and @endtime group by C_id, asr_typeid
select @K_Alarm_14=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =14 and createtime between @t and @endtime group by C_id, asr_typeid
select @K_Alarm_15=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =15 and createtime between @t and @endtime group by C_id, asr_typeid
select @K_Alarm_16=count(1) from AlarmSpecialRecords_tbl where C_id=@c_id and asr_typeid =16 and createtime between @t and @endtime group by C_id, asr_typeid
--动作
select @K_Action_0=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =0 and [DHR_Datetim] between @t and @endtime group by c_id ,[DA_ID]
select @K_Action_1=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =1 and [DHR_Datetim] between @t and @endtime group by c_id ,[DA_ID]
select @K_Action_2=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =2 and [DHR_Datetim] between @t and @endtime group by c_id ,[DA_ID]
select @K_Action_3=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =3 and [DHR_Datetim] between @t and @endtime group by c_id ,[DA_ID]
select @K_Action_6=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =6 and [DHR_Datetim] between @t and @endtime group by c_id ,[DA_ID]
select @K_Action_7=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =7 and [DHR_Datetim] between @t and @endtime group by c_id ,[DA_ID]
select @K_Action_9=count(1) from [DrivingHabitsRecords_tbl] where C_id=@c_id and [DA_ID] =9 and [DHR_Datetim] between @t and @endtime group by c_id ,[DA_ID]
set @datatime=@t
--创建sql 语句
set @sql = @sql + ' if exists (select 1 from DriverKpiData_tbl where D_ID='+@d_id + ' K_DateTime = '''+ @datatime +''') '
+ ' begin '
+ ' update DriverKpiData_tbl '
+ ' set K_DriverTravelTime = K_DriverTravelTime +' + @K_DriverTravelTime
+ ' set K_Oil=K_Oil+' +@K_Oil
+ ' set K_Mileage=K_Mileage+' +@K_Mileage
+ ' set K_Action_0=K_Action_0+' +@K_Action_0
+ ' set K_Action_1=K_Action_1+' +@K_Action_1
+ ' set K_Action_2=K_Action_2+' +@K_Action_2
+ ' set K_Action_3=K_Action_3+' +@K_Action_3
+ ' set K_Action_6=K_Action_6+' +@K_Action_6
+ ' set K_Action_7=K_Action_7+' +@K_Action_7
+ ' set K_Action_9=K_Action_9+' +@K_Action_9
+ ' set K_Alarm_13=K_Alarm_13+' +@K_Alarm_13
+ ' set K_Alarm_14=K_Alarm_14+' +@K_Alarm_14
+ ' set K_Alarm_15=K_Alarm_15+' +@K_Alarm_15
+ ' set K_Alarm_16=K_Alarm_16+' +@K_Alarm_16
+ ' where D_ID='+@d_id + ' and K_DateTime='''+ @datatime + ''''
+ ' end '
+ ' else '
+ ' begin '
+ ' insert into DriverKpiData_tbl(C_ID,K_DriverTravelTime,K_Oil,K_Mileage,K_Action_0,K_Action_1,K_Action_2,K_Action_3,K_Action_6,K_Action_7,K_Action_9,K_Alarm_13,K_Alarm_14,K_Alarm_15,K_Alarm_16,K_DateTime) '
+ ' VALUES('+@c_id+','+@K_DriverTravelTime+','+@K_Oil+','+@K_Mileage+','+@K_Action_0+','+@K_Action_1+','+@K_Action_2+','+@K_Action_3+','+@K_Action_6+','+@K_Action_7+','+@K_Action_9+','+@K_Alarm_13+','+@K_Alarm_14+','+@K_Alarm_15+','+@K_Alarm_16+','''+@datatime+''')'
+ ' end '
--事务添加
BEGIN TRY
BEGIN TRANSACTION
--/插入需要事务执行的代码
exec @sql
update [DriverRecord_tbl] set IsStatistics=1 where DR_ID=@ID
COMMIT TRANSACTION
END TRY
BEGIN CATCH
--SET @error=ERROR_PROCEDURE()
--RAISERROR(@error,16,1)
ROLLBACK TRANSACTION
END CATCH
print '----------------------------'
fetch next from drecordcursor into @id,@c_id,@d_id,@startime,@endtime
print @id
end
close drecordcursor
DEALLOCATE drecordcursor