sql-server – SQL Server printf

前端之家收集整理的这篇文章主要介绍了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.f2er.com/mssql/79187.html

猜你在找的MsSQL相关文章