RAISERROR in SQL SERVER

RAISERROR will use the built-in messages in sys.messages view or dynamically generated error message.

The Syntax is as follows
RAISERROR ( { msg_id | msg_str | @local_variable }    
{ ,severity ,state }     
[ ,argument [ ,...n ] ] )     [ WITH option [ ,...n ] ]

Severity 0 – 18 , Can be raised by any user

Severity 19 – 25, Can be raised by those who have sysadmin role.

State : This is a integer value between 0 to 255

The below shows an error message if the employee id does not exists

CREATE PROCEDURE GenerateError(@EmployeeID int)
AS
BEGIN
If exists(SELECT 1 FROM employee WHERE EmployeeID = @EmployeeID)
BEGIN
SELECT * FROM Employee WHERE EmployeeID = @EmployeeID
END
ELSE
BEGIN
RAISERROR ('Employee ID does not exists', 10, 2)
END
END

Comments

Popular posts from this blog

“Unable to retrieve the metadata for …” Error while using ASP.NET MVC 3

Getting Started with ASP.NET MVC, SignalR KnockoutJS

2012 - > 2013 : Happy New Year