Hello I would like to know is is possible to drop all tables in database what was created under custom schema for example DBO1...with one sql query or special script.
T
Building on chris LB's answer, I added
GROUP BY d.constraint_object_id, t.name
because I saw duplicate constraint deletions in my query. constraint_object_id
is the FK Constraint ID, as noted at https://msdn.microsoft.com/en-us/library/ms186306.aspx
DECLARE @SqlStatement NVARCHAR(MAX),
@Schema NVARCHAR(20)
SET @Schema = 'aa'
SELECT @SqlStatement =
COALESCE(@SqlStatement,'') + 'ALTER TABLE '+@Schema+'.' + t.name + ' DROP CONSTRAINT ' +
OBJECT_NAME(d.constraint_object_id) + ';' + CHAR(13) + CHAR(10)
FROM sys.tables t
JOIN sys.foreign_key_columns d on t.object_id = d.parent_object_id
INNER JOIN sys.schemas s on t.schema_id = s.schema_id
WHERE s.name = @Schema
GROUP BY d.constraint_object_id, t.name
ORDER BY t.name;