2012-05-03 21 views
0

私は毎月最後の木曜日に予定されている官公庁に月次報告を行うSQL 2005 SSISパッケージを入手しました。私は正しい日にパッケージを実行するようにSQL Serverをセットアップしました。レポートのスケジュール設定は問題ありません。SQLスケジュールを使用したSSISの毎月の実行

現在、SSISパッケージは月間のレポートを作成します。私は2つの変数を使用しました。式を使って月の最初の日を決定し、 "5/1/2012"のような文字列に変換するBeginDate。同様に、「2010年5月3日」のような今日の日付を吐き出すEndDateとがあります。

BeginDate変数をレポートを最後に実行した日の翌日に設定する方法はありますか?前月の最後の木曜日の日付を見つける良い方法はありますか?

答えて

1

これを解決する方法はいろいろあります。

オプション1

あなたはSQLエージェント、使用 SQL Agentを使用しているので。ジョブを作成するときは、ボックスをクリックして履歴を保存していることを確認します。過去1ヶ月のジョブ履歴をデータベース保守ポリシーで削除しないと仮定すると、ジョブステップが最後にいつ正常に完了したかを判断するためのクエリを書くことができます。 Execute SQLステップでこのようなクエリを実行すると、SSISステップが正常に実行された最後の時間が得られます。あなたがする必要があるだろうすべては

-- this query will find the most recent, successful execution of a job 
-- named 'Last Thursday Of the Month job' with a job step of 
-- 'The SSIS Step' 
SELECT 
    J.name AS job_name 
, JH.step_name AS job_step_name 
, MAX(msdb.dbo.agent_datetime(JH.run_date, JH.run_time)) AS execution_datetime 
FROM 
    msdb.dbo.sysjobhistory JH 
    INNER JOIN 
     msdb.dbo.sysjobs J 
     ON J.job_id = JH.job_id 
    INNER JOIN 
     msdb.dbo.sysjobsteps JS 
     ON JS.job_id = J.job_id 
      AND JS.step_id = JH.step_id 
WHERE 
    JH.run_status = 1 
    AND J.name = 'Last Thursday Of the Month job' 
    AND JH.step_name = 'The SSIS Step' 
GROUP BY 
    J.name 
, JH.step_name; 

オプション2

カスタムテーブルを作成し、あなたの仕事は、そのテーブルに最後の処理日時を記録している変数あなたの終了日に第三の要素の値を割り当てるです。プロセスは処理の開始時にそのテーブルを調べ、最後の日付を終了日として使用します。

CREATE TABLE dbo.AlmostEndOfTheMonth 
(
    -- Can't use date as you're on 2005 
    execution_date datetime 
); 

SELECT 
    MAX(AEOM.execution_date) AS most_recent_execution_date 
FROM 
    dbo.AlmostEndOfTheMonth AEOM; 

オプション3

計算あなたの好きな言語での月の最終木曜日(.NET、TSQLは、おそらくSSIS式言語が働くだろうが、私がしようとしません)

DECLARE 
    @daysInWeek int 
, @dayOfWeek int 
SELECT 
    @daysInWeek = 7 
, @dayOfWeek = 5; 

; WITH LAST_DAY_OF_PREVIOUS_MONTH (last_day_month) AS 
(
    --http://blog.sqlauthority.com/2007/08/18/sql-server-find-last-day-of-any-month-current-previous-next/ 
    -- SQL 2012 makes this much easier with EOM and/or datefromparts functions 
    SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)) 
) 
, LAST_THURSDAY_REFERENCE (last_thursday, last_day_month) AS 
(
    SELECT CAST('2012-01-26' AS datetime), cast('2012-01-31' AS datetime) 
    UNION ALL SELECT CAST('2012-02-23' AS datetime), cast('2012-02-29' AS datetime) 
    UNION ALL SELECT CAST('2012-03-29' AS datetime), cast('2012-03-31' AS datetime) 
    UNION ALL SELECT CAST('2012-04-26' AS datetime), cast('2012-04-30' AS datetime) 
    UNION ALL SELECT CAST('2012-05-31' AS datetime), cast('2012-05-31' AS datetime) 
    UNION ALL SELECT CAST('2012-06-28' AS datetime), cast('2012-06-30' AS datetime) 
    UNION ALL SELECT CAST('2012-07-26' AS datetime), cast('2012-07-31' AS datetime) 
    UNION ALL SELECT CAST('2012-08-30' AS datetime), cast('2012-08-31' AS datetime) 
    UNION ALL SELECT CAST('2012-09-27' AS datetime), cast('2012-09-30' AS datetime) 
    UNION ALL SELECT CAST('2012-10-25' AS datetime), cast('2012-10-31' AS datetime) 
    UNION ALL SELECT CAST('2012-11-29' AS datetime), cast('2012-11-30' AS datetime) 
    UNION ALL SELECT CAST('2012-12-27' AS datetime), cast('2012-12-31' AS datetime) 
) 
SELECT 
    * 
    -- Thursday is the 5th day of the week, assuming you haven't messed with calendar's start of week 
    -- We need to subtract up to 6 days from the end of the month to find the 
    -- last Thursday. We can use the mod operator on ensure our dateadd function doesn't modify the 
    -- date if the end of the month is actually Thursday, otherwise we want to back it off N days 
    -- Examples might be easier to understand 
    -- Last day DayWeek  WeekdayNumber DaysToSubtract 
    -- 2012-01-31 Tuesday  3    -5 
    -- 2012-02-29 Wednesday 4    -6 
    -- 2012-03-31 Saturday 7    -2 
    -- 2012-04-30 Monday  2    -4 
    -- 2012-05-31 Thursday 5    0 
    -- 2012-06-30 Saturday 7    -2 
    -- 2012-07-31 Tuesday  3    -5 
    -- 2012-08-31 Friday  6    -1 
    -- 2012-09-30 Sunday  1    -3 
    -- 2012-10-31 Wednesday 4    -6 
    -- 2012-11-30 Friday  6    -1 
    -- 2012-12-31 Monday  2    -4 
, dateadd(d, -((@daysInWeek - @dayOfWeek) + DATEPART(dw, LDM.last_day_month)) % @daysInWeek, LDM.last_day_month) AS last_thursday_of_month 
FROM 
    LAST_DAY_OF_PREVIOUS_MONTH LDM 
    -- Comment the above and uncomment the below to 
    -- evaluate all the dates in the 2012 
    -- LAST_THURSDAY_REFERENCE LDM 

オプション4

オプション1と同様ですが、SSISロギングを使用してSQL Serverにログオンし、最後に正常に実行された日付を探し、それを終了日として使用します。

-- this code is approximate, I don't have a 2005 instance about 
-- if you've logged to a different database, change the msdb reference 
SELECT 
    max(starttime) AS execution_datetime 
FROM 
    msdb.dbo.sysdtslog90 L 
WHERE 
    L.event = 'PackageStart' 
    AND L.source = 'MyPackage'; 
+0

私はオプション2が好きです。純粋にコントロールのために、データ統合開発者に与えます。 :{> –

+0

私はAndyが与えた理由のためにオプション2を使いました。 SQL Agentの使用を中止することに決めた場合でも、プロセスは正しく実行されます。 – nlinus

関連する問題