What are TCL Commands in SQL Server
Loading
What are TCL Commands in SQL Server
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tuhin PaulRank #7 · 71.9K reputationPosted Jan 26, 2025, 8:17 AM EST
Part -2
Say you are transferring funds from one account to another. If any step fails, the entire transaction should be rolled back to maintain data integrity.
-- Start a transaction BEGIN TRANSACTION; -- Deduct amount from Account A UPDATE Accounts SET Balance = Balance - 100 WHERE AccountID = 1; -- Add amount to Account B UPDATE Accounts SET Balance = Balance + 100 WHERE AccountID = 2; -- Check for errors IF @@ERROR <> 0 BEGIN -- Rollback the transaction if there's an error ROLLBACK; PRINT 'Transaction rolled back due to an error.'; END ELSE BEGIN -- Commit the transaction if there are no errors COMMIT; PRINT 'Transaction committed successfully.'; ENDWhen to use :
When performing multiple related operations that must succeed or fail together.
To roll back changes if an error occurs during execution.
To check the database remains consistent during complex operations.
Jaish MathewsRank #225 · 11K reputationPosted Jan 26, 2025, 1:44 AM EST
TCL (Transaction Control Language) commands in SQL Server are used to manage transactions within the database. These commands allow you to ensure data integrity and control over how changes are committed or reverted in the database. The primary TCL commands are:
1. COMMIT
COMMIT;2. ROLLBACK
ROLLBACK;BEGIN TRANSACTION.3. SAVEPOINT
4. SET TRANSACTION
SET TRANSACTION ISOLATION LEVEL { READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE };Example Usage
BEGIN TRANSACTION; -- Insert data INSERT INTO Employees (Name, Position) VALUES ('John Doe', 'Developer'); -- Savepoint SAVE TRANSACTION SavePoint1; -- Update data UPDATE Employees SET Position = 'Senior Developer' WHERE Name = 'John Doe'; -- Rollback to the savepoint ROLLBACK TRANSACTION SavePoint1; -- Commit the transaction COMMIT;Key Points
COMMITto save changes andROLLBACKto undo changes.SAVEPOINTadds flexibility in managing complex transactions.Tuhin PaulRank #7 · 71.9K reputationPosted Jan 26, 2025, 8:10 AM EST
Part -1
Transactions are sequences of operations performed as a single logical unit of work. TCL commands checks data integrity by allowing you to control when changes are saved permanently or rolled back in case of errors.