SSIS - 如何捕获 PRINT 消息?
SSIS - how to capture PRINT message?
Execute SQL 任务调用一个内部有 PRINT
命令的过程。如何在 progress
选项卡(或 output
)中一次显示这些消息?
您需要编写自己的过程来调用 SQL,因为执行 SQL 任务没有为侧通道通信定义的事件处理程序。
我会从脚本任务开始
设置
在我的数据库中,我有一个定义如下的存储过程
CREATE OR ALTER PROCEDURE dbo.Printsalot
AS
BEGIN
SET NOCOUNT ON;
RAISERROR('This is the first message', 10, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:05';
RAISERROR('This is the second message', 10, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:04';
RAISERROR('This is the third message', 10, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:03';
RAISERROR('This is the fourth message', 10, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:02';
RAISERROR('This is the fifth message', 10, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:01';
RAISERROR('This is the sixth message', 10, 1) WITH NOWAIT;
END;
我使用 RAISERROR 而不是 print,所以我可以 WITH NOWAIT
否则,PRINT 不会那么有用。我在那里设置延迟以模拟更长的 运行 过程。
脚本任务
你的 TODO 代码如下
- 适当定义
connString
。我从来没有在脚本任务中绑定到现有的连接管理器,所以我通常使用 C# 变量来保存连接字符串信息并将其传递到脚本中
- 如果 proc 花费的时间太长,请在您的命令对象上定义一个
CommandTimeout
- 适当定义
proc
和代码
using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
namespace ST_ExecuteSQLWithInfo
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
// TODO: Fix this
string connString = @"SERVER=.\dev2017;Integrated Security=true;DATABASE=tempdb";
// TODO: Fix this
string proc = "dbo.Printsalot";
using (SqlConnection connection = new SqlConnection(connString))
{
connection.Open();
connection.FireInfoMessageEventOnUserErrors = true;
connection.InfoMessage += new SqlInfoMessageEventHandler(HandleSqlProgress);
using (SqlCommand command = new SqlCommand(proc, connection))
{
command.CommandType = CommandType.StoredProcedure;
// TODO: Define a command.CommandTimeout value
command.ExecuteNonQuery();
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
public void HandleSqlProgress(object sender, SqlInfoMessageEventArgs e)
{
bool fireAgain = true;
Dts.Events.FireInformation(0, "SQL Progress", e.Message, "", 0, ref fireAgain);
}
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
}
}
结果
从输出window/Progresswindow
SSIS package "C:\Users\bfellows\source\repos\SO_Trash\SO_Trash\SO_71109444.dtsx" starting.
Information: 0x0 at SCR Echo Proc, SQL Progress: This is the first message
Information: 0x0 at SCR Echo Proc, SQL Progress: This is the second message
Information: 0x0 at SCR Echo Proc, SQL Progress: This is the third message
Information: 0x0 at SCR Echo Proc, SQL Progress: This is the fourth message
Information: 0x0 at SCR Echo Proc, SQL Progress: This is the fifth message
Information: 0x0 at SCR Echo Proc, SQL Progress: This is the sixth message
SSIS package "C:\Users\bfellows\source\repos\SO_Trash\SO_Trash\SO_71109444.dtsx" finished: Success.
参考资料
- http://www.sqlskills.com/blogs/jonathan/capturing-infomessage-output-print-raiserror-from-sql-server-using-powershell/
- c# - SqlConnection InfoMessage triggering only at end of execution
- https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlinfomessageeventhandler
- https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.infomessage
- https://dba.stackexchange.com/q/54544/2131
Execute SQL 任务调用一个内部有 PRINT
命令的过程。如何在 progress
选项卡(或 output
)中一次显示这些消息?
您需要编写自己的过程来调用 SQL,因为执行 SQL 任务没有为侧通道通信定义的事件处理程序。
我会从脚本任务开始
设置
在我的数据库中,我有一个定义如下的存储过程
CREATE OR ALTER PROCEDURE dbo.Printsalot
AS
BEGIN
SET NOCOUNT ON;
RAISERROR('This is the first message', 10, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:05';
RAISERROR('This is the second message', 10, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:04';
RAISERROR('This is the third message', 10, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:03';
RAISERROR('This is the fourth message', 10, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:02';
RAISERROR('This is the fifth message', 10, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:01';
RAISERROR('This is the sixth message', 10, 1) WITH NOWAIT;
END;
我使用 RAISERROR 而不是 print,所以我可以 WITH NOWAIT
否则,PRINT 不会那么有用。我在那里设置延迟以模拟更长的 运行 过程。
脚本任务
你的 TODO 代码如下
- 适当定义
connString
。我从来没有在脚本任务中绑定到现有的连接管理器,所以我通常使用 C# 变量来保存连接字符串信息并将其传递到脚本中 - 如果 proc 花费的时间太长,请在您的命令对象上定义一个
CommandTimeout
- 适当定义
proc
和代码
using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
namespace ST_ExecuteSQLWithInfo
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
// TODO: Fix this
string connString = @"SERVER=.\dev2017;Integrated Security=true;DATABASE=tempdb";
// TODO: Fix this
string proc = "dbo.Printsalot";
using (SqlConnection connection = new SqlConnection(connString))
{
connection.Open();
connection.FireInfoMessageEventOnUserErrors = true;
connection.InfoMessage += new SqlInfoMessageEventHandler(HandleSqlProgress);
using (SqlCommand command = new SqlCommand(proc, connection))
{
command.CommandType = CommandType.StoredProcedure;
// TODO: Define a command.CommandTimeout value
command.ExecuteNonQuery();
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
public void HandleSqlProgress(object sender, SqlInfoMessageEventArgs e)
{
bool fireAgain = true;
Dts.Events.FireInformation(0, "SQL Progress", e.Message, "", 0, ref fireAgain);
}
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
}
}
结果
从输出window/Progresswindow
SSIS package "C:\Users\bfellows\source\repos\SO_Trash\SO_Trash\SO_71109444.dtsx" starting.
Information: 0x0 at SCR Echo Proc, SQL Progress: This is the first message
Information: 0x0 at SCR Echo Proc, SQL Progress: This is the second message
Information: 0x0 at SCR Echo Proc, SQL Progress: This is the third message
Information: 0x0 at SCR Echo Proc, SQL Progress: This is the fourth message
Information: 0x0 at SCR Echo Proc, SQL Progress: This is the fifth message
Information: 0x0 at SCR Echo Proc, SQL Progress: This is the sixth message
SSIS package "C:\Users\bfellows\source\repos\SO_Trash\SO_Trash\SO_71109444.dtsx" finished: Success.
参考资料
- http://www.sqlskills.com/blogs/jonathan/capturing-infomessage-output-print-raiserror-from-sql-server-using-powershell/
- c# - SqlConnection InfoMessage triggering only at end of execution
- https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlinfomessageeventhandler
- https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.infomessage
- https://dba.stackexchange.com/q/54544/2131