Skip to main content
Question

Vertica transactions

  • March 22, 2023
  • 3 replies
  • 7 views

Goldengate

Hello guys, hope you're doing great!

I'm working with a SQL script on Dbeaver on Vertica 11.1 and I need to create a transaction with setting autocommit off and something like this:

BEGIN TRY
BEGIN TRANSACTION

    CREATE TABLE myTable (A INT NOT NULL, B VARCHAR(10),C  NUMERIC(10,2) NOT NULL)

    INSERT INTO myTable (myColumns ...) VALUES (myValues ...);
    INSERT INTO myTable (myColumns ...) VALUES (myValues ...);
    INSERT INTO myTable (myColumns ...) VALUES (myValues ...);

COMMIT TRAN  --DO IT NICELY!

END TRY
BEGIN CATCH
@TRANCOUNT > 0
-- ERROR ERROR ERROR ROLLBACK!!!!!!!!
ROLLBACK TRAN
END CATCH

Any ideas?

Regards

Fran

3 replies

SruthiA
Forum|alt.badge.img+1
  • Participating Frequently
  • March 22, 2023

Goldengate
  • Author
  • New Participant
  • March 22, 2023

Thx @SruthiA but I don't need SP just a script to create table, insert in the table one time. The point is I need to rollback the inserts if them fail and don't want AUTOCOMMIT in the way. So how can I accomplish my script of automatic rollback if gets any errors?

Regards

Fran


SruthiA
Forum|alt.badge.img+1
  • Participating Frequently
  • March 22, 2023

you can just create a .sql file with with create table and insert statement and set AUTOCOMMIT off and ON_ERR0R_STOP. It will rollback if insert fails with errors.

https://www.vertica.com/docs/11.1.x/HTML/Content/Authoring/ConnectingToVertica/vsql/Variables/ON_ERROR_STOP.htm