Transaction can't be executed

Yumeng Zhang 1 Reputation point Microsoft Employee
2021-11-18T13:45:54.32+00:00

I'm running a statement in Synapse and want to create a new table from a old table, rename both tables, and drop the old table in a transaction block, but it shows me the "111212;Operation cannot be performed within a transaction" error. Could anyone tell me if there is a syntax error in my statement and how can I fix it?

The following is my statement:

BEGIN TRANSACTION;
CREATE TABLE [dbo].[TableA]
WITH
(
DISTRIBUTION = HASH ( [content_id] )
)
AS
SELECT *
FROM [dbo].[TableB]
OPTION (LABEL = 'CTAS : Delete');

RENAME OBJECT [dbo].[TableB] TO [TableB_old]
RENAME OBJECT [dbo].[TableA] TO [TableB]

drop table dbo.TableB_old;
COMMIT ;

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,135 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,676 questions
{count} votes

2 answers

Sort by: Most helpful
  1. HimanshuSinha-msft 19,476 Reputation points Microsoft Employee
    2021-11-24T20:21:14.177+00:00

    Hello @Yumeng Zhang ,
    Thanks for the ask and using Microsoft Q&A platform .
    Transaction is are only for "Data modification " ( Update , Insert & Delete ) , what you are trying is DDL ( Data definatiion query ) .

    Please read more here .https://learn.microsoft.com/en-us/sql/t-sql/language-elements/transactions-transact-sql?view=sql-server-ver15

    I am pasting the important lines from the article below ,

    A transaction is a single unit of work. If a transaction is successful, all of the data modifications made during the transaction are committed and become a permanent part of the database. If a transaction encounters errors and must be canceled or rolled back, then all of the data modifications are erased.

    Please do let me know how it goes .
    Thanks
    Himanshu

    -------------------------------------------------------------------------------------------------------------------------

    • Please don't forget to click on 130616-image.png or upvote 130671-image.png button whenever the information provided helps you. Original posters help the community find answers faster by identifying the correct answer. Here is how
    • Want a reminder to come back and check responses? Here is how to subscribe to a notification
    • If you are interested in joining the VM program and help shape the future of Q&A: Here is how you can be part of Q&A Volunteer Moderators
    2 people found this answer helpful.

  2. Erland Sommarskog 115.6K Reputation points MVP
    2021-11-18T22:58:59.157+00:00

    The error message says "Operation cannot be performed in a transaction", which should be clear enough. Remove BEGIN and COMMIT TRANSACTION.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.