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


Microsoft SQL Server 2005 - Standard Edition 5 CAL's

Price: $1785
Buy It Now
SQL Server 2005 Enterprise Edition 1 CAL
Current Price: $4599.99
Current Bids: 0
228-00683 MS SQL Server 2000 Standard 5 CAL

Price: $375
Buy It Now
Microsoft SQL Server 2008 R2 Enterprise w/25 Device Cal
Price: $13049
Buy It Now
Microsoft SQL Server 2000 Standard 10 CAL 228-00684 NEW

Price: $939
Buy It Now
Microsoft SQL Server 2000 Standard Edition -Five CAL -

Price: $399
Buy It Now







Copyright 2010 - TSQLTutorials.com