As part of a script, in which execution is halted and changes are rolled back if there are any errors, I use the following statements to drop an xml schema collection if it exists
IF EXISTS (SELECT * FROM sys.xml_schema_collections WHERE name = 'MyXMLSchemaCollection')
BEGIN
PRINT 'Drop MyXMLSchemaCollection';
DROP XML SCHEMA COLLECTION MyXMLSchemaCollection;
END
GO
This works nicely except for when the schema collection is in use, the result of which is an error like the following:
Specified collection 'MyXMLSchemaCollection' cannot be dropped because it is used by object 'MyObject'.
How would I go about only dropping the schema collection if it is not used by any objects in the database? The xml schema collection does not need to be dropped if it's being used.