有没有办法设置一个可以导致PostgreSQL脚本继续的选项,即使有错误? [英] Is there a way to set an option that will cause a PostgreSQL script to continue even if there are errors?

查看:125
本文介绍了有没有办法设置一个可以导致PostgreSQL脚本继续的选项,即使有错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有一个命令来设置一个可以使PostgreSQL脚本继续运行的选项,例如有时候当我有大量的数据时一个用于插入到表中的电子表格,我使用公式来创建INSERT语句,并将这些语句复制到一个文件中并执行它们,或将它们复制到一个PgAdmin来运行它们。精确度并不总是很重要,我不希望整个过程由于几个记录而失败。



语法错误不是这里的问题,只是命令失败由于某些错误,如尝试创建已存在的索引或插入重复记录。

解决方案

如果使用 psql ,您需要:

  \set ON_ERROR_ROLLBACK on 

手册


ON_ERROR_ROLLBACK



当开启时,如果事务块中的语句产生错误,
错误将被忽略,事务继续。当交互式时,这种
错误只能在交互式会话中被忽略,而不是在读取
脚本文件时。关闭(默认)时,生成错误的事务
块中的语句会中止整个事务



Is there a command to set an option that will cause a PostgreSQL script to continue even if there are errors?

eg sometimes when I have a lot of data from a spreadsheet to insert into a table, I use a formula to create INSERT statements, and I copy the statements into a file and execute them, or copy them into a PgAdmin to run them. The accuracy is not always important and I don't want the whole process to fail because of a few records.

Syntax errors are not the issue here, just commands which fail due to some errors, like trying to create an index that already exists, or inserting a duplicate record.

解决方案

If using psql, you want:

\set ON_ERROR_ROLLBACK on

From the manual:

ON_ERROR_ROLLBACK

When on, if a statement in a transaction block generates an error, the error is ignored and the transaction continues. When interactive, such errors are only ignored in interactive sessions, and not when reading script files. When off (the default), a statement in a transaction block that generates an error aborts the entire transaction

这篇关于有没有办法设置一个可以导致PostgreSQL脚本继续的选项,即使有错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆