1

Is there a way to delete multiple object inside database? I have 20 tables, 40 views, 40 stored procedures, 20 triggers.

I don't want to delete them one by one. I've been naming all the objects starting with _, e.g. _TableName, _ViewName, _StoredProcedureName, etc.

Thank you.

1 Answer 1

1

You can achieve that using dynamic SQL and query catalog views for your objects (there's one for tables, views, procedures, etc.). You can then build up a list of drop statement separated by ; and execute it.

For example, for dropping tables it will be:

declare @sql nvarchar(max) = N''

select @sql = @sql + N'drop table ' + QUOTENAME(s.name) + N'.' + QUOTENAME(t.name) + N'; ' 
from sys.tables t
inner join sys.schemas s on t.schema_id = s.schema_id
where left(t.name, 1) = '_'

exec sp_executesql @sql
Sign up to request clarification or add additional context in comments.

4 Comments

I tried your code from new query windows then execute it.. It doesn't work. My table wasn't drop.
@Haminteu Sorry, when copying the code I missed the line that actually executes it. Please try now.
sys.tables.. Its for table, how about view ? Storeprocedure ?
Check this link: msdn.microsoft.com/en-us/library/ms189783%28v=sql.100%29.aspx. There's a view for views, procedures, triggers, etc.

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.