Question

I just want to drop all table that start with "T%".

The db is Netezza.

Does anyone know the sql to do this?

Regards,

Was it helpful?

Solution

With the catalog views and execute immediate it is fairly simple to write this in nzplsql. Be careful though, call drop_like('%') will destroy a database pretty fast.

create or replace procedure drop_like(varchar(128))
    returns boolean
    language nzplsql
as
begin_proc
declare
    obj record;
    expr alias for $1;
begin
    for obj in select * from (
        select 'TABLE' kind, tablename name  from _v_table where tablename like upper(expr)
        union all
        select 'VIEW' kind, viewname name from _v_view where viewname like upper(expr)
        union all
        select 'SYNONYM' kind, synonym_name name from _v_synonym where synonym_name like upper(expr)
        union all
        select 'PROCEDURE' kind, proceduresignature name from _v_procedure where "PROCEDURE" like upper(expr)
    ) x
    loop
        execute immediate 'DROP '||obj.kind||' '||obj.name;
    end loop;
end;
end_proc;

OTHER TIPS

You can create a script and then execute it. something like this...

DECLARE @SQL nvarchar(max)

SELECT @SQL = STUFF((SELECT   CHAR(10)+ 'DROP TABLE ' + quotename(TABLE_SCHEMA) + '.' + quotename(TABLE_NAME)
               + CHAR(10) + 'GO '
               FROM INFORMATION_SCHEMA.TABLES WHERE Table_Name LIKE 'T%'
               FOR XML PATH('')),1,1,'')
PRINT @SQL

Result

DROP TABLE [dbo].[tTest2]
GO 
DROP TABLE [dbo].[TEMPDOCS]
GO 
DROP TABLE [dbo].[team]
GO 
DROP TABLE [dbo].[tbInflowMaster]
GO 
DROP TABLE [dbo].[TABLE_Name1]
GO 
DROP TABLE [dbo].[Test_Table1]
GO 
DROP TABLE [dbo].[tbl]
GO 
DROP TABLE [dbo].[T]
GO 

You can find tables with a certain name in Netezza using this query.

select *
from _v_table a
where lower(tablename) like 't%'

@M.Ali is correct that you need to wrap the results in a script if you want to automate dropping the tables. Below is a VBScript that would execute the task.

SQL = "select tablename from _v_table a where lower(table_name) like 't%'"

Set conn = CreateObject("ADODB.Connection")
conn.Open "NETEZZA_DSN" 

Set rs = conn.execute(SQL)   

    Do while not rs.eof 
        tblnm = rs.fields("tablename")
        DROP_TABLE_SQL = "drop table " &  tblnm
        wscript.echo "Dropping table: " & tblnm
        conn.execute  DROP_TABLE_SQL

    rs.movenext()
    Loop
conn.close
wscript.echo "Process Complete"

Run the below SQL:

SELECT 'DROP TABLE '||TABLENAME||';' FROM _V_TABLE WHERE TABLENAME LIKE 'T%'

Please ensure that correct database is selected before executing the above query.

The above query will return output like :

DROP TABLE TABLENAME1;
DROP TABLE TABLENAME2;
.
.
DROP TABLE TABLENAMEn;

Now, just copy the above SQL result to SQL editor and drop all the tables in a single run.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top