I am working with an existing SQL script which drops all foreign keys from a database. This runs into trouble if one of the tables is a FileTable
.
The primary question: is there a way to detect that a particular table is a FILETABLE
and skip dropping the foreign keys on that table?
If that is possible: is it also possible to get even more granular and drop any non-system generated foreign keys from the FILETABLE
by differentiating between system foreign keys and custom foreign keys?
DECLARE @fkdel varchar(512);
DECLARE FkCrsr CURSOR FOR
SELECT 'ALTER TABLE [' + TABLE_SCHEMA + '].[' + TABLE_NAME + '] DROP CONSTRAINT [' + CONSTRAINT_NAME +']'
FROM information_schema.table_constraints WITH (NOLOCK)
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY';
OPEN FkCrsr;
FETCH NEXT FROM FkCrsr INTO @fkdel;
WHILE @@FETCH_STATUS = 0
BEGIN;
PRINT @fkdel;
EXEC (@fkdel);
FETCH NEXT FROM FkCrsr INTO @fkdel;
END;
CLOSE FkCrsr;
DEALLOCATE FkCrsr;
EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all";
Running this on a database containing a FileTable results in an error like
Msg 3865, Level 16, State 1, Line 3
The operation on object 'FK__DocumentS__paren__3A69DAC6' is blocked. The object is a FileTable system defined object and user modifications are not allowed.Msg 3727, Level 16, State 0, Line 3
Could not drop constraint. See previous errors.