The Execute SQL task calls a procedure that has a PRINT
command inside. How can I display these messages at once in the progress
tab (or in output
)?
问问题
29 次
1 回答
2
您将需要编写自己的调用 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# 变量来保存连接字符串信息并将其传递到脚本中 CommandTimeout
如果 proc 耗时太长,请在命令对象上定义 aproc
适当地定义
和代码
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
};
}
}
结果
从输出窗口/进度窗口
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/
- https://stackoverflow.com/a/349362/181965
- c# - SqlConnection InfoMessage 仅在执行结束时触发
- 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
于 2022-02-14T15:34:45.760 回答