如何在 SQL Server 2012 中为 RAISERROR 函数在 varchar @params 中转义“%”

ado*_*lot 11 sql-server t-sql raiserror

我应该如何转义%参数中的字符,以便我重新调整RAISERROR我的消息

declare @msg varchar(max)  = (SELECT ' Message with %  ' AS MSG)

if @msg is not null
begin
    RAISERROR (@msg ,16,1); 
end
Run Code Online (Sandbox Code Playgroud)

这将引发错误消息

消息 2787,级别 16,状态 1,第 4 行
无效的格式规范:“%”。

对于最终用户,此消息是不可读的。

我的消息是从数据库生成并设置此消息。

我避免收到错误消息的方法是替换

set @msg = REPLACE(@msg,'%','P')
Run Code Online (Sandbox Code Playgroud)

但是我还是不知道怎么加%符号

Rem*_*anu 18

使用%%

raiserror(N'This is a message with %%', 0, 1);
Run Code Online (Sandbox Code Playgroud)


小智 8

有一种简单、巧妙的方法可以解决百分比符号问题。使用消息文本作为参数:

RAISERROR ('%s', 16, 1, 'Message with %')