Here are two ways to do this:
1. Execute Drop table statements - Copy the result of this query which generates DROP TABLE statements and execute the result again. Eg:
select 'drop table '+table_name from INFORMATION_SCHEMA.TABLES where table_name like 'tmp_%'
The above query generates drop table statements like
drop table tmp_test
drop table tmp_emp
drop table tmp_data
provided there are tables named tmp_test, tmp_test and tmp_data in the database.
2. Concatenate drop table statements and execute it as a whole
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+' drop table '+table_name from INFORMATION_SCEHMA.TABLES where table_name like 'tmp_%'
exec(@sql)
The variable @sql will have drop table statements concatenated for each table. It will have the string drop table tmp_test drop table tmp_emp drop table tmp_data
When you execute the string generated from that variable, all those tables get dropped
2 comments:
Since it's PostgreSQL, the + sign will not concatenate strings. You should do like this
select 'drop table ' || table_name || ';' from INFORMATION_SCHEMA.TABLES where table_name like 'tmp_%'
Adding the ; needed in order to not get the syntax error
Thanks very much!
Post a Comment