SQL server 实战篇(九) 利用动态SQL 进行单位的转换

USE [FTIR2017]
GO
/****** Object:  StoredProcedure [dbo].[Data_Ftir_UnitFix]    Script Date: 2018/8/4 16:18:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- exec Data_Ftir_UnitFix '2015-06-27','2015-09-01',1,1

-- 原始数据的单位转换
ALTER proc [dbo].[Data_Ftir_UnitFix]
    @startTime datetime,
    @endTime datetime,
    @Base_StationInfo_ID int,       -- 站房ID
    @Base_InstrumentInfo_ID int     -- 设备ID
AS
BEGIN

    Declare
        @columnName nvarchar(30),   -- 列名
        @updateSQL nvarchar(2000),  -- 更新语句

        @difftime int,          --  时间差(天):用于获取对应因子范围的中间变量
        @tableName nvarchar(max),   -- 表名集合
        @startYearInt int,      --  起始年
        @startMonthInt int,     --  起始月
        @startDayInt  int,      --  起始日

        @endYearInt int,        --  截止年
        @sendMonthInt int,      --  截止月
        @endDayInt  int,        --  截止日
        @selectSQL nvarchar(max)    --  查询赋值语句

        ;

        SELECT @difftime = DATEDIFF(DAY,@startTime,@endTime);
        SET @tableName = '''';

        -- 初始化起止时间(默认的时间规则 12:00:00)
         SELECT @startTime = DATEADD(HOUR,12,@startTime);
         SELECT @endTime = DATEADD(HOUR,12,@endTime);

        Declare @subTemp int=1

            WHILE @subTemp<=@difftime
                BEGIN

                    --时间界定
                    SELECT @startYearInt=DATEPART(YEAR,DATEADD(day,@subTemp,@startTime));
                    SELECT @startMonthInt=DATEPART(MONTH,DATEADD(day,@subTemp,@startTime));
                    SELECT @startDayInt=DATEPART(DAY,DATEADD(day,@subTemp,@startTime));

                    SET @tableName = @tableName+'CMC'+CAST(@startYearInt as varchar);
                        IF @startMonthInt>0 and @startMonthInt<10
                            BEGIN
                                SET @tableName = @tableName+'0'+CAST(@startMonthInt as varchar);
                            END
                        ELSE 
                            BEGIN
                                SET @tableName = @tableName+CAST(@startMonthInt as varchar);
                            END

                        IF @startDayInt>0 and @startDayInt<10
                            BEGIN
                                SET @tableName = @tableName+'0'+CAST(@startDayInt as varchar);
                            END
                        ELSE
                            BEGIN
                                SET @tableName = @tableName+CAST(@startDayInt as varchar);
                            END

                        SET @tableName = @tableName+''',''';
                        SET @subTemp = @subTemp+1;
                    END


            -- 去掉字符的最后一位逗号 select00 left(@str,len(@str)-1)
            SET @tableName = LEFT(@tableName,LEN(@tableName)-2);

            SET @selectSQL = 'INSERT INTO CurrentColumnNames SELECT NEWID(),colName,@inStationID,@inInstrumentID FROM OriginalData_Names WHERE Base_StationInfo_ID=@inStationID AND Base_InstrumentInfo_ID=@inInstrumentID AND tableName IN('+@tableName+') GROUP BY colName';
            EXEC sp_executesql 
                @stmt = @selectSQL,
                @params =  N'@inStationID as int,@inInstrumentID as int',
                @inStationID = @Base_StationInfo_ID,
                @inInstrumentID = @Base_InstrumentInfo_ID;
            DELETE CurrentColumnNames WHERE columnName in('Count','DateTime');

            -- 进行逐列的数据零值修复
            DECLARE Cur_Ftir_UnitFix CURSOR FOR
                SELECT columnName FROM CurrentColumnNames order by CAST(columnName AS int);
            --打开游标
            OPEN Cur_Ftir_UnitFix
                --循环游标
                FETCH NEXT FROM Cur_Ftir_UnitFix INTO @columnName
                WHILE @@FETCH_STATUS = 0
                    BEGIN


                        -- 初始化更新语句,这里作简单分类,若气象数据不为空,则按前者的公式来算;若是气象数据为空,则按后者的公式来算
                        SET @updateSQL =  '';
                        SET @updateSQL = 'Update #Temp SET ['+@columnName+'] =(m.mw * ['+@columnName+'])/22.4*(273.0/(273.0+[temperature]))*(([airPressure]*100.0)/101325.0) FROM Base_LibraryMonitor m WHERE m.gasId=@gasid and Base_StationInfo_ID=@stationID and Base_InstrumentInfo_ID = @InstrumentID and DateTime>@sTime and DateTime <=@eTime and ['+@columnName+']!=-1 and temperature is not null and airPressure is not null;
                                          Update #Temp SET ['+@columnName+'] =(m.mw * ['+@columnName+'])/22.4 FROM Base_LibraryMonitor m WHERE m.gasId=@gasid and Base_StationInfo_ID=@stationID and Base_InstrumentInfo_ID = @InstrumentID and DateTime>@sTime and DateTime <=@eTime and ['+@columnName+']!=-1 and temperature is null and airPressure is  null ';
                        EXEC sp_executesql
                            @stmt = @updateSQL,
                            @params = N'@gasid AS int,@stationID AS int,@InstrumentID AS int,@sTime AS datetime,@eTime AS datetime', 
                            @gasid = @columnName,
                            @stationID = @Base_StationInfo_ID,
                            @InstrumentID = @Base_InstrumentInfo_ID,
                            @sTime = @startTime,
                            @eTime =  @endTime
                            ;

                        --得到下一条记录
                        FETCH NEXT FROM Cur_Ftir_UnitFix INTO @columnName
                    END
            --关闭并释放游标占有的资源
            CLOSE Cur_Ftir_UnitFix
            DEALLOCATE Cur_Ftir_UnitFix

    -- 清理当前列名保存表信息
    DELETE CurrentColumnNames;

    PRINT '数据单位转换完毕';
END

猜你喜欢

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