2

I am trying to drop tables from each server/DB.

I ran the query to get the list of the tables in each database from different server.

SELECT * 
FROM sys.objects 
WHERE type = 'u' AND name LIKE '%JSK%'

I want to drop those tables.

I need query how to do it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ETLUser
  • 371
  • 4
  • 7
  • 19
  • Which dbms and waht about dependances you can't drop customers before orders, if orders has a foreign key reference for CustomerID for instance. – Tony Hopkinson Mar 06 '13 at 19:06
  • those are user tables and not doing business with particular client name JSK. So we are trying to drop those USER Tables. We already checked data in all those table which only contain info from JSK only. – ETLUser Mar 06 '13 at 19:09

1 Answers1

5

Assuming no foreign key relationships make order of dropping important:

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql += N'
DROP TABLE '
  + QUOTENAME(SCHEMA_NAME([schema_id]))
  + '.' + QUOTENAME(name) + ';'
FROM sys.tables
WHERE name LIKE '%JSK%';

PRINT @sql;
-- EXEC sp_executesql @sql;
Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490