| SQL Server Basics

Truncate Query in SQL Server

learn - query in ms sql server 2008, ms sql server 2012, ms sql server 2014, ms sql server 2016

Description

 The SQL SERVER Truncate query will all the records at a shot the in SQL Server.

Syntax

 Below is the sql code for delete statement in sql server

TRUNCATE TABLE table_name;

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | SQL Server Basics

Parameters or Arguments

 table  The table on which we need to truncate or delete all the records

at a shot.

Example- truncate statement in sql server

 Delete statement will one or more to filter the data before deleting is really important or else it will delete the entire records in the table.

Syntax

truncate table dbo.Customers

 The above statement will truncate the entire records from the table at a shot.

Delete from table dbo.customers

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | SQL Server Basics

 Even, delete statement do the same job right. Then, what’s the difference.

Delete vs truncate

1. Delete statement’s data can be rolled back as truncate statement data cannot be rolled bac 2. Delete statement is one by one record removal and the data is stored in log file. Truncate statement is a batch statement executed at a shot. 3. Truncate statement is faster than delete statement from query tuning and database performance perspective.

Syntax

DELETE FROM table_name WHERE [condition1] and [condition2]

Sample sql Code

 Prerequisite query:

 create table dbo.cutomers(customerid int, city varchar(100)) go

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | SQL Server Basics

 The above queries will create a table and the data will be inserted into it. Already if you got customers table which we created in our previous excercises. Do not worry, pass only delete statement to it. Here is the query,

 Delete from dbo.customers -- This query will delete all records and pass the statement.

 Now execute your insert statements.

insert into dbo.Customers values (1,'New York') insert into dbo.Customers values (2,'washington')

insert into dbo.Customers values (3,'Los Angeles') insert into dbo.Customers values (4,'Chicago') insert into dbo.Customers values (5,'Houston') insert into dbo.Customers values (6,'Boston') insert into dbo.Customers values (7,'Philadelphia') insert into dbo.Customers values (8,'San Diego') insert into dbo.Customers values (9,'Phoenix')

insert into dbo.Customers values (10,'Denver')

 Now execute your delete query

truncate table dbo.Customers

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | SQL Server Basics

Code Explanation

Delete the customer id =4 from the table customers. Fetch all the data from the table. We will receive data without the customer id =4.

Delete top 2 customers from the table.

Delete 50% of records from the table. Considering, If we got 7 records

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved. | SQL Server Basics

50% is more than 3 right. So, 4 records will be deleted.

Fetch the remaining records from the table.

Facebook.com/wikitechy twitter.com/wikitechy © Copyright 2016. All Rights Reserved.