This post has been moved!
Please visit my new blog here:
-- Add my message so that I can have an error number
EXEC sp_addmessage @msgnum = 50025,
@severity = 9,
@msgtext = N'This is a user defined error!';
-- Use the message number that I just created
-- Sev 9 (http://msdn.microsoft.com/en-us/library/ms164086.aspx)
-- State 0 since we're not getting too fancy here.
RAISERROR (50025, 9, 0) WITH LOG;
EXEC sp_dropmessage @msgnum = 50025;
|Note: In SQL Server 2012 you can use the THROW statement to create an error with a custom number without creating a message record as follows:
THROW 50025, N'This is a user defined error!', 0;
You should remember though that this will always be severity 16, and can’t be used for alerts since only severities 19 through 25 are logged by default as outlined at http://msdn.microsoft.com/en-us/library/ms189982.aspx and the THROW statement does not provide an option for logging to the error log.