2

指定されたディレクトリに3つのファイルが含まれていることを確認するスクリプトタスクで始まるSSISパッケージがあります。ファイルが2つある場合は警告が表示され、ファイルが1つ以下の場合はエラーメッセージを出力してプログラムを終了します。SQL Agentが私のパッケージを登録し、指定された時間に実行するという考え方です。ただし、スクリプトの次の条件が満たされている必要があります。

switch (numFiles)
            {
                case 0:
                    MessageBox.Show("Error: No files are in the directory C:\\Directory1\n Please restart execution.");
                    break;
                case 1:
                    MessageBox.Show("Error: Only one file was found in the directory C:\\Directory1\n Please restart execution.");
                    break;
                case 2:
                    MessageBox.Show("Warning: Only two files have been loaded into the directory C:\\Directory1\n Is this intended?.");
                    break;
            }

プログラムを正常に終了し、警告メッセージを出力するにはどうすればよいですか?サーバーで実行されているときにこのメッセージを表示するにはどうすればよいですか?

4

2 に答える 2

3

既存のEventsにフックするのが一番良いと思います。コードをベースラインとして使用して、変数をパッケージに追加し、SQL Server にデプロイしました。

        int numFiles = Convert.ToInt32(Dts.Variables["numFiles"].Value.ToString());
        switch (numFiles)
        {
            case 0:
                //MessageBox.Show("Error: No files are in the directory C:\\Directory1\n Please restart execution.");
                Dts.Events.FireError(0, "File count", "Error: No files are in the directory C:\\Directory1\n Please restart execution.", string.Empty, 0);
                break;
            case 1:
                //MessageBox.Show("Error: Only one file was found in the directory C:\\Directory1\n Please restart execution.");
                Dts.Events.FireError(0, "File count", "Error: Only one file was found in the directory C:\\Directory1\n Please restart execution.", string.Empty, 0);
                break;
            case 2:
                //MessageBox.Show("Warning: Only two files have been loaded into the directory C:\\Directory1\n Is this intended?.");
                Dts.Events.FireWarning(0, "File count", "Warning: Only two files have been loaded into the directory C:\\Directory1\n Is this intended?.", string.Empty, 0);
                break;
        }

コマンドラインからの実行

"DTExec.exe" /file /set .\so_RaiseEvents.dtsx /set \Package.Variables[User::numFiles];0

"DTExec.exe" /file /set .\so_RaiseEvents.dtsx /set \Package.Variables[User::numFiles];1

"DTExec.exe" /file /set .\so_RaiseEvents.dtsx /set \Package.Variables[User::numFiles];2

期待される出力が表示されます

Error: 2012-08-09 08:53:58.77
   Code: 0x00000000
   Source: Script Task File count
   Description: Error: No files are in the directory C:\Directory1
 Please restart execution.
End Error

Error: 2012-08-09 08:51:56.75
   Code: 0x00000000
   Source: Script Task File count
   Description: Error: Only one file was found in the directory C:\Directory1
 Please restart execution.
End Error

Warning: 2012-08-09 08:51:51.82
   Code: 0x00000000
   Source: Script Task File count
   Description: Warning: Only two files have been loaded into the directory C:\Directory1
 Is this intended?.
End Warning

エージェントから実行すると警告が表示されるはずですが、そうでない場合は、レポートにパラメーターを追加する必要があります。/report W

編集

そして、これらの点に対処するために

「プログラムを正常に終了して警告メッセージを表示するにはどうすればよいですか」 FireError により、スクリプト タスクは失敗コードを返しMicrosoft.SqlServer.Dts.Runtime.DTSExecResult.Success、パッケージの実行が終了します。警告、パッケージの実行を停止したい場合は、スクリプト タスクを変更して、成功しなかったことを示します。これを行うには、 DTSExecResult .Canceledに変換される Warning の 3 番目の ScriptResult 列挙を追加しました(何かが計画どおりに進まなかったことを伝える他のオプションのみ)。完全なコードは以下にあります

[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{


    /// <summary>
    /// This method is called when this script task executes in the control flow.
    /// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
    /// To open Help, press F1.
    /// </summary>
    public void Main()
    {
        int numFiles = Convert.ToInt32(Dts.Variables["numFiles"].Value.ToString());
        switch (numFiles)
        {
            case 0:
                //MessageBox.Show("Error: No files are in the directory C:\\Directory1\n Please restart execution.");
                Dts.Events.FireError(0, "File count", "Error: No files are in the directory C:\\Directory1\n Please restart execution.", string.Empty, 0);
                break;
            case 1:
                //MessageBox.Show("Error: Only one file was found in the directory C:\\Directory1\n Please restart execution.");
                Dts.Events.FireError(0, "File count", "Error: Only one file was found in the directory C:\\Directory1\n Please restart execution.", string.Empty, 0);
                break;
            case 2:
                //MessageBox.Show("Warning: Only two files have been loaded into the directory C:\\Directory1\n Is this intended?.");
                Dts.Events.FireWarning(0, "File count", "Warning: Only two files have been loaded into the directory C:\\Directory1\n Is this intended?.", string.Empty, 0);
                Dts.TaskResult = (int)ScriptResults.Warning;
                break;
            default:
                Dts.TaskResult = (int)ScriptResults.Success;
                break;
        }

    }

    /// <summary>
    /// This enum provides a convenient shorthand within the scope of this class for setting the
    /// result of the script.
    /// 
    /// This code was generated automatically.
    /// </summary>
    enum ScriptResults
    {
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure,
        Warning = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Canceled,
    };

}

誰かがパッケージの実行を監視して応答することを期待しているようです。一般に、これは SSIS に実行してほしくないことです。これらのことを行うことができます。ユーザーが続行するかどうかを確認するメッセージ ボックス プロンプトを表示することができますが、サーバー上で実行すると、対話モードで実行されていないことが検出されるため、パッケージは検証チェックに失敗します。誰もサーバーに定期的にログインしていないため、人々がメッセージ ボックスをサーバーに残したままにし、パッケージが数週間ハングする場合、これは DTS よりもはるかに優れています。両方のマスター (有人実行と無人実行) を提供する必要がある場合は、System::InteractiveMode 変数を使用して、無人実行中に UI コードを表示しないようにします。

私の考えでは、自動化された環境でうまく機能するように、上記のようにイベントの発火を維持することをお勧めします。次に、手動で実行するために、ジョブを実行するための軽量の .NET ラッパーを提供します。ファイルチェックもオフロードします。はい、作業は繰り返されますが、ETL と UI コードをより明確に分離できます。

于 2012-08-09T13:57:13.553 に答える
0

サーバー上で実行している場合、そのようなメッセージ ボックスを表示することはできません。

ログ ファイル ( http://msdn.microsoft.com/en-us/library/ms167456%28v=sql.105%29.aspx ) に書き込む必要があります。

スクリプト タスクから実行されているパッケージを停止するには、RunningPackage.Stopメソッドを使用します。

        static void Main(string[] args)
        {
            Application app = new Application();
            RunningPackages pkgs = app.GetRunningPackages("yourserver");

            int pkgsRunning = pkgs.Count;
            Console.WriteLine("Packages before stop: thas + pkgsRunning);

            foreach (RunningPackage p in pkgs)
            {
                Console.WriteLine("InstanceID: " + p.InstanceID);
                Console.WriteLine("PackageDescription: " + p.PackageDescription);
                Console.WriteLine("PackageID: " + p.PackageID);
                Console.WriteLine("PackageName: " + p.PackageName);
                Console.WriteLine("UserName: " + p.UserName);
            }

            pkgs = app.GetRunningPackages("yourserver");
            foreach (RunningPackage package in pkgs)
            {
                package.Stop();
            }

            pkgsRunning = app.GetRunningPackages("yourserver").Count;
            Console.WriteLine("Packages after stop " + pkgsRunning);
        }

http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dts.runtime.runningpackage.stop.aspx

于 2012-08-09T12:27:33.787 に答える