T-SQL Tutorials - TSQLTutorials.com

TRUNCATE TABLE Statement


Introduction

The TRUNCATE statement is used for removing all records from a table. The TRUNCATE statement is not a logged transaction. The TRUNCATE Statement has a simple syntax:

  • TRUNCATE tablename

Example

In this example we will assume that the 'Employees' table contains the follow columns: 'Name', 'Position', 'Office' and 'Salary'. SELECT *
FROM Employees
Here is a sample resultset from the above SELECT query:

NamePositionOfficeSalary
Joe GrapeManagerHouston80000
John PlumSoftware DeveloperHouston65000
Frank AppleSoftware DeveloperCleveland62000
Patty PineappleSoftware DeveloperCleveland60000
Judy PeachSoftware DeveloperBoston50000
Jane OrangeProject ManagerHouston75000




We'll use the TRUNCATE statement to remove all the records from the table. TRUNCATE TABLE Employees

Now we'll try to query the table again. We will find out that there are no longer any rows in the table. SELECT *
FROM Employees
NamePositionOfficeSalary
No Records


Factory Sealed - 228-10255 Microsoft SQL Server 2014 Standard 10 CAL (USA Only)

Price: $2150
Buy It Now
Microsoft Software & Training Material. Office, Server, SQL, SBS, CAL's, etc.

Current Price: $20
Current Bids: 0
Microsoft SQL Server 2008 R2 Standard or Enterprise 5 CAL Add-on Licenses New!

Price: $285
Buy It Now
Factory Sealed - 228-04023 Microsoft SQL Server 2005 Standard 5 CAL

Price: $685
Buy It Now
Microsoft SQL Server 2008 Standard Edition inc 10 CAL 228-08394

Price: $3761.15
Buy It Now
Dell PowerEdge 2950 6x 300GB SAS 32Gb Windows 2012 R2 SQL 2014 no CAL's Required

Price: $1995
Buy It Now







Copyright 2014 - TSQLTutorials.com