SQL Server:阻止输出具有日期的tablename,直到今天

时间:2015-11-25 10:48:15

标签: sql sql-server

我试图从11个表中显示11个月的数据。每个表包含不同的月份数据。表名包含每个表的日期。目前,我的代码的工作方式是一个人必须输入一个日期,如201404,然后它将显示从201404年到201503年的所有月份。我需要检查一下,以便如果该人在201507年输入它由于剩余的月份尚未存在,因此仅在2015年至2015年期间抛出201507。

这是我的代码:

alter PROCEDURE stp3
     @FirstTableMonth int =0,
        @EndMonth datetime
AS

DECLARE 
            @LinkedServerName sysname = 'SERVER1',
            @left int = 0,
            @right int= 0,
            @STRING VARCHAR(6),
            @monthex int = 12,
            @yearex int,
            @yearstring int,
            @DynamicSQL nvarchar(max) = '',
            @DynamicSQL2 nvarchar(max) = '',
            @OpenQuerySql nvarchar(max),
            @Table_Name sysname,
            @Table_Name2 sysname,
            @TableMonth int,
            @TableMonth2 int ,
            @CurrentMonth int = 0,
            @NextYearMonth int = 1,
            @part2 nvarchar(max) = '',
            @TwelfthMonth int = 0;


        SET @FirstTableMonth= CAST(@FirstTableMonth AS VARCHAR(6))
        set @left = cast(left(@FirstTableMonth,4) +1   as varchar(max))
        set @right = right(@FirstTableMonth,2) - 1 
        set @monthex = 11
        set @yearex = cast(left(@FirstTableMonth,4)   as varchar(max))
        set @yearstring = case when right(@FirstTableMonth,2) = '01' then left(@FirstTableMonth,4)  + RIGHT( @monthex + LTRIM( RTRIM( right(@FirstTableMonth,2) ) ), 6 ) else @left end 
        SET @STRING  = 
            case when  right(@FirstTableMonth,2) = '01' then @yearstring
            when @right < 10 then CAST(@left AS VARCHAR(4)) + RIGHT( '0' + LTRIM( RTRIM( @right ) ), 6 ) 
            else CAST(@left AS VARCHAR(4)) + RIGHT(  LTRIM( RTRIM( @right ) ), 6 ) end 


WHILE @CurrentMonth < 11 AND @EndMonth < getdate()
BEGIN

    SELECT  @TableMonth =   CASE WHEN (@FirstTableMonth + @CurrentMonth) % 100 < 13 THEN 
                                @FirstTableMonth + @CurrentMonth 
                            ELSE
                                @FirstTableMonth + 100 - (@FirstTableMonth % 100) + @NextYearMonth
                            END,
                    @NextYearMonth =    CASE WHEN (@FirstTableMonth + @CurrentMonth) % 100 < 13 THEN 
                                    @NextYearMonth
                                ELSE
                                    @NextYearMonth + 1
                                END,
                    @Table_Name =  'XX_'+CAST(@TableMonth as varchar)+'_T' ,

                    @TableMonth2 = @TableMonth,
                    @Table_Name2 = 'XX_'+CAST(@TableMonth2 as varchar)+'_T'  ,


            @DynamicSQL = @DynamicSQL + 
                                    'SELECT 
                                            *
                                         FROM '+ @Table_Name + ' 
'+ CASE WHEN @CurrentMonth < 10 THEN ' UNION ALL ' ELSE '' END ,


                  @DynamicSQL2 = @DynamicSQL2 + 
                                'SELECT 
                                        *
                                     FROM '+ @Table_Name2 + ' 
' 


        SET @CurrentMonth = @CurrentMonth + 1
        IF OBJECT_ID('tempdb..#TEMP')       IS NOT NULL BEGIN DROP TABLE #TEMP END
        IF OBJECT_ID('tempdb..#Unioned')        IS NOT NULL BEGIN DROP TABLE #Unioned END
--last months snapshot
    SELECT 
                        *
                INTO #UNIONED
            FROM OPENQUERY(SERVER1,' SELECT 
                        *
            FROM yy_T  
'
                                                            )
            union all
            SELECT 
                        *
            FROM OPENQUERY(SERVER1,' SELECT 
                        *
            FROM yk  
'
                                                                                                    )                                                                               
DECLARE @dateAsNumber VARCHAR(8);
SET @dateAsNumber = CONVERT(CHAR(6),GETDATE(), 112);
DECLARE @DateConvertedFromNumber DATETIME; 
SELECT  @DateConvertedFromNumber = CONVERT (DATETIME, CONVERT(CHAR(8), @dateAsNumber + '01'))

                select 
                            *
                            LEFT(CONVERT(VARCHAR,   DATEADD(MONTH, -1,@DateConvertedFromNumber), 112),6) AS 'Period'
                            INTO #temp
                from #Unioned


    drop table OpenQTable


--if equal to month + 99 = previous month then union 11 tables with last months snapshot 
--else 
--union 11 tables with 12th table
SET @OpenQuerySql = 'IF  (' + cast(@FirstTableMonth as varchar) + '+99) = CONVERT(nvarchar(6), dateadd(month,-1,GETDATE()), 112)
    BEGIN
                    SELECT *
            FROM OPENQUERY(['+ @LinkedServerName +'], '''+ @DynamicSQL + ''' ) 
    UNION ALL 
                SELECT *
    FROM #temp
    END
    else 
    begin 
                SELECT *
                FROM OPENQUERY(['+ @LinkedServerName +'], '''+ @DynamicSQL + ''' ) 
    UNION ALL 
                SELECT *
                FROM OPENQUERY(['+ @LinkedServerName +'], '''+ @DynamicSQL2 + ''' ) 
    end
'
END

insert into  OpenQTable
EXECUTE sp_executesql @OpenQuerySql 

go

}

0 个答案:

没有答案