I just created a Instead After Trigger whose syntax is given below:
Create trigger tgrInsteadTrigger on copytableto Instead of Insert as Declare @store_name varchar(30); declare @sales int; declare @date datetime; select @store_name = i.store_name from inserted i select @sales = i.sales from inserted i select @date = i.Date from inserted i begin if (@sales > 1000) begin RAISERROR('Cannot Insert where salary > 1000',16,1); ROLLBACK; end else begin insert into copytablefrom(store_name, sales, date) values (@store_name, @sales, @date); Print 'Instead After Trigger Executed'; end End
In the above syntax I have used RAISERROR('Cannot Insert where salary > 1000',16,1)
But when I write RAISERROR('Cannot Insert where salary > 1000')
it gives the error "Incorrect syntax near ')'" on the same line.
Can anyone please explain the use of (16,1) here.
RAISERROR is a SQL Server error handling statement that generates an error message and initiates error processing. RAISERROR can either reference a user-defined message that is stored in the sys. messages catalog view or it can build a message dynamically.
Is an integer from 0 through 255. Negative values default to 1. Values larger than 255 should not be used. If the same user-defined error is raised at multiple locations, using a unique state number for each location can help find which section of code is raising the errors.
The syntax of the SQL RAISERROR statement is the following: RAISERROR ( { message_text | message_id | @local_variable } { ,severity ,state } [ ,argument [ ,...n ] ] [ WITH option [ ,...n ] ];
When RAISERROR is run with a severity of 11 or higher in a TRY block, it transfers control to the associated CATCH block. The error is returned to the caller if RAISERROR is run: Outside the scope of any TRY block. With a severity of 10 or lower in a TRY block.
It is the severity level of the error
. The levels are from 11 - 20 which throw an error in SQL
. The higher the level, the more severe the level and the transaction
should be aborted.
You will get the syntax error when you do:
RAISERROR('Cannot Insert where salary > 1000').
Because you have not specified the correct parameters
(severity level
or state
).
If you wish to issue a warning and not an exception
, use levels 0 - 10.
From MSDN:
severity
Is the user-defined severity level associated with this message. When using msg_id to raise a user-defined message created using sp_addmessage, the severity specified on RAISERROR overrides the severity specified in sp_addmessage. Severity levels from 0 through 18 can be specified by any user. Severity levels from 19 through 25 can only be specified by members of the sysadmin fixed server role or users with ALTER TRACE permissions. For severity levels from 19 through 25, the WITH LOG option is required.
state
Is an integer from 0 through 255. Negative values or values larger than 255 generate an error. If the same user-defined error is raised at multiple locations, using a unique state number for each location can help find which section of code is raising the errors. For detailed description here
16 is severity and 1 is state, more specifically following example might give you more detail on syntax and usage:
BEGIN TRY -- RAISERROR with severity 11-19 will cause execution to -- jump to the CATCH block. RAISERROR ('Error raised in TRY block.', -- Message text. 16, -- Severity. 1 -- State. ); END TRY BEGIN CATCH DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT; SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(); -- Use RAISERROR inside the CATCH block to return error -- information about the original error that caused -- execution to jump to the CATCH block. RAISERROR (@ErrorMessage, -- Message text. @ErrorSeverity, -- Severity. @ErrorState -- State. ); END CATCH;
You can follow and try out more examples from http://msdn.microsoft.com/en-us/library/ms178592.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With