Error message:
DELETE is not allowed because the statement updates view "VVV" which participates in a join and has an INSTEAD OF DELETE trigger.
Explanation:
A view with an INSTEAD OF UPDATE / INSTEAD OF DELETE trigger cannot be a target of an UPDATE or DELETE statements with a FROM clause.
Work-Arounds:
1. Replace the JOIN with 'IN':
delete dbo.test1
from dbo.test1 t1 join dbo.test2 t2 on t1.Id = t2.Id
-->
delete dbo.test1 where t1.Id IN (select Id from dbo.test2)
2. Replace the JOIN with EXISTS:
delete dbo.test1
from dbo.test1 t1 join dbo.test2 t2 on t1.Id = t2.Id
-->
delete dbo.test1
from dbo.test1 t1
where exists ( Select top 1 Id from dbo.test2 t2 Where t1.Id = t2.Id )
3. If possible - do the update/deletion on the original tables (= the source table/tables of the View).
It's not an ANSI SQL and not idempotent, you can use MERGE to achieve this. FYI https://stackoverflow.com/questions/3085036/update-is-not-allowed-because-the-statement-updates-view-table-name-which-part
ReplyDelete