SQL Server printf

前端 未结 5 1529
臣服心动
臣服心动 2021-02-13 05:19

Is there a printf-like function in Sql Server? I want the same features as the RAISERROR function, but instead of throwing an error, or printing a message, I want to wri

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-13 06:11

    If you have a limited number of format strings, and are able to add them to sysmessages (via sp_addmessage), you can use 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.


    The below would be a valid answer for SQL Server 2005 or later, but unfortunately, the OP is seeking a solution for SQL Server 2000:


    It's ugly, and an abuse of Try/Catch and RAISERROR:

    declare @message varchar(50)
    
    begin try
        RAISERROR('Hello %s',16,1,'george')
    end try
    begin catch
        set @message = ERROR_MESSAGE()
    end catch
    
    print @message
    

提交回复
热议问题