SQL Server 打印输出 [英] SQL Server printf

查看:84
本文介绍了SQL Server 打印输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sql Server 中是否有类似 printf 的函数?我想要与 RAISERROR 函数相同的功能,但不是抛出错误或打印消息,我想将其写入 varchar,因为我的 ERP 不会让我处理错误消息.

Is there a printf-like function in Sql Server? I want the same features as the RAISERROR function, but instead of throwing an error, or printing a message, I want to write it in a varchar, because my ERP won't let me handle the error messages.

这是 SQL Server 2000.

This is SQL Server 2000.

RAISERROR 的实际工作示例:

Actual working example with RAISERROR:

declare @name varchar(10)
set @name = 'George'

RAISERROR ('Hello %s.', 10, 1, 'George')

打印Hello George

我在找什么:

declare @name varchar(10), @message varchar(50)
set @name = 'George'

SET @message = printf('Hello %s.', 'George')
return @message

这将返回 Hello George

推荐答案

如果您的格式字符串数量有限,并且能够将它们添加到 sysmessages(通过 sp_addmessage),您可以使用 FORMATMESSAGE:

If you have a limited number of format strings, and are able to add them to sysmessages (via sp_addmessage), you can use FORMATMESSAGE:

与 RAISERROR 语句一样,FORMATMESSAGE 通过将提供的参数值替换为消息中的占位符变量来编辑消息.有关错误消息中允许的占位符和编辑过程的更多信息,请参阅 RAISERROR.

Like the RAISERROR statement, FORMATMESSAGE edits the message by substituting the supplied parameter values for placeholder variables in the message. For more information about the placeholders allowed in error messages and the editing process, see RAISERROR.

<小时>

以下是 SQL Server 2005 或更高版本的有效答案,但不幸的是,OP 正在寻求 SQL Server 2000 的解决方案:


The below would be a valid answer for SQL Server 2005 or later, but unfortunately, the OP is seeking a solution for SQL Server 2000:

这很丑陋,而且是对 Try/Catch 和 RAISERROR 的滥用:

It's ugly, and an abuse of Try/Catch and RAISERROR:

declare @message varchar(50)

begin try
    RAISERROR('Hello %s',16,1,'george')
end try
begin catch
    set @message = ERROR_MESSAGE()
end catch

print @message

这篇关于SQL Server 打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆