Sample Header Ad - 728x90

SQL via JDBC fails on MSSQL Server with BEGIN TRAN

0 votes
0 answers
52 views
Running this SQL via JDBC on MSSQL works as expected:
UPDATE costcenter
SET    updatetimestamp = Getdate(),
       [de] = 'CostCenterName',
       [en] = 'CostCenterName_en'
WHERE  [customerid] = 'abcd'
       AND [principal] = '1'
But running this SQL with a Transaction fails:
BEGIN TRAN
IF EXISTS (SELECT [customerid],
                  [principal],
                  [companycode],
                  [costcenter]
           FROM   costcenter
           WHERE  [customerid] = 'abcd'
                  AND [principal] = '1')
  BEGIN
      UPDATE costcenter
      SET    updatetimestamp = Getdate(),
             [de] = 'CostCenterName',
             [en] = 'CostCenterName_en'
      WHERE  [customerid] = 'abcd'
             AND [principal] = '1'
  END
ELSE
  BEGIN
      INSERT INTO costcenter
                  (inserttimestamp,
                   updatetimestamp,
                   customerid,
                   principal,
                   companycode,
                   costcenter,
                   de,
                   en)
      VALUES      (Getdate(),
                   Getdate(),
                   'abcd',
                   '1',
                   '1',
                   '0',
                   'CostCenterName',
                   'CostCenterName_en')
  END
COMMIT TRAN
The reported error is: Incorrect syntax near the keyword 'BEGIN' Running this SQL on MS SQL Management Studio works. So I think it has to do with the JDBC-Connection.
Asked by Tobias G. (1 rep)
Sep 6, 2024, 10:31 AM