In Sql Server I want to prevent users from dropping objects marked with a particular extended property. I thought this could easily be accomplished with a DDL trigger on the DROP event. As it turns out these triggers are fired after the object has been dropped and therefore it's no longer possible to access the extended properties and they are not available in the event data.
Is there any way to access extended properties for a dropped object in a DDL trigger?
Code below does not work since the extended properties have already been deleted:
CREATE TRIGGER PreventDeletionOfAutogeneratedTriggers ON DATABASE
FOR DROP_TRIGGER
AS
DECLARE @TriggerName sysname = EVENTDATA().value('(/EVENT_INSTANCE/ObjectName)[1]', 'sysname')
DECLARE @TriggerSchema sysname = EVENTDATA().value('(/EVENT_INSTANCE/SchemaName)[1]', 'sysname')
DECLARE @ObjectId int = OBJECT_ID(QUOTENAME(@TriggerSchema) + '.' + QUOTENAME(@TriggerName))
IF EXISTS (
SELECT
*
FROM
sys.extended_properties
WHERE
major_id = @ObjectId AND
name = 'Autogenerated'
)
BEGIN
RAISERROR ('Cannot drop triggers that are autogenerated.',16, 10)
ROLLBACK
END
GO