加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql-server – SQL Server printf

发布时间:2020-12-12 06:24:13 所属栏目:MsSql教程 来源:网络整理
导读:在Sql Server中是否有类似printf的功能?我想要与RAISERROR功能相同的功能,但是我不想抛出错误或打印消息,而是将其写入varchar,因为我的ERP不会让我处理错误消息. 这是SQL Server 2000. RAISERROR的实际工作示例: declare @name varchar(10)set @name = 'Geo
在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读