2013-10-29 122 views
0

我可和PostgreSQL合作,我想如果存在。我的代码DROP TABLE是PostgreSQL的删除表如果存在

execute 'drop table if exists production.'|| layer_name || '' into result_var; 
exception 
    when others then 
    execute 'insert into production.'|| layer_name || ' select * from staging.'|| layer_name ; 
return 1; 

但表如果不存在thows exeption。我不想特罗exeption

有谁能够帮助我?

+0

正如Daniel提到的,“WHEN OTHERS”块隐藏了错误。如果您打算使用这种全面的方法,使用“SQLERRM”或“GET STACKED DIAGNOSTICS”发出错误消息通常会很有帮助。请参阅http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html#PLPGSQL-EXCEPTION-DIAGNOSTICS – bma

回答

2

DROP TABLE不返回任何行,因此不得使用INTO子句。

此外,不要在异常块中使用WHEN OTHERS,该异常块可以消除任何错误。 在代码部署后,调试和故障排除问题都令人沮丧。

如果不是这个子句,你会得到错误信息,说明它为什么不起作用。