在Netezza中删除多个表

时间:2014-01-08 22:15:22

标签: sql netezza

我只想删除以“T%”开头的所有表格。

db是Netezza。

有没有人知道这样做的sql?

此致

4 个答案:

答案 0 :(得分:2)

使用目录视图和execute immediate,在nzplsql中编写它是相当简单的。但要小心,call drop_like('%')会很快破坏数据库。

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;

答案 1 :(得分:1)

您可以创建脚本然后执行它。像这样......

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

<强>结果

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 

答案 2 :(得分:1)

您可以使用此查询在Netezza中找到具有特定名称的表。

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

@ M.Ali是正确的,如果要自动删除表,则需要将结果包装在脚本中。下面是一个执行任务的VBScript。

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"

答案 3 :(得分:-1)

运行以下SQL:

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

  

请确保在执行之前选择了正确的数据库   以上查询。

上述查询将返回如下输出:

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

现在,只需将上面的SQL结果复制到SQL编辑器中,然后在一次运行中删除所有表。