SQL : Understanding RAISERROR in Sql Server
In SQL Server, Sometimes we have to display server error message then RAISERROR will be used for showing server side error.
RAISERROR syntax :
RAISERROR ( { error_number | message | @local_variable } { ,severity ,state }
[ ,argument [ ,...n ] ] ) [ WITH option [ ,...n ] ]
RAISERROR will be used for generating an error message for sql server session.
RAISERROR can refrence dynamic user-defined message stored in sys.messages.
we can dynamically add custom message for specified error in sys.messages
After inserting message in RAISERROR with error number like this
RAISERROR syntax :
RAISERROR ( { error_number | message | @local_variable } { ,severity ,state }
[ ,argument [ ,...n ] ] ) [ WITH option [ ,...n ] ]
RAISERROR will be used for generating an error message for sql server session.
RAISERROR can refrence dynamic user-defined message stored in sys.messages.
we can dynamically add custom message for specified error in sys.messages
-- format for inserting custom message in sys.messages insert into sys.messages values('language_id','severity','is_event_log','text') -- example insert into sys.messages values('1033','10','0','Please Select Valid Adviser') -- check language id from syslanguages select msglangid,* from syslanguages
After inserting message in RAISERROR with error number like this
-- Format raiseerror RAISERROR ('error_number','sevrity','is_event_log') --example RAISERROR (50004,16,0) -- this will return the text for this error number
If we want to display only error test with sql error number then use like this
-- Format raiseerror RAISERROR ('Error Message Body',16,0) --example RAISERROR ('Please Select valid name',16,0) -- this will return the text with error number
if we want to display as text only then use like this
-- Format raiseerror use severity 10 RAISERROR ('Error Message Body',10,0) --example RAISERROR ('Please Select valid name',10,0) -- this will return the text only
Important Note : RAISERROR is can't be used in SQL Server 2014. This will be used through THROW. THROW is now introduced in SQL Server 2012.
Comments
Post a Comment