SQL server 实战篇(四)利用游标判定新物质

USE [Data Inspection]
GO
/****** Object:  StoredProcedure [dbo].[statistic_factor]    Script Date: 2018/8/4 15:13:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

--统计因子
ALTER proc [dbo].[statistic_factor]
@startime datetime,
@endtime datetime,
@Base_StationInfo_ID int,       -- 站房ID
@Base_InstrumentInfo_ID int     -- 设备ID
as 
begin

Declare
@UpStartTime datetime,
@UpEndTime  datetime,
@name varchar(30),
@stationName varchar(30),
@colName   varchar(10),
@DateTime datetime,                --本天最高浓度对应的时间
@CountSQL nvarchar(MAX)

set @UpStartTime = DATEADD(hour,12,@startime)
set @UpEndTime = DATEADD(hour,12,@endtime)

declare FACTOR cursor for 
select distinct monitorname from FTIR_original_data where belongDevices = 0 and area = @Base_StationInfo_ID AND stationID = @Base_InstrumentInfo_ID  and reportTime between @UpStartTime and @UpEndTime

open Factor
fetch next from Factor into @colname
while @@FETCH_STATUS = 0
    begin
        set @CountSQL =N' select @mintime = min(reporttime) from FTIR_original_data where area = @area and stationID = @InstrumentInfo_ID  and belongDevices = 0 and reportTime between @Supstarttime and @Supendtime and monitorName = @Scolname '
        exec sp_executesql @Countsql,
        @params = N'@area int,@InstrumentInfo_ID int,@Supstarttime datetime, @Supendtime DateTime, @Scolname varchar(10),@mintime datetime output',
        @area = @Base_StationInfo_ID,
        @InstrumentInfo_ID = @Base_InstrumentInfo_ID,
        @SUpEndTime = @UpEndTime,
        @Scolname = @colName,
        @Supstarttime = @UpStartTime,
        @mintime = @DateTime output

        if not exists(select *from Factor where area = @Base_StationInfo_ID and station = @Base_InstrumentInfo_ID and factor_name = @colName)
            begin
                insert into Factor values(NEWID(),@Base_StationInfo_ID,@Base_InstrumentInfo_ID,@colName,@DateTime)
            end

        fetch next from Factor into  @colname
    end
close Factor
deallocate Factor
end

猜你喜欢

转载自blog.csdn.net/qq_39001049/article/details/81411338