微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sql-server – SQL Server printf

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

这是sql Server 2000.

RAISERROR的实际工作示例:

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

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

打印你好乔治

我正在寻找:

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

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

这将返回你好乔治

解决方法

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

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寻求解决方案:

这是丑陋的,滥用Try / Catch和RAISERROR:

declare @message varchar(50)

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

print @message

原文地址:https://www.jb51.cc/mssql/79187.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐