zaagan
8/3/2019 - 7:01 PM

MS SQL Custom Error Messages

MS SQL Custom Error Messages

-- Add New message
EXEC sp_addmessage 50001, 16,
    'Unable to update %s';

-- Update Message
EXEC sp_addmessage 50001, 16, 
    'Still unable to update %s',
    @Replace = 'Replace';

SELECT * 
  FROM sys.messages
  WHERE message_id > 50000;
  
-- Generate messages script
SELECT 'EXEC sp_addmessage ' 
  + Cast(message_id as VARCHAR(7)) 
  + ', ' + Cast(Severity as VARCHAR(2)) 
  + ', ''' + [text] +  ''';' 
FROM sys.messages
WHERE message_id > 50000;

-- Delete Message
EXEC sp_dropmessage 50001