-4

I can't drop an SQL table even after I tried everything I found on the web. I actually want to delete and recreate an SQL table after clicking the "SAVE" button in my C# app (a simple SQL database editor). It works fine for tables with just data (no special rules like primary keys, forbidden nulls, etc.) but for more complicated ones I call a delete function (DROP TABLE table1), then a CREATE TABLE table1 ..., it throws an error "There is already an object named 'table1' in the database". Can anyone help? Thanks in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lazar Galić
  • 3
  • 1
  • 4
  • 1
    So `DROP TABLE` fails, you ignore errors and than creation fails too... Seem to be expected behavior. To clarify what is going on make sure to provide exact error messages for DROP and preferably sample setup for the DB (i.e. showing `CREATE ...` statements for simplest DB demonstrating the issues) – Alexei Levenkov May 08 '17 at 21:27
  • 2
    If you just need an empty table then you can try to use TRUNCATE TABLE instead of dropping and then recreate the same table. – Steve May 08 '17 at 21:30
  • Do you have Foreign Keys, constraints, etc still on the table? You need to ensure all other dependant objects have been removed first. – Andrew Harris May 08 '17 at 21:38
  • http://stackoverflow.com/questions/15107238/how-can-i-drop-a-table-if-there-is-a-foreign-key-constraint-in-sql-server – fqhv May 08 '17 at 21:44

1 Answers1

1

i think your issue the tables didn't drop. since there is a relationship between tables. So if the table you doping is a FK to other table , it will not go through. try DeleteRule = Cascade.

H. Rashid
  • 176
  • 13