调试 SSIS SQL 任务的 SQL 语句以插入日期范围

Debugging SQL statement of SSIS SQL task to insert range of date

我正在尝试使用 SQL 任务将日期范围插入日期维度 table,并将 BeginDate / EndDate 参数传递给它。但是,如果我尝试执行包,维度 table 中没有插入数据,但包执行正常。

如何获取 SQL 任务的 SQL 语句,以便识别问题?

我分别使用 02/02/2014 和 02/02/2015 作为 BeginDate 和 EndDate。

/*---------------------------------------------------------------------------*/
/* Loads a Date Dimension                                                    */
/*---------------------------------------------------------------------------*/

-- A few notes, this code does nothing to the existing table, no deletes
-- are triggered before hand. Because the DateKey is uniquely indexed,
-- it will simply produce errors if you attempt to insert duplicates.
-- You can however adjust the Begin/End dates and rerun to safely add
-- new dates to the table every year.
--
-- If the begin date is after the end date, no errors occur but nothing
-- happens as the while loop never executes.

SET NOCOUNT ON -- turn off all the 1 row inserted messages

-- Hold our dates
DECLARE @BeginDate DATETIME
DECLARE @EndDate DATETIME

-- Holds a flag so we can determine if the date is the last day of month
DECLARE @LastDayOfMon CHAR(1)

-- Number of months to add to the date to get the current Fiscal date
DECLARE @FiscalYearMonthsOffset INT   

-- These two counters are used in our loop.
DECLARE @DateCounter DATETIME    --Current date in loop
DECLARE @FiscalCounter DATETIME  --Fiscal Year Date in loop

-- Set the date to start populating and end populating
SET @BeginDate = ?
SET @EndDate = ?

-- Set this to the number of months to add to the current date to the
-- beginning get of the Fiscal year. For example, if the Fiscal year
-- begins July 1, put a 6 there.
-- Negative values are also allowed, thus if your 2010 Fiscal year
-- begins in July of 2009, put a -6.
SET @FiscalYearMonthsOffset = 6

-- Start the counter at the begin date
SET @DateCounter = @BeginDate

WHILE @DateCounter <= @EndDate
      BEGIN
            -- Calculate the current Fiscal date as an offset of
            -- the current date in the loop
            SET @FiscalCounter = DATEADD(m, @FiscalYearMonthsOffset, @DateCounter)

            -- Set value for IsLastDayOfMonth
            IF MONTH(@DateCounter) = MONTH(DATEADD(d, 1, @DateCounter))
               SET @LastDayOfMon = 'N'
            ELSE
               SET @LastDayOfMon = 'Y'  

            -- add a record into the date dimension table for this date
            INSERT  INTO [dbo].[DimDate]
                    (
                      [DimDateID]
                    , [DimFullDate]
                    , [DimDateName]
                    , [DimDateNameUS]
                    , [DimDateNameEU]
                    , [DimDayOfWeek]
                    , [DimDayNameOfWeek]
                    , [DimDayOfMonth]
                    , [DimDayOfYear]
                    , [DimWeekdayWeekend]
                    , [DimWeekOfYear]
                    , [DimMonthName]
                    , [DimMonthOfYear]
                    , [DimIsLastDayOfMonth]
                    , [DimCalendarQuarter]
                    , [DimCalendarYear]
                    , [DimCalendarYearMonth]
                    , [DimCalendarYearQtr]
                    , [DimFiscalMonthOfYear]
                    , [DimFiscalQuarter]
                    , [DimFiscalYear]
                    , [DimFiscalYearMonth]
                    , [DimFiscalYearQtr]
                    )
            VALUES  (
                      ( YEAR(@DateCounter) * 10000 ) + ( MONTH(@DateCounter)
                                                         * 100 )
                      + DAY(@DateCounter)  --DateKey
                    , @DateCounter -- FullDate
                    , CAST(YEAR(@DateCounter) AS CHAR(4)) + '/'
                      + RIGHT('00' + RTRIM(CAST(DATEPART(mm, @DateCounter) AS CHAR(2))), 2) + '/'
                      + RIGHT('00' + RTRIM(CAST(DATEPART(dd, @DateCounter) AS CHAR(2))), 2) --DateName
                    , RIGHT('00' + RTRIM(CAST(DATEPART(mm, @DateCounter) AS CHAR(2))), 2) + '/'
                      + RIGHT('00' + RTRIM(CAST(DATEPART(dd, @DateCounter) AS CHAR(2))), 2)  + '/'
                      + CAST(YEAR(@DateCounter) AS CHAR(4))--DateName
                    , RIGHT('00' + RTRIM(CAST(DATEPART(dd, @DateCounter) AS CHAR(2))), 2) + '/'
                      + RIGHT('00' + RTRIM(CAST(DATEPART(mm, @DateCounter) AS CHAR(2))), 2)  + '/'
                      + CAST(YEAR(@DateCounter) AS CHAR(4))--DateName
                    , DATEPART(dw, @DateCounter) --DayOfWeek
                    , DATENAME(dw, @DateCounter) --DayNameOfWeek
                    , DATENAME(dd, @DateCounter) --DayOfMonth
                    , DATENAME(dy, @DateCounter) --DayOfYear
                    , CASE DATENAME(dw, @DateCounter)
                        WHEN 'Saturday' THEN 'Weekend'
                        WHEN 'Sunday' THEN 'Weekend'
                        ELSE 'Weekday'
                      END --WeekdayWeekend
                    , DATENAME(ww, @DateCounter) --WeekOfYear
                    , DATENAME(mm, @DateCounter) --MonthName
                    , MONTH(@DateCounter) --MonthOfYear
                    , @LastDayOfMon --IsLastDayOfMonth
                    , DATENAME(qq, @DateCounter) --CalendarQuarter
                    , YEAR(@DateCounter) --CalendarYear
                    , CAST(YEAR(@DateCounter) AS CHAR(4)) + '-'
                      + RIGHT('00' + RTRIM(CAST(DATEPART(mm, @DateCounter) AS CHAR(2))), 2) --CalendarYearMonth
                    , CAST(YEAR(@DateCounter) AS CHAR(4)) + 'Q' + DATENAME(qq, @DateCounter) --CalendarYearQtr
                    , MONTH(@FiscalCounter) --[FiscalMonthOfYear]
                    , DATENAME(qq, @FiscalCounter) --[FiscalQuarter]
                    , YEAR(@FiscalCounter) --[FiscalYear]
                    , CAST(YEAR(@FiscalCounter) AS CHAR(4)) + '-'
                      + RIGHT('00' + RTRIM(CAST(DATEPART(mm, @FiscalCounter) AS CHAR(2))), 2) --[FiscalYearMonth]
                    , CAST(YEAR(@FiscalCounter) AS CHAR(4)) + 'Q' + DATENAME(qq, @FiscalCounter) --[FiscalYearQtr]
                    )

            -- Increment the date counter for next pass thru the loop
            SET @DateCounter = DATEADD(d, 1, @DateCounter)
      END

SET NOCOUNT ON -- turn the annoying messages back on

-- Select all rows inserted for the final year as a sanity check
SELECT  *
FROM    [dbo].[DimDate]
WHERE DimDateID > (YEAR(@EndDate) * 10000)

这里是创建tableSQL

/* Create table dbo.DimDate */
CREATE TABLE [dbo].[DimDate]
       ( [DimDateID] BIGINT NOT NULL
       , [DimFullDate] DATETIME NULL
       , [DimDateName] CHAR(11) NULL
       , [DimDateNameUS] CHAR(11) NULL   --US Date FORMAT, MM/DD/YYYY
       , [DimDateNameEU] CHAR(11) NULL   --European Union Date Format DD/MM/YYYY
       , [DimDayOfWeek] TINYINT NULL
       , [DimDayNameOfWeek] CHAR(10) NULL
       , [DimDayOfMonth] TINYINT NULL
       , [DimDayOfYear] SMALLINT NULL
       , [DimWeekdayWeekend] CHAR(7) NULL
       , [DimWeekOfYear] TINYINT NULL
       , [DimMonthName] CHAR(10) NULL
       , [DimMonthOfYear] TINYINT NULL
       , [DimIsLastDayOfMonth] CHAR(1) NULL
       , [DimCalendarQuarter] TINYINT NULL
       , [DimCalendarYear] SMALLINT NULL
       , [DimCalendarYearMonth] CHAR(7) NULL
       , [DimCalendarYearQtr] CHAR(7) NULL
       , [DimFiscalMonthOfYear] TINYINT NULL
       , [DimFiscalQuarter] TINYINT NULL
       , [DimFiscalYear] INT NULL
       , [DimFiscalYearMonth] CHAR(9) NULL
       , [DimFiscalYearQtr] CHAR(8) NULL
       , CONSTRAINT [DimDateIDPK] PRIMARY KEY CLUSTERED ( [DimDateID] )
       )
ON     [PRIMARY];

您是否尝试过 运行在数据库上使用 SQL Server Profiler 并在执行 SSIS 程序包时捕获 Trace?您应该能够以这种方式看到针对您的数据库 运行 的 TSQL。

确保传递给脚本的值正确的另一种方法是在 SSIS 包中的执行 SQL 任务上放置一个执行前断点,然后查看值适用于您传递给脚本的每个变量。

此致, 克里斯托