您可能想要使用DBACCNOIGN
環境變量,它使DB-Access關注失敗的語句 - 並停止。如果已設置並且您啓動了事務,然後事務內的語句失敗,則DB-Access將終止,這意味着事務將被回滾。
例如:
$ DBACCNOIGN=1 dbaccess stores - <<'EOF'
> begin work;
> create table anything (num INT NOT NULL, str VARCHAR(20) NOT NULL);
> insert into anything values(1, "one");
> select * from abelone;
> insert into anything values(2, "two");
> select * from anything;
> commit work;
> EOF
Database selected.
Started transaction.
Table created.
1 row(s) inserted.
206: The specified table (abelone) is not in the database.
111: ISAM error: no record found.
Error in line 1
Near character position 21
377: Must terminate transaction before closing database.
853: Current transaction has been rolled back due to error
or missing COMMIT WORK.
$ dbaccess stores - <<'EOF'
> begin work;
> create table anything (num INT NOT NULL, str VARCHAR(20) NOT NULL);
> insert into anything values(1, "one");
> select * from abelone;
> insert into anything values(2, "two");
> select * from anything;
> commit work;
> EOF
Database selected.
Started transaction.
Table created.
1 row(s) inserted.
206: The specified table (abelone) is not in the database.
111: ISAM error: no record found.
Error in line 1
Near character position 21
1 row(s) inserted.
num str
1 one
2 two
2 row(s) retrieved.
Data committed.
Database closed.
$
然後我不得不再次使用DB-訪問命令來刪除創建的表什麼。
DBACCNOIGN
設定的值無關緊要;將其設置爲0
或1
或者一個空字符串都工作得很好。
這是一個有限的設施;您沒有程序控制是否忽略來自任何給定語句的錯誤。您要麼放棄出現第一個錯誤,要麼無論錯誤如何都繼續結束。
您可以考慮從IIUG(International Informix User Group)Software Archive獲得的'真正'的SQLCMD程序(而不是微軟的johnny-come-lately)。它允許您控制是否忽略來自任何給定語句組的錯誤。但是,它不會給你全面的流量控制 - 你不能有條件地執行語句。
是否有可能在{}塊內部從db中檢索值?我想在進行比較時使用該值來決定是否提交或回滾。 – oceanfeeling
嗨@oceanfeeling,我只使用SQL語句改進答案,沒有shell腳本處理... – ceinmart