2013-10-29 46 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,該異常塊可以消除任何錯誤。 在代碼部署後,調試和故障排除問題都令人沮喪。

如果不是這個子句,你會得到錯誤信息,說明它爲什麼不起作用。