0% found this document useful (0 votes)
2 views4 pages

TCL in SQL Server

Uploaded by

Niranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

TCL in SQL Server

Uploaded by

Niranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Temporary Tables in SQL Server

Temporary tables are special tables that exist only for the duration of a
session or a transaction.
They are used to store intermediate results temporarily — like variables but for
sets of data.

🔹 Types of Temporary Tables


Name
Type Scope Description
Format
Local Visible only to the connection that
Current
Temporary #TempTable created it. Automatically dropped when
session
Table the session ends.
Visible to all users. Automatically
Global
All dropped when the session that created
Temporary ##TempTable
sessions it ends and all other sessions stop
Table
referencing it.

1. Local Temporary Table Example

-- Create a local temporary table


CREATE TABLE #EmployeeTemp
(
EmpId INT,
EmpName NVARCHAR(50),
Salary DECIMAL(10,2)
);

-- Insert data
INSERT INTO #EmployeeTemp VALUES (1, 'Ravi', 30000), (2, 'Neha', 40000);
-- Select data
SELECT * FROM #EmployeeTemp;

-- Drop the table manually (optional)


DROP TABLE #EmployeeTemp;

Global Temporary Table Example

-- Create a global temporary table


CREATE TABLE ##DepartmentTemp
(
DeptId INT,
DeptName NVARCHAR(50)
);

-- Insert data
INSERT INTO ##DepartmentTemp VALUES (1, 'IT'), (2, 'HR');

-- Access from multiple connections


SELECT * FROM ##DepartmentTemp;

-- Drop table manually (optional)


DROP TABLE ##DepartmentTemp;
TCL (Transaction Control Language) Commands in SQL Server
TCL (Transaction Control Language) commands in SQL Server are used to
manage transactions — that is, to control how changes made by DML
statements (INSERT, UPDATE, DELETE) are applied to the database.

TCL Commands in SQL Server


Command Description
BEGIN TRANSACTION Starts a new transaction.
COMMIT Saves (commits) all changes made during the
TRANSACTION transaction.
ROLLBACK Undoes (rolls back) changes made during the current
TRANSACTION transaction.
Sets a savepoint within a transaction that you can roll
SAVE TRANSACTION
back to.

1. BEGIN TRANSACTION / COMMIT TRANSACTION

BEGIN TRANSACTION;

INSERT INTO Employees VALUES (101, 'Ravi', 'IT', 40000);


UPDATE Employees SET Salary = 42000 WHERE EmpId = 101;

COMMIT TRANSACTION;

2.BEGIN TRANSACTION / ROLLBACK TRANSACTION


BEGIN TRANSACTION;

UPDATE Employees SET Salary = Salary + 5000 WHERE Dept = 'HR';


DELETE FROM Employees WHERE EmpId = 5;

-- Something went wrong!


ROLLBACK TRANSACTION;

3. SAVE TRANSACTION (Savepoint Example)

BEGIN TRANSACTION;

INSERT INTO Employees VALUES (201, 'Suresh', 'Finance', 50000);


SAVE TRANSACTION Save1; -- First savepoint

INSERT INTO Employees VALUES (202, 'Anita', 'Sales', 55000);


ROLLBACK TRANSACTION Save1; -- Rolls back to savepoint

COMMIT TRANSACTION;

You might also like