4

I'm searching for a generic SQL query that replaces the following procedures:

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------  

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

Example:

The following query drops foreign keys for specific table. Can it be converted to a generic one? (Please don't suggest dropping the whole database as I don't have permissions.)

SELECT 
'ALTER TABLE ' + OBJECT_NAME(parent_object_id) + 
' DROP CONSTRAINT ' + name
FROM sys.foreign_keys
WHERE referenced_object_id = object_id('Student')

Question:

How can I empty a database without dropping it?


Edit: Wait Wait, it's not a duplication question! The other question was about emptying rows ( or data ONLY ) keeping relations and tables. I'm trying to drop all data, tables & relations without dropping the database itself!

8
  • What is the purpose of doing this? Commented Dec 23, 2012 at 13:45
  • Emptying database without dropping it, Godaddy Rules! Commented Dec 23, 2012 at 13:46
  • Wait Wait, It's not a duplication one! other question was about emptying rows ( data ), I'm here for dropping database without really dropping it! Commented Dec 23, 2012 at 13:55
  • What do you mean by "Generic"? Commented Dec 23, 2012 at 15:02
  • 2
    I would use GoDaddy's backup/restore to repeatedly refresh a database back to an empty state, ie Create a new empty db once, back it up, and then periodically restore to a known (and empty) state. Commented Dec 24, 2012 at 2:44

1 Answer 1

1

Check out this answer How to drop all Foreign Key constraints in all tables?

This will drop your foreign keys from all tables, you can either add a drop table into the loop, or create a new loop afterwards to drop the tables

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.