I have a table in which view names can be inserted and the following trigger should ensure that only vew_names can be inserted that really exist in the System (that's all the code the trigger consists of):
CREATE TRIGGER check_if_view_exists ON DATABASE FOR [INSERT], [UPDATE] AS BEGIN IF EXISTS(SELECT view_name from MySpecial_views EXCEPT SELECT name from sys.views) RAISERROR ('You can only insert views in that table that really exist!',10, 1) ROLLBACK END -- end if END -- end trigger
However upon creation of the trigger I get the following errors:
Msg 102, Level 15, State 1, Procedure check_if_view_exists, Line 1 [Batch Start Line 0] Incorrect syntax near 'INSERT'.
Msg 102, Level 15, State 1, Procedure check_if_view_exists, Line 8 [Batch Start Line 0] Incorrect syntax near 'END'.
What is missing? I have carefully read the grammar of T-SQL Triggers but can't figure out why my trigger is wrong.