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

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...