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.
according to MSDN
16
would be the severity.1
would be the state.The error you get is because you have not properly supplied the required parameters for the
RAISEERROR
function.It is the severity level of the
error
. The levels are from 11 - 20 which throw an error inSQL
. The higher the level, the more severe the level and thetransaction
should be aborted.You will get the syntax error when you do:
Because you have not specified the correct
parameters
(severity level
orstate
).If you wish to issue a warning and not an
exception
, use levels 0 - 10.From MSDN:
16 is severity and 1 is state, more specifically following example might give you more detail on syntax and usage:
You can follow and try out more examples from http://msdn.microsoft.com/en-us/library/ms178592.aspx
The severity level 16 in your example code is typically used for user-defined (user-detected) errors. The SQL Server DBMS itself emits severity levels (and error messages) for problems it detects, both more severe (higher numbers) and less so (lower numbers).
The state should be an integer between 0 and 255 (negative values will give an error), but the choice is basically the programmer's. It is useful to put different state values if the same error message for user-defined error will be raised in different locations, e.g. if the debugging/troubleshooting of problems will be assisted by having an extra indication of where the error occurred.