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 write it in a varchar, because my ERP won't let me handle the error messages.
This is SQL Server 2000.
Actual working example with RAISERROR:
declare @name varchar(10)
set @name = 'George'
RAISERROR ('Hello %s.', 10, 1, 'George')
prints Hello George
What I'm looking for:
declare @name varchar(10), @message varchar(50)
set @name = 'George'
SET @message = printf('Hello %s.', 'George')
return @message
This would return Hello George
If you have a limited number of format strings, and are able to add them to sysmessages (via sp_addmessage), you can use FORMATMESSAGE:
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
:PRINT
is justRAISERROR
with a severity of 0. So you can use.Edit to store it into a variable you can use the
xp_sprintf
extended stored procedure.Here's a simple printf procedure using sql_variant data types. Unfortunately, it only works for SQL Server 2008 and above.
And here are sample invocations:
If you are looking to store some message in a variable, then SET should be enough for you to handle right? Unless I am not clear with the question.