Question : Delete Tables with Certain Condition

Hi All,

I want to delete (drop) tables with certain condition.

How could I do it?

Thank you.

Answer : Delete Tables with Certain Condition

Try this code.
Tweak the condition until it outputs the right set of tables,
then un-comment the line:

--exec ('drop table ' + @tablename)

Please be very careful with this code and take backups of your database first!!!!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
declare @tablename varchar (100)
select name into #tables from sysobjects where type = 'U'
declare tables_cursor CURSOR FOR
select name from #tables
open tables_cursor
fetch next from tables_cursor into @tablename
while @@fetch_status = 0
	begin
		if @tablename >= 'THSJPOJUAL2000'
		begin
			print @tablename + ' will be deleted'
			--exec ('drop table ' + @tablename)
		end
		fetch next from tables_cursor into @tablename
	end
close tables_cursor
deallocate tables_cursor

drop table #tables
Random Solutions  
 
programming4us programming4us