Hi,
I have a database 'Database1'.
Inside that i have multi-company structure as Company1, Company2 & so on.
I want to delete all the tables within Comapany1 including system tables, data tables with their data at one instance. Currently, i have to go to each table and press delete manually.
Can anyone please help with SQL query to carry out this task.
eg: Each type has 3 kinds of tables created. I want to delete entire things of Company1
dbo.Company1.MySettings.data
dbo.Company1.MySettings.sidedata
dbo.Company1.MySettings.audit
dbo.Company1.MyContacts.data
dbo.Company1.MyContacts.sidedata
dbo.Company1.MyContacts.audit


Naitik JaniRank #5,681 · 290 reputationPosted Apr 21, 2014, 12:48 AM EDT
You can try this commands
SELECT 'DROP TABLE "' + TABLE_NAME + '"'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'company1%'
So you will get list of Table names with queries and you can select result and paste it in Query window and after executing script all tables will be dropped.
You can create script for same.
Hope this helps !!
Thanks,
Naitik
Jasmine NagrechaRank #8,839 · 215 reputationPosted Apr 21, 2014, 3:24 AM EDT
Firstly thank you so much for your prompt reply.
The drop query works well with most tables. However, for few types it throws error of foreign key constraint and for few permission not available errors.
But when i delete the base type, i can than delete the reference data.
Overall much eased task now. Thanks once again.
regards,
Jasmine N
Naitik JaniRank #5,681 · 290 reputationPosted Apr 23, 2014, 3:25 AM EDT
Yes..you are right..
But that needs to be taken care by us which tables are referenced tables and which are primary tables but still will check if i can find another way that this list comes in way reference table first and last primary table.
sudipta sanyalRank #1,945 · 753 reputationPosted Apr 23, 2014, 10:37 PM EDT