【问题标题】:Drop if exists in netezza如果 netezza 中存在则删除
【发布时间】:2014-12-24 18:56:50
【问题描述】:

如果 NETEZZA 中存在表,我需要一个命令来删除该表,类似这样:

drop table if exists xxx;

我已经搜索并尝试了很多,但没有成功。你能帮帮我吗?

【问题讨论】:

  • 为什么会有人投反对票?

标签: if-statement exists netezza


【解决方案1】:

没有内置任何内容,但您可以创建一个存储过程,它使用目录视图在尝试删除表之前检查表是否存在:

create or replace procedure maybe_drop(varchar(128))
    returns boolean
    language nzplsql
as
begin_proc
declare
    oname alias for $1;
    o record;
begin
    select otype into o
    from (
        select 'TABLE' otype from _v_table where tablename = upper(oname)
        union all
        select 'VIEW' otype from _v_view where viewname = upper(oname)
    ) x;

    if found then
        execute immediate 'DROP '||o.otype||' '||oname;
    end if;
end;
end_proc;

【讨论】:

  • 其实我需要一个SQL cmd,应该直接用这个作为查询吗?
  • 不能在nzplsql之外做,也就是说需要创建sproc。创建存储过程后,您可以使用 call maybe_drop('my_table'); 在单个语句中调用
  • 谢谢你们的回复:)
【解决方案2】:

netezza 中,您可以使用以下语法:

drop table table_name if exists;

【讨论】:

  • 从 7.2.0 开始出现在 docs
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-16
  • 2013-10-25
相关资源
最近更新 更多