チケット #40931

Transaction-like behaviour with COPY QUERY

登録: 2020-11-07 21:11 最終更新: 2020-11-08 23:45

報告者:
(匿名)
担当者:
(未割り当て)
チケットの種類:
状況:
完了
コンポーネント:
(未割り当て)
マイルストーン:
(未割り当て)
優先度:
5 - 中
重要度:
5 - 中
解決法:
修正済み
ファイル:
なし

詳細

Hi,

I'm attempting to copy data between two different databases. The system works fine:

-- !x! CONNECT TO SQLSERVER(...) as output_db -- !x! COPY QUERY <<!!query1!!>> FROM input_db TO DEST_TABLE IN output_db -- !x! COPY QUERY <<!!query2!!>> FROM input_db TO OTHER_DEST_TABLE IN output_db -- !x! DISCONNECT output_db -- !x! DISCONNECT input_db

However, I'm struggling to understand from the docs how to model a Transaction-like behaviour: if any COPY QUERY command fails, I'd love no data at all to be written to the destination database (e.g. rollback if the transaction fails).

By reading the docs it seems I should do the following:

- set AUTOCOMMIT to off - Wrap the block above in a BEGIN BATCH/END BATCH

However the docs for COPY QUERY report the following: "The data addition to the target table is always committed. Therefore, the COPY QUERY metacommand generally should not be used within transactions or BATCHes."

Does that mean transaction-like behaviours are not supported?

チケットの履歴 (4 件中 3 件表示)

2020-11-07 21:11 更新者: None
  • 新しいチケット "Transaction-like behaviour with COPY QUERY" が作成されました
2020-11-08 04:03 更新者: rdnielsen
  • 解決法なし から 修正済み に更新されました
コメント

The documentation of the COPY, COPY QUERY, and IMPORT metacommands has been modified to, perhaps, be a little clearer.

The commit that is automatically done by default by these metacommands is subject to the AUTOCOMMIT setting, but is not turned off by BEGIN BATCH.

Therefore, to perform multiple COPY operations and not commit any of them unless they all succeed, the approach should be to: 1) Set AUTOCOMMIT OFF for each database that is the target of a copy command; 2. Run the COPY metacommands; 3. After END BATCH, set AUTOCOMMIT ON WITH COMMIT for each database to which data were copied. If there is an error during copying, step 3 will not be executed, and a rollback will be performed on all databases when execsql exits.

A BATCH could be used within the AUTOCOMMIT OFF/ON statements, but it is unnecessary when AUTOCOMMIT is used for every individual database. AUTOCOMMIT is database-specific, whereas the BEGIN BATCH and END BATCH metacommands will turn off/on committing of SQL statements for all databases. The BATCH metacommands don't affect commits that may be made by COPY and IMPORT metacommands--only the AUTOCOMMIT metacommand affects those.

2020-11-08 19:53 更新者: None
コメント

Thank you very much, this is much clearer now!

2020-11-08 23:45 更新者: rdnielsen
  • 状況オープン から 完了 に更新されました

添付ファイルリスト

添付ファイルはありません

編集

ログインしていません。ログインしていない状態では、コメントに記載者の記録が残りません。 » ログインする