Drop all prefixed tables at once

Here is the simple way to delete all the object from the sql server database given the prefix of the objects.

Support you have created the tables with prefix “joo_”, the below script can delete all objects with the prefix “joo_”.

USE mydatabase /*** Change to your Database name ***/

GO

declare names_curr cursor read_only forward_only for

SELECT r='Drop Table ' + SCHEMA_NAME(schema_id)+'.'+name

FROM sys.tables where name like 'tableprefix_%'

declare @tmp_field as varchar(255)

open names_curr

fetch next from names_curr into @tmp_field

while (@@FETCH_STATUS = 0)

begin

Exec(@tmp_field)

fetch next from names_curr into @tmp_field

end

close names_curr

deallocate names_curr
, ,

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Share to Facebook Share to Twitter Stumble It Share on Tumblr Digg More...