How to drop tables based on sys.objects? [duplicate]

会有一股神秘感。 提交于 2019-12-11 01:15:28

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/15255768/how-to-drop-tables-based-on-sys-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!