SQL Server printf

前端 未结 5 1519
臣服心动
臣服心动 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 05:57

    PRINT is just RAISERROR with a severity of 0. So you can use.

    declare @name varchar(10)
    set @name = 'George'
    
    RAISERROR ('Hello %s.', 0, 1, 'George') WITH NOWAIT
    

    Edit to store it into a variable you can use the xp_sprintf extended stored procedure.

    declare @name varchar(10)
    set @name = 'George'
    
    DECLARE @ret varchar(500)
    exec master..xp_sprintf @ret OUTPUT, 'Hello %s.', @name
    PRINT @ret
    

提交回复
热议问题