0

I want to DROP all the tables with name starts with particular name, I wrote the script for the same but, when I try to delete the TABLE , I am getting Constraint issue.

So I want the approach to drop each table, in loop without/ignoring Relationships/Forieng_Key. Please suggested optimized script to perform the same operation. Please share your thoughts.

    DECLARE @SqlStatement VARCHAR(MAX)

    SET @SqlStatement = ''
    Print 'Deleting Tables and Columns from the Agency Table schema'
    SELECT @SqlStatement = 
        COALESCE(@SqlStatement, '') + 'DROP TABLE ['+@agencyName+'].' + QUOTENAME(TABLE_NAME) + ';' + CHAR(13)
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_SCHEMA = @agencyName;

    PRINT @SqlStatement
    --exec(@SqlStatement) -- In this Line I am getting the Foreign Key constraint issue. how do I achieve the functionality.

    --DROP SCHEMA Agency3
Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
Akshay Joy
  • 1,765
  • 1
  • 14
  • 23

0 Answers0