使用SQL“消息”的结果 窗格

发布于 2024-07-28 22:39:01 字数 761 浏览 2 评论 0原文

我有一个现有的 SQL 2005 存储过程,由于某种原因,它在 SSMS 的“消息”窗格而不是“结果”窗格中输出其结果。 (它实际上是一个 CLR 过程,已经编译并部署到我们所有的服务器上,并用于另一个日常进程。所以我无法更改它,我只想使用它的输出。)

为了讨论起见,这里有一个存储过程行为方式相同:

CREATE PROCEDURE [dbo].[OutputTest] 
    @Param1  int, @Param2 varchar(100)
AS
BEGIN
    SET NOCOUNT ON;
    PRINT 'C,10000,15000';
    PRINT 'D,30000,90000';
    PRINT 'E,500,50000';
END

因此,其中没有实际的 SELECT 语句,如果运行此语句,您将仅在“消息”窗格中看到这些结果。

我有什么方法可以使用这些结果作为更大查询的一部分吗? 将它们放入临时表或其他东西中,以便我可以解析它们?

“正常的东西”都不起作用,因为这里没有真正的“输出”:

INSERT INTO #output
EXEC OutputTest 100, 'bob'

只是显示

C,10000,15000
D,30000,90000
E,500,50000

(0 row(s) affected)

在消息窗格上,临时表实际上没有放入任何内容。

I have an existing SQL 2005 stored procedure that for some reason, outputs its results in the Messages pane in SSMS instead of the Results pane. (Its actually a CLR procedure already compiled and deployed to all our servers, and used for another daily process. So I can't change it, I just want to use its output.)

For the sake of discussion, here's a stored proc that behaves the same way:

CREATE PROCEDURE [dbo].[OutputTest] 
    @Param1  int, @Param2 varchar(100)
AS
BEGIN
    SET NOCOUNT ON;
    PRINT 'C,10000,15000';
    PRINT 'D,30000,90000';
    PRINT 'E,500,50000';
END

So no actual SELECT statement in there, and if you run this, you'll see these results only on the Messages pane.

Is there any way for me to use these results as part of a larger query? Put them in a temp table or something, so I can parse them out?

None of the "normal stuff" works, because there is no true "output" here:

INSERT INTO #output
EXEC OutputTest 100, 'bob'

just shows

C,10000,15000
D,30000,90000
E,500,50000

(0 row(s) affected)

on the messages pane, and the temp table doesn't actually get anything put into it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北城挽邺 2024-08-04 22:39:01

您可以从 C# 代码执行存储过程吗? 如果是这样,您也许能够挂接到名为 SqlInfoMessage 的 SqlCommand 事件:

SqlConnection _con = new SqlConnection("server=.;
            database=Northwind;integrated Security=SSPI;");

_con.InfoMessage += new SqlInfoMessageEventHandler(_con_InfoMessage);

事件处理程序将如下所示:

static void _con_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
    string myMsg = e.Message;            
}

“e.Message”是打印到 SQL Server Mgmt Studio 中的消息窗口的消息。

虽然它不会很漂亮并且可能需要一些丑陋的解析代码,但至少我希望您可以通过这种方式获取这些消息!

马克

Can you execute the stored proc from C# code? If so, you might be able to hook into the SqlCommand event called SqlInfoMessage:

SqlConnection _con = new SqlConnection("server=.;
            database=Northwind;integrated Security=SSPI;");

_con.InfoMessage += new SqlInfoMessageEventHandler(_con_InfoMessage);

The event handler will look like this:

static void _con_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
    string myMsg = e.Message;            
}

The "e.Message" is the message printed out to the message window in SQL Server Mgmt Studio.

While it won't be pretty and might require some ugly parsing code, at least you could get a hold of those messages that way, I hope!

Marc

纵山崖 2024-08-04 22:39:01

您无法从 SQL Server 内部捕获、捕获或使用这些消息。 但是,您可以从客户端应用程序中接收它们。

You cannot trap, catch or use these messages from within SQL Server. You can, however, receive them from within a client application.

说谎友 2024-08-04 22:39:01

我不认为有办法,但即使有,我认为你也应该认真考虑这是否是一个好主意。 这听起来像是一种忽悠,从长远来看只会给你带来痛苦。 创建一个完全符合您想要的功能的替代过程对我来说听起来是一个更好的计划。

I don't think there is a way but even if there is I think you should seriously consider whether it is a good idea. This sounds like a fudge which can only cause you pain in the long term. Creating an alternative proc that does exactly what you want sounds to me like a better plan.

維他命╮ 2024-08-04 22:39:01

无法从结果中的消息窗格获取消息。
如果你仔细想想,SSMS 只是一个客户端,它按照你所看到的方式解析这些消息。

如果您想在应用程序中使用它们,请查看 ADO 中的连接事件。网络

there is no way to get messages from the message pane in your result.
if you think about it the SSMS is just a client that parses those messages the way you see it.

if you wan to use them in your app take a look at Connection Events in ADO.NET

唯憾梦倾城 2024-08-04 22:39:01

我认为这可能的唯一方法是通过 RAISERROR 命令打印输出。 在这种情况下,您也许可以使用 TRY/CATCH 在其他地方捕获它。

但这只是一个想法:我从未这样做过。 事实上,我们所做的唯一接近的事情是我们有一个命令行工具来在批处理作业中运行存储过程,而不是使用 sql server 代理来调度它们。 这样,我们所有的夜间作业都安排在一个地方(Windows 任务计划程序)而不是两个地方,并且命令行工具将打印到消息窗口的任何内容捕获到我们监视的公共日志系统中。 因此,某些程序会向该窗口输出大量详细信息。

The only way I could think that this might be possible is if the output is printed via the RAISERROR command. In that case, you might be able to capture it elsewhere using TRY/CATCH.

But that's just an idea: I've never done it. In fact, the only thing we do that's remotely close is that we have a command line tool to run stored procedures in batch jobs rather than using sql server agent to schedule them. This way all of our nightly jobs are scheduled in one place (the windows task scheduler) rather than two, and the command line tool captures the anything printed to the message window into a common logging system that we monitor. So some of procedures will output quite a lot of detail to that window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文